Introduction
There are three points, where you can inject your dependecies. In previous part of this tutorial I have use injection to costructor. Available are also injections to setters or directly to fields. Today our domain will be order processing.Main
First some classes, which will be our dependecies:public class Order { private final String item; private final int amount; public Order(final String item, final int amount) { this.item = item; this.amount = amount; } public String getItem() { return item; } public int getAmount() { return amount; } }
public class OrderProcessor { public void process(final Order order) { // ... } }
public class OrderDao { public void save(final Order order) { // ... } }
class OrderValidator { public void validate(final Order order) { // ... } }
Controller which has all these dependecies is here:
public class OrderController { private final OrderDao orderDao; private OrderProcessor orderProcessor; @Inject private OrderValidator orderValidator; @Inject public OrderController(final OrderDao orderDao) { this.orderDao = orderDao; } @Inject public void setOrderProcessor(final OrderProcessor orderProcessor) { this.orderProcessor = orderProcessor; } public void invokeProcessing(final Order order) { orderValidator.validate(order); orderDao.save(order); orderProcessor.process(order); } }
As you can see, the Inject annotation is set on constructor of whole class, setter of OrderDao and on OrderValidator field. Injecting to constructor is the best option in my opinion, because all needed dependecies should be inject, when class is created.
Setter injection sometimes is helpful, for example when you need to inject something later using injectMember (it will be cover in one of next parts).
Field injecting is often a bad idea, because during testing this field should be protected or default (or public, but this is the worst idea) to mock this dependency. Of course you can use refletion, but reflection API is not easy to use. But you have this opportunity to inject field.
Testing
One simple test to prove that all this injection points works and there are not NullPointerExceptions.public class OrderControllerIT { @Test public void shouldInjectAllDependency() { //given Injector injector = Guice.createInjector(); Order order = new Order("apple", 10); //when OrderController sut = injector.getInstance(OrderController.class); //then sut.invokeProcessing(order); } }
Conclusion
You could inject dependecies in three placess in classes, but you almost always should use constructor injector. Injecting to setters should be hardly ever used. Field injecting is available, but should not be used.All sources are available here.
No comments:
Post a Comment