Spring is an open source control reversal (inversion of controls, IoC) and a container framework for aspect-oriented (AOP). Its main aim is to simplify enterprise development.
IOC control reversal
public class Personservicebean {
private Persondao Persondao = new Persondaobean ();
public void save [person person] {
persondao.save (person);
}
Persondaobean is created and maintained within the application. The so-called control reversal is that the application itself is not responsible for the creation and maintenance of dependent objects, and the creation and maintenance of dependent objects are handled by the external container. Thus the control is transferred from the application to the external container, and the transfer of control is the so-called reversal.
Dependency Injection (Dependency injection)
When we give the dependent object to the external container to create it, the Personservicebean class can be changed as follows:
public class Personservicebean {
private Persondao Persondao;
By using the constructor parameters, the container injects the created dependent object into the Personservicebean, and of course it can be injected with the setter method. Public
Personservicebean (Persondao persondao) {
This.persondao=persondao;
}
public void save [person person] {
persondao.save (person);
}
Dependency injection means that, at runtime, a dependent object is dynamically injected into an assembly by an external container.
Why do I use spring
At least in my opinion, introducing spring into a project can immediately bring the following benefits
Reduce the coupling between components to achieve decoupling between the layers of software.
You can use many of the services provided by the container, such as transaction management services, messaging services, and so on. When we use containers to manage transactions, developers no longer need to manually control transactions. Nor does it need to handle complex transactional propagation.
The container provides a single case pattern support, and developers no longer need to write the implementation code themselves.
The container provides AOP technology, it is easy to implement such functions as permission interception, runtime monitoring and so on.
The numerous auxiliary classes provided by the container, using these classes can accelerate the development of applications, such as: JdbcTemplate, Hibernatetemplate.
Spring provides integrated support for the mainstream application framework, such as integrated hibernate, JPA, struts, and so on, which makes it easier to develop applications.
If we use spring, we no longer need to manually control the transaction
Also, if you are using spring, we do not need to deal with complex transactional propagation behavior