File tree Expand file tree Collapse file tree 10 files changed +179
-0
lines changed
src/main/java/by/andd3dfx/guice Expand file tree Collapse file tree 10 files changed +179
-0
lines changed Original file line number Diff line number Diff line change 26
26
<groupId >org.projectlombok</groupId >
27
27
<artifactId >lombok</artifactId >
28
28
</dependency >
29
+
30
+ <!-- Guice -->
31
+ <dependency >
32
+ <groupId >com.google.inject</groupId >
33
+ <artifactId >guice</artifactId >
34
+ <version >5.1.0</version >
35
+ </dependency >
29
36
</dependencies >
30
37
31
38
<build >
Original file line number Diff line number Diff line change
1
+ package by .andd3dfx .guice ;
2
+
3
+ import static java .lang .annotation .RetentionPolicy .RUNTIME ;
4
+
5
+ import java .lang .annotation .Retention ;
6
+ import javax .inject .Qualifier ;
7
+
8
+ @ Qualifier
9
+ @ Retention (RUNTIME )
10
+ @interface Count {
11
+
12
+ }
Original file line number Diff line number Diff line change
1
+ package by .andd3dfx .guice ;
2
+
3
+ import by .andd3dfx .guice .util .Communicator ;
4
+ import by .andd3dfx .guice .util .DefaultCommunicatorImpl ;
5
+ import by .andd3dfx .guice .util .Spawner ;
6
+ import by .andd3dfx .guice .util .SpawnerImpl ;
7
+ import com .google .inject .AbstractModule ;
8
+ import com .google .inject .Provides ;
9
+
10
+ /**
11
+ * Guice module that provides bindings for message and count used in {@link Greeter}.
12
+ */
13
+ public class DemoModule extends AbstractModule {
14
+
15
+ @ Provides
16
+ @ Count
17
+ static Integer provideCount () {
18
+ return 3 ;
19
+ }
20
+
21
+ @ Provides
22
+ @ Message
23
+ static String provideMessage () {
24
+ return "hello world" ;
25
+ }
26
+
27
+ @ Override
28
+ protected void configure () {
29
+ bind (Communicator .class ).to (DefaultCommunicatorImpl .class );
30
+ bind (Spawner .class ).to (SpawnerImpl .class );
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ package by .andd3dfx .guice ;
2
+
3
+ import by .andd3dfx .guice .util .Communicator ;
4
+ import by .andd3dfx .guice .util .Spawner ;
5
+ import javax .inject .Inject ;
6
+
7
+ public class Greeter {
8
+
9
+ private final String message ;
10
+ private final int count ;
11
+ private Communicator communicator ;
12
+
13
+ // Field injection example
14
+ @ Inject
15
+ private Spawner spawner ;
16
+
17
+ // Constructor injection example
18
+ // Greeter declares that it needs a string message and an integer representing the number of time the message to be printed.
19
+ // The @Inject annotation marks this constructor as eligible to be used by Guice
20
+ @ Inject
21
+ public Greeter (@ Message String message , @ Count int count ) {
22
+ this .message = message ;
23
+ this .count = count ;
24
+ }
25
+
26
+ // Method injection example
27
+ @ Inject
28
+ public void setCommunicator (Communicator communicator ) {
29
+ this .communicator = communicator ;
30
+ }
31
+
32
+ public void sayHello () {
33
+ for (int i = 0 ; i < count ; i ++) {
34
+ System .out .println (message );
35
+ }
36
+ }
37
+
38
+ public void communicate () {
39
+ communicator .communicate ();
40
+ }
41
+
42
+ public void spawn () {
43
+ spawner .spawn ();
44
+ }
45
+ }
Original file line number Diff line number Diff line change
1
+ package by .andd3dfx .guice ;
2
+
3
+ import by .andd3dfx .guice .util .Communicator ;
4
+ import by .andd3dfx .guice .util .Spawner ;
5
+ import com .google .inject .Guice ;
6
+ import com .google .inject .Injector ;
7
+
8
+ /**
9
+ * According to Guice "Getting Started" page: https://github.com/google/guice/wiki/GettingStarted
10
+ * <p>
11
+ * and Baeldung Guice manual: https://www.baeldung.com/guice
12
+ */
13
+ public class GuiceDemo {
14
+
15
+ public static void main (String [] args ) {
16
+ /*
17
+ * Guice.createInjector() takes one or more modules, and returns a new Injector
18
+ * instance. Most applications will call this method exactly once, in their
19
+ * main() method.
20
+ */
21
+ Injector injector = Guice .createInjector (new DemoModule ());
22
+
23
+ /*
24
+ * Now that we've got the injector, we can build objects.
25
+ */
26
+ Greeter greeter = injector .getInstance (Greeter .class );
27
+
28
+ // Prints "hello world" 3 times to the console.
29
+ greeter .sayHello ();
30
+ greeter .communicate ();
31
+ greeter .spawn ();
32
+
33
+ // Make communication
34
+ Communicator communicator = injector .getInstance (Communicator .class );
35
+ communicator .communicate ();
36
+
37
+ // Make some spawning
38
+ Spawner spawner = injector .getInstance (Spawner .class );
39
+ spawner .spawn ();
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+ package by .andd3dfx .guice ;
2
+
3
+ import static java .lang .annotation .RetentionPolicy .RUNTIME ;
4
+
5
+ import java .lang .annotation .Retention ;
6
+ import javax .inject .Qualifier ;
7
+
8
+ @ Qualifier
9
+ @ Retention (RUNTIME )
10
+ @interface Message {
11
+
12
+ }
Original file line number Diff line number Diff line change
1
+ package by .andd3dfx .guice .util ;
2
+
3
+ public interface Communicator {
4
+
5
+ void communicate ();
6
+ }
Original file line number Diff line number Diff line change
1
+ package by .andd3dfx .guice .util ;
2
+
3
+ public class DefaultCommunicatorImpl implements Communicator {
4
+
5
+ @ Override
6
+ public void communicate () {
7
+ System .out .println ("Making friendly communication..." );
8
+ }
9
+ }
Original file line number Diff line number Diff line change
1
+ package by .andd3dfx .guice .util ;
2
+
3
+ public interface Spawner {
4
+
5
+ void spawn ();
6
+ }
Original file line number Diff line number Diff line change
1
+ package by .andd3dfx .guice .util ;
2
+
3
+ public class SpawnerImpl implements Spawner {
4
+
5
+ @ Override
6
+ public void spawn () {
7
+ System .out .println ("Spawn something useful..." );
8
+ }
9
+ }
You can’t perform that action at this time.
0 commit comments