File tree Expand file tree Collapse file tree 6 files changed +95
-0
lines changed
main/java/com/baeldung/guice
test/java/com/baeldung/guice Expand file tree Collapse file tree 6 files changed +95
-0
lines changed Original file line number Diff line number Diff line change 105
105
<artifactId >javax.persistence-api</artifactId >
106
106
<version >2.2</version >
107
107
</dependency >
108
+ <dependency >
109
+ <groupId >com.google.inject</groupId >
110
+ <artifactId >guice</artifactId >
111
+ <version >7.0.0</version >
112
+ </dependency >
108
113
<dependency >
109
114
<groupId >com.github.ben-manes.caffeine</groupId >
110
115
<artifactId >caffeine</artifactId >
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .guice ;
2
+
3
+ public interface Logger {
4
+ String log (String message );
5
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .guice ;
2
+
3
+ public interface Notifier {
4
+ void sendNotification (String message );
5
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments