Dependency Injection and Inversion of Control are the same concepts. Specifically, when a role (possibly a Java instance, caller) needs the assistance of another role (another Java instance, called), in the traditional programming process, generally, the caller creates the called instance. However, in Spring, the creation of the called instance is no longer completed by the caller, so it is called control inversion. The creation of the called instance is usually completed by the Spring container and then injected into the caller, therefore, it is also called dependency injection.
Benefits of using Spring
1. Reduce coupling between components;
Original
Class UserService {
UserDao udao = new UserDaoImpl ();
}
To use Spring, you only need to inject attributes.
Class UserService {
Private UserDao udao;
Public void setUdao (UserDao udao ){
This. udao = udao;
}
Public UserDao getUdao (){
Return udao;
}
}
2. Spring provides multiple service support, such as transactions and jms services. However, when using transaction management, developers do not need to manually control transactions;
3. Single-state mode
4. Provides an AOP mechanism to easily implement permission interception and pre-runtime monitoring.
5. Support mainstream frameworks