Spring is an open source inversion of control (IoC inversion of control) and a slicing (AOP) oriented container framework, which is designed to simplify enterprise development.
Please inquire about the content of inversion control. Simply put: The application itself is not responsible for the creation and maintenance of objects, the creation and maintenance of objects is the responsibility of the external container, so that control is given to the external container, reducing the degree of coupling between the code.
Give an example of a code:
Originally created a class:
Class people{
Mans xiaoming = new Man ();
public string males;
public int age;
}
When we instantiate an object xiaoming we need to new a class in the people class, but through the spring container we can instantiate it with an external XML file, and in people we just need to declare a Xiaoming object.
Class pepele{
Private Man xiaoming;
public string males;
public int age;
}
To create a new XML configuration file:
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:p= "http://www.springframework.org/schema/p"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd ">
......
</beans>
You can copy such a piece of code directly from the description document in spring.
Instantiate the Spring container:
There are two or more ways to instantiate a spring container ClassPathXmlApplicationContext FileSystemXmlApplicationContext .
(1) Looking for a configuration file in the Classpath to instantiate the container
ApplicationContext CTX = new ClassPathXmlApplicationContext (new string[]{"Bean.xml"});
(2) Looking for a configuration file under File system to instantiate a container
ApplicationContext CTX = new FileSystemXmlApplicationContext (new string[]{"D:\\bean.xml"});
Because of the differences between Windows and Linux operating systems, we typically use the first way to instantiate.
configuring bean files and Adding classes
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:p= "http://www.springframework.org/schema/p"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean id= "Peopleservice" name= "" class= "Org.spring.beans.PeopleService" >
</bean>
</beans>
public class Peopleservice implements Peopleserviceinterf {
/* (Non-javadoc)
* @see Org.spring.beans.peopleserviceinterf#testbeans ()
*/
@Override
public void Testbeans () {
SYSTEM.OUT.PRINTLN ("Test is instantiated!");
}
}
Test
@Test
public void Test () {
Instantiate a spring container
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Bean.xml");
Peopleservice Peopleservice = (peopleservice) ctx.getbean ("Peopleservice");
Peopleservice.testbeans ();
}
You can see that the test results are correct and the test output is "is the test instantiated?"
Advantages and Disadvantages
What are the IOC's greatest benefits? Because the object generation is defined in XML, when we need to change an implementation subclass it will be very simple (generally such objects are implemented in some kind of interface), as long as the XML can be modified, so we can even implement object hot plug (like USB interface and SCSI hard disk). In addition, the spring container provides AOP technology, enabling the ability to intercept, run-time monitoring and more, as well as integrate with third-party frameworks such as Hibernate and JdbcTemplate. What is the IOC's biggest drawback? (1) The steps to generate an object become complicated (in fact, it is quite simple in operation), for those who are not accustomed to this way, will feel somewhat awkward and not intuitive. (2) object generation because of the use of reflection programming, the efficiency of some loss. But this loss is negligible relative to the increased maintainability and flexibility of the IOC, unless the generation of an object is particularly demanding in terms of efficiency. (3) Lack of support for IDE refactoring operations, if you want to rename the class in Eclipse, then you need to go to the XML file manually to change, which seems to be the shortcoming of all the XML way. (Baidu Encyclopedia)
Spring (1)