The spring container provides two interface containers for the bean assembly, namely the "ApplicationContext interface Container " and the "Beanfactory interface container ", where " The Beanfactory interface container is the top interface container of spring, and theApplicationContext interface container inherits the beanfactory interface container . , and two interface containers are different for the timing of the bean assembly, theApplicationContext interface Container assembles all the beans in the container during the initialization phase, and The Beanfactory Interface container interface container is the Getbean method that is called on the container object to assemble the bean.
In addition, spring's two interface containers when the bean is assembled, is to call the class's parameterless construction method, create a Null value object.
Let's illustrate this by example.
Add Maven dependencies.
<Properties> <org.springframework.version>4.1.4.RELEASE</org.springframework.version> </Properties> <Dependencies> <!--Spring Related Jars - <Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-beans</Artifactid> <version>${org.springframework.version}</version> </Dependency> <Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-context-support</Artifactid> <version>${org.springframework.version}</version> </Dependency> </Dependencies>
Student class
Public class Student { public Student () { System.out.println ("Spring Load String Class"); } }
Configuration file:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:task= "Http://www.springframework.org/schema/task"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <BeanID= "Student"class= "Com.opensource.bean.Student"></Bean> </Beans>
Test class:
Public class MyTest { publicstaticvoid main (string[] args) { new Classpathxmlapplicationcontext ("Spring-bean.xml"); Beanfactory BFnew xmlbeanfactory (new classpathresource ("Spring-bean.xml")); Bf.getbean ("student");} }
Let's take a look at each of the two containers for the log when creating an instance of the Student class:
(1) ApplicationContext interface container
(2) The Beanfactory interface container, which calls the Getbean ("Student") method only, invokes the student parameterless construction method, creating an instance of Studnet in the container.
Call Getbean ("student");
Here we can think of spring's configuration file as an explicit representation of the spring container in a narrow sense, because in the configuration file we can see that the containers are assembling those beans. From the container's top-level interface beanfactory It's easy to see that the Spring container uses Factory mode for bean creation, from the <bean id=" student " Class=" Com.opensource.bean.Student "></bean>
To conclude, we, as programmers, are going to go into a little bit of a thorough study of the problem. When you have a thorough understanding of the principle, development is also handy, a lot of development problems and doubts will be solved, and in the face of other problems can also be done comprehend by analogy. Of course, in the development of not too much time to let you study the principle of development in order to achieve the premise of the function, you can wait for the project on-line, you have a lot of time or spare time, you can go to the inquisitive, in-depth study of a technology, in order to think this is a programmer's growth is very important thing.
Assembly of the bean of the SPRING2--IOC