Skip to content

Commit 23aecd5

Browse files
committed
[BAEL-6028] Guice Provider class vs @provides
1 parent 1ccaa17 commit 23aecd5

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

libraries-data/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@
105105
<artifactId>javax.persistence-api</artifactId>
106106
<version>2.2</version>
107107
</dependency>
108+
<dependency>
109+
<groupId>com.google.inject</groupId>
110+
<artifactId>guice</artifactId>
111+
<version>7.0.0</version>
112+
</dependency>
108113
<dependency>
109114
<groupId>com.github.ben-manes.caffeine</groupId>
110115
<artifactId>caffeine</artifactId>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.guice;
2+
3+
import com.google.inject.Provider;
4+
5+
public class EmailNotifier implements Notifier, Provider<Notifier> {
6+
7+
private String smtpUrl;
8+
private String user;
9+
private String password;
10+
private EmailNotifier emailNotifier;
11+
12+
@Override
13+
public Notifier get() {
14+
// perform some initialization for email notifier
15+
this.smtpUrl = "smtp://localhost:25";
16+
emailNotifier = new EmailNotifier();
17+
return emailNotifier;
18+
}
19+
20+
@Override
21+
public void sendNotification(String message) {
22+
System.out.println("Sending email notification: " + message);
23+
}
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.guice;
2+
3+
public interface Logger {
4+
String log(String message);
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.guice;
2+
3+
import com.google.inject.AbstractModule;
4+
import com.google.inject.Provides;
5+
6+
public class MyGuiceModule extends AbstractModule {
7+
/**
8+
* This method is called when the Guice injector is created.
9+
* It binds the Notifier interface to the EmailNotifier implementation.
10+
*/
11+
12+
@Override
13+
protected void configure() {
14+
bind(Notifier.class).to(EmailNotifier.class);
15+
}
16+
17+
@Provides
18+
public Logger provideLogger() {
19+
return new Logger() {
20+
@Override
21+
public String log(String message) {
22+
return "Logging message: " + message;
23+
}
24+
};
25+
}
26+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.guice;
2+
3+
public interface Notifier {
4+
void sendNotification(String message);
5+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.guice;
2+
3+
import org.junit.Test;
4+
import org.junit.jupiter.api.Assertions;
5+
6+
import com.google.inject.Guice;
7+
import com.google.inject.Injector;
8+
9+
public class GuiceProviderTester {
10+
@Test
11+
public void givenGuiceProvider_whenInjecting_thenShouldReturnEmailNotifier() {
12+
// Create a Guice injector with the NotifierModule
13+
Injector injector = Guice.createInjector(new MyGuiceModule());
14+
// Get an instance of Notifier from the injector
15+
Notifier notifier = injector.getInstance(Notifier.class);
16+
// Assert that notifier is of type EmailNotifier
17+
assert notifier != null;
18+
assert notifier instanceof EmailNotifier;
19+
}
20+
21+
@Test
22+
public void givenGuiceProvider_whenInjectingWithProvides_thenShouldReturnCustomLogger() {
23+
// Create a Guice injector with the NotifierModule
24+
Injector injector = Guice.createInjector(new MyGuiceModule());
25+
// Get an instance of Logger from the injector
26+
Logger logger = injector.getInstance(Logger.class);
27+
assert logger != null;
28+
Assertions.assertNotNull(logger.log("Hello world"));
29+
}
30+
}

0 commit comments

Comments
 (0)