这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libraries-data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
Expand Down
24 changes: 24 additions & 0 deletions libraries-data/src/main/java/com/baeldung/guice/EmailNotifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baeldung.guice;

import com.google.inject.Provider;

public class EmailNotifier implements Notifier, Provider<Notifier> {

private String smtpUrl;
private String user;
private String password;
private EmailNotifier emailNotifier;

@Override
public Notifier get() {
// perform some initialization for email notifier
this.smtpUrl = "smtp://localhost:25";
emailNotifier = new EmailNotifier();
return emailNotifier;
}

@Override
public void sendNotification(String message) {
System.out.println("Sending email notification: " + message);
}
}
5 changes: 5 additions & 0 deletions libraries-data/src/main/java/com/baeldung/guice/Logger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.guice;

public interface Logger {
String log(String message);
}
26 changes: 26 additions & 0 deletions libraries-data/src/main/java/com/baeldung/guice/MyGuiceModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baeldung.guice;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;

public class MyGuiceModule extends AbstractModule {
/**
* This method is called when the Guice injector is created.
* It binds the Notifier interface to the EmailNotifier implementation.
*/

@Override
protected void configure() {
bind(Notifier.class).to(EmailNotifier.class);
}

@Provides
public Logger provideLogger() {
return new Logger() {
@Override
public String log(String message) {
return "Logging message: " + message;
}
};
}
}
5 changes: 5 additions & 0 deletions libraries-data/src/main/java/com/baeldung/guice/Notifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.guice;

public interface Notifier {
void sendNotification(String message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.baeldung.guice;

import org.junit.Test;
import org.junit.jupiter.api.Assertions;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class GuiceProviderTester {
@Test
public void givenGuiceProvider_whenInjecting_thenShouldReturnEmailNotifier() {
// Create a Guice injector with the NotifierModule
Injector injector = Guice.createInjector(new MyGuiceModule());
// Get an instance of Notifier from the injector
Notifier notifier = injector.getInstance(Notifier.class);
// Assert that notifier is of type EmailNotifier
assert notifier != null;
assert notifier instanceof EmailNotifier;
}

@Test
public void givenGuiceProvider_whenInjectingWithProvides_thenShouldReturnCustomLogger() {
// Create a Guice injector with the NotifierModule
Injector injector = Guice.createInjector(new MyGuiceModule());
// Get an instance of Logger from the injector
Logger logger = injector.getInstance(Logger.class);
assert logger != null;
Assertions.assertNotNull(logger.log("Hello world"));
}
}