So apart from recovering from a long bout of the stomach flu, enjoying good food again at a "Come dine with me" night (our parents would be so proud) and planning a rush trip to Vegas I've managed to get out a first draft of the Dependency Injection chapter for the Well Grounded Java 7 Developer.
Java 7 unifies some of the basic standards that the various Dependency Injection frameworks (Spring, Guice, PicoContainer etc) have, making it easier for developers to move between the frameworks as needed.
A reminder of what DI is with regards to Java
The javax.inject package specifies a means for obtaining objects in such a way as to maximize re-usability, testability and maintainability compared to traditional approaches such as constructors, factories, and service locators (e.g., JNDI). This process, known as Dependency Injection, is beneficial to most nontrivial applications.
So without further preamble, here's a short section from the chapter, let me know what you think!
The @Inject annotation interface can be used in three places to indicate where you'd like a dependency to be injected. Below are the types of members that can be injected, in the order that they are processed at runtime:
You can annotate a constructor with @Inject and expect its parameters to be provided at runtime by your configured IoC container e.g:
@Inject public MurmurMessage(Header header, Content content)
this.header = header;
this.content = content;
In this case both the Header and Content parameters would be injected at runtime. The specification allows for 0+ parameters to be injected for constructors, so injecting a zero-parameter constructor is still valid.
WARNING As per the specification there can only be one constructor in a class with an @Inject annotation, this makes sense as the JRE would not be able to decide which injected constructor took precedence.
You can annotate a method with @Inject and like a constructor, expect its 0+ number of parameters to be injected at runtime. There are some restrictions in that injected methods cannot be declared abstract and cannot declare type parameters of their own. The short code sample below demonstrates the use of @Inject with a setter method, a common technique when using Dependency Injection.
@Inject public void setContent(Content content)
this.content = content;
This technique of method parameter injection is especially powerful when it comes to providing service methods with the resources they need to do their job. For example you could pass a DAO argument to a finder service method that was tasked to retrieve some data.
TIP It has become a default best-practice to use constructor injection for setting mandatory dependencies for a class and to used setter injection for non-mandatory dependencies, e.g. fields that already have sensible defaults.
It is also possible to inject fields (as long as they are not final), however the practice is not common. The syntax again is quite simple.
public class MurmurMessenger
@Inject private MurmurMessage murmurMessage;
You can read further about the @Inject annotation in the Javadoc, where you can discover some nuances about what types of values can be injected and how circular dependencies are dealt with.