Inversion of Control and Dependency Injection is a core design pattern of Spring framework. IOC and DI design pattern is also a popular design pattern interview question in Java. As name suggest Inversion of control pattern Inverts responsibility of managing life cycle of object e.g. creating object, setting there dependency etc from application to framework, which makes writing Java application even more easy. Programmer often confused between IOC and DI, well both words used interchangeably in Java world but Inversion of Control is more general concept and Dependency Injection is a concrete design pattern. Spring framework provides two implementation of IOC container in form of Application Context and BeanFactory which manages life-cycle of bean used by Java application. As you may know necessity is mother of invention, it benefit to first understand problem solved by IOC and Dependency Injection design pattern. This makes your understanding more clear and concrete. We have touched basics of Dependency Injection and Inversion of control in our article 10 OOPS and SOLID design principles for Java programmer and this Java article tries to explain it by taking a real life example of Service based architecture popular in enterprise Java development. In this Spring or design pattern tutorial we will first see normal implementation of AutditService class, a class in this example which provides auditing in enterprise Java application and than use of dependency Injection. This will allow us to find out problems and how they are solved by Dependency injection design pattern. . Also there are multiple way to inject dependency in spring e.g. Setter Injection or Constructor Injection, which uses setter method and constructor for injecting dependency, see Setter injection vs Constructor injection to find out when to use them.
Inversion of Control and Dependency Injection design pattern
* Java Service class which provides auditing functionality by storing
* auditing message into persistent.
*/
public class AuditServiceImpl implements AuditService{
private AuditDAO auditDao = new AuditDAO();
@Override
public boolean audit (String message) {
return auditDao.store(message);
}
}
1) Every AuditServiceImpl has its own copy of AuditDAO which is an expensive object as it wraps a database connection with in. It make no sense to create separate instances of AuditDAO, if you can share one between multiple AuditService.
2) AuditServiceImpl is closely coupled with AuditDAO as its creating instance of AuditDAO using new() operator. If you change the constructor of AuditDAO this code will be broken. Because of this coupling its difficult to replace AuditDAO with better implementation.
3) There is no easy way to test audit() method which is dependent on auditDAO. Since you can not mock AuditDAO you have to rely on actual implementation and if AuditDAO is an environmental dependent object which it is as it connect to different database on different environment, your Junit test case may pass in some environment and may fail in other environment.
What is Dependency Injection concept:
Dependency Injection is a design pattern on which dependency of object (in this case AuditDAO is a dependency for AuditServiceImpl Object) is injected by framework rather than created by Object itself. Dependency Injection reduces coupling between multiple object as its dynamically injected by framework. One of the implementation of DI is Inversion of Control (IOC) on which framework like Spring controls object’s dependency. There are mainly two types of Dependency Injection: Constructor Injection and Setter Injection.
In Constructor Injection, dependency of Object is injected using constructor, while in Setter Injection, Dependency is provided by setter method. Both has there pros and cons. Constructor DI allows object to be created in complete state and follows principle of fully functional object while Setter DI allows object to be created without its dependency. which may result in incomplete object if dependency is not available. This answers one of the famous spring interview question "when do you use Setter injection and Constructor Injection in Spring". Another benefit of Setter Dependency Injection is readability, since Spring is configured with xml configuration file and setter injection is provided with bean property which is much easier to read and understand than constructor injection which doesn't state the property.
private AuditDAO auditDao;
public void setAuditDao(AuditDAO AuditDao) {
this.AuditDao = AuditDao;
}
@Override
public boolean audit (String message) {
return auditDao.store(message);
}
}
2. Since AuditServiceImpl is not creating instance of AuditDAO its no more coupled with AuditDAO and work with any implementation of AuditDAO, thanks to another famous object oriented design principle “program for interface than implementation".
Other design pattern and Spring tutorials from Javarevisited
Decorator design pattern in Java with real life example
How to implement LDAP authentication in Java using Spring security
When to use Factory method design pattern in Java
What is Builder design pattern in Java with real world example
What is Observer design pattern in Java with real life example
No comments:
Post a Comment