-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreeter.java
More file actions
48 lines (38 loc) · 1.2 KB
/
Copy pathGreeter.java
File metadata and controls
48 lines (38 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package by.andd3dfx.guice;
import by.andd3dfx.guice.util.Communicator;
import by.andd3dfx.guice.util.Spawner;
import lombok.extern.slf4j.Slf4j;
import javax.inject.Inject;
@Slf4j
public class Greeter {
private final String message;
private final int count;
private Communicator communicator;
// Field injection example
@Inject
private Spawner spawner;
// Constructor injection example
// Greeter declares that it needs a string message and an integer representing the number of time the message to be printed.
// The @Inject annotation marks this constructor as eligible to be used by Guice
@Inject
public Greeter(@Message String message, @Count int count) {
this.message = message;
this.count = count;
}
// Method injection example
@Inject
public void setCommunicator(Communicator communicator) {
this.communicator = communicator;
}
public void sayHello() {
for (int i = 0; i < count; i++) {
log.info(message);
}
}
public void communicate() {
communicator.communicate();
}
public void spawn() {
spawner.spawn();
}
}