3. Code without DI
public void sendButtonClicked() {
String text = messageArea.getText();
Validator validator = new MailValidator();
validator.validate(text);
MailSender sender = new MailSender();
sender.send(text);
}
4. Using factories
public void sendButtonClicked() {
String text = messageArea.getText();
Validator validator = ValidatorFactory.get();
validator.validate(text);
MailSender sender = SenderFactory.get();
sender.send(text);
}
5. Testable now
public void testSendButton() {
MockSender mockSender = new MockSender();
SenderFactory.setInstance(mockSender);
MailForm form = new MailForm();
form.getMessageArea().setText("Some text");
form.sendButtonClicked();
assertEquals("Some text", mockSender.getSentText());
SenderFactory.clearInstance();
}
6. Well, not really
public void testSendButton() {
MockSender mockSender = new MockSender();
SenderFactory.setInstance(mockSender);
try {
MailForm form = new MailForm();
form.getMessageArea().setText("Some text");
form.sendButtonClicked();
assertEquals("Some text", mockSender.getSentText());
} finally {
SenderFactory.clearInstance();
}
}
8. Testing now
public void testSendButton() {
MockSender mockSender = new MockSender();
Validator validator = new Validator();
MailForm form = new MailForm(validator, mockSender);
form.getMessageArea().setText("Some text");
form.sendButtonClicked();
assertEquals("Some text", mockSender.getSentText());
}
9. Why DI frameworks
Avoid boilerplate code
AOP
Integrate your DI with http session/request,
data access APIs, e.t.c
Separate dependencies configuration from
code
Makes life easier
10. What is Guice
Open source dependency injection framework
License : Apache License 2.0
Developer : Google
11. Why Guice
Java API for configuration
Easier to use
Line numbers in error messages
Less overhead
Less features, too
DI in GWT client-side code (using GIN)
12. Getting Guice
http://code.google.com/p/google-guice/
http://mvnrepository.com/
Small – 865 KB
Version without AOP, suitable for Android –
470KB
Lacks fast reflection and line numbers in errors
18. Injecting with setter methods
private Validator validator;
public MailForm() {
}
@Inject
public void setValidator(Validator validator) {
this.validator = validator;
}
19. Optional injection
Inject(optional=true)
Ignores values for which no bindings are
avalaible
Possible with setter methods only
20. Bind instance
protected void configure() {
bind(Integer.class).annotatedWith(ThreadCount.cla
ss).toInstance(4);
}
..
@ThreadCount
int threadCount;
IDE autocomplete, find usages e.t.c
21. Two implementations
protected void configure() {
bind(Validator.class).to(ValidatorImpl.class);
bind(Validator.class).to(StrictValidatorImpl.class);
}
22. Two implementations
Exception in thread "main" com.google.inject.CreationException:
Guice configuration errors:
1) Error at lv.jug.MailService.configure(YourAppModule.java:12):
A binding to lv.jug.MailService was already configured at
lv.jug.YourAppModule.configure(YourAppModule.java:11)
29. Grapher
Describe the object graph in detail
Show bindings and dependencies from several
classes in a complex application in a unified
diagram
Generates .dot file
30. Advanced Guice
AOP
Warp
Google Gin
Spring integration
Using guice with Servlets
36. Google Gin
Automatic dependency injection for GWT client-
side code
Code generation
Little-to-no runtime overhead, compared to
manual DI
Uses Guice binding language
http://code.google.com/p/google-gin/
37. Spring integration
http://code.google.com/p/guice-spring/
@Named(”mySpringBean”)