Assembly of the bean of the SPRING2--IOC

Source: Internet
Author: User

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://"Xmlns:xsi= "Http://"xmlns:p= ""Xmlns:context= "Http://"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://"Xmlns:task= "Http://"Xmlns:mvc= "Http://"xsi:schemalocation= "Http://                    Http://                    Http://                    HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP                    Http:// Http:// ">    <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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.