Skip to content

Commit 1c21d30

Browse files
committed
Pull Guice usage example from repo: https://github.com/andrei-punko/java-interview-coding
1 parent d9b8cbb commit 1c21d30

File tree

10 files changed

+179
-0
lines changed

10 files changed

+179
-0
lines changed

common/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
<groupId>org.projectlombok</groupId>
2727
<artifactId>lombok</artifactId>
2828
</dependency>
29+
30+
<!-- Guice -->
31+
<dependency>
32+
<groupId>com.google.inject</groupId>
33+
<artifactId>guice</artifactId>
34+
<version>5.1.0</version>
35+
</dependency>
2936
</dependencies>
3037

3138
<build>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package by.andd3dfx.guice.util;
2+
3+
public interface Communicator {
4+
5+
void communicate();
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package by.andd3dfx.guice.util;
2+
3+
public interface Spawner {
4+
5+
void spawn();
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}

0 commit comments

Comments
 (0)