In continuation with my effort of trying to simplify Dependency Injection, I want to elaborate on the different types of injection. Dependency Injection is decoupling an application and service so that the application does not need to know anything about the service implementation. Dependency injection can be broadly classified in three categories: Constructor, Setter and Interface.
Object dependencies when passed as parameters to a constructor is termed as Constructor Injection.
<br /> public class Account {<br />   public User user;<br />   public Account (User user) {<br />    this.user = user;<br />   }<br /> }<br /> </p>

Pico Container is a framework which prefers constructor injection. Constructor injection ensures that the application object is created with all its service dependencies satisfied. The constructor arguments clearly delineates the dependencies and makes the code easy to understand. Using constructor injection also relieves the developer to explicitly check for the validity of the created object i.e the constructor enforces that all the dependencies for the required object have been injected already. Another advantage of constructor injection is that immutable properties can be hidden by not providing a setter. If setters are used to initialize these objects it can become a problem because not having a setter clearly signifies that the object is non modifiable. The code snippet shown above is a simple example of an Account class where the dependency of User class is injected through the constructor.</div>

But, there are cases where Constructor injection can get messy, like having a class with a lot of dependencies. A constructor with many parameters can make the code hard to manage. Moreover, when dealing with multiple constructors and inheritance one has to initialize the super class correctly while adding your own parameters too. In such cases, using Setter Injection might be a better choice. In Setter Injection an explicit setter method is responsible for injecting the desired dependency as shown below.

<br /> public class Account {<br />   public User user;<br />   public setUser (User user) {<br />    this.user = user;<br />   }<br /> }<br />

The configurations to accomplish the injections can be set using XML or directly through code. One particular advantage of Setter injection is that it allows the creation of the resource as late as possible and only when it is needed. This is really useful in cases where the injected instance is expensive to create and is used rarely. The Spring framework prefers this type of injection although it does support constructor and interface injection.

The third category is of Interface Injection where an interface defines the injection method. The method providing the implementation has to implement the interface and as such the service is dependent on the interface and not on the implementation.  The example below shows the Account class which depends on the IUser interface and the AccountController class injects the required dependency in this case the object of the User class which implements the IUser interface.</p>

<br /> public class Account {<br />   public IUser user;<br />   public setUser (IUser user) {<br />    this.user = user;<br />   }<br /> }</p> <p>public class AccountController {<br />   public void initDefaultAccount () {<br />    Account defaultUser = new Account;<br />    defaultUser.setUser(new User());<br />   }<br /> }</p> <p>public class User implements IUser {<br />   public String name;<br />   public Long id;<br /> ...<br /> }


To sum up, Dependency Injection in itself a very good practice to keep your code understandable, efficient and robust. However, what type of injection to use depends more or less on your code. The constructor and setter injections are the more commonly used approaches and there is a lot of debate going on about which is better. Both of them have their strengths and weaknesses and a good rule of thumb which works for most cases is using constructor injection for required dependencies and setter injection for optional dependencies. More detailed information about Dependency Injection can be found here.