How SPRINGIOC_ objects are created

Source: Internet
Author: User

Several ways that spring creates objects

    1. The default construct
    2. Static Factory
    3. Instance Factory

One: default constructor method to create an object

1.applicationcontext.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5. XSD ">               <BeanID= "HelloWorld"class= "Com.spring.createobject.method.HelloWord"></Bean>    </Beans>

2. Test class

 Package Com.spring.createobject.method;  Public class Helloword {        public  Helloword () {        System.out.println ("Create object");    }          Public void Hello () {        System.out.println ("Hello word!" );    }}

3, test

   /**       * In the spring container, the default constructor for a class is called by default to create an object  */  public  void   Testcreateobject_default () { //  Kai Move Spring container  applicationcontext context= new  Classpathxmlap                Plicationcontext ("Applicationcontext.xml" );         //  Extract the beans from the spring container by ID                 Helloword Helloword = (Helloword) context.getbean ("HelloWorld" );                    Helloword.hello (); }

Output:

Create Object
Hello word!

Two: Static factory-created objects

1.applicationcontext.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5. XSD ">    <!--Factory-method refers to a static factory method -    <BeanID= "HelloWord2"class= "Com.spring.createobject.method.factory.HelloWordFactory"Factory-method= "GetInstance"></Bean>        </Beans>

2. Test class

 Package Com.spring.createobject.method;  Public class Helloword {        public  Helloword () {        System.out.println ("Create object");    }          Public void Hello () {        System.out.println ("Hello word!" );    }}

 Package com.spring.createobject.method.factory; Import Com.spring.createobject.method.HelloWord; // Static Factory Method  Public class hellowordfactory {        publicstatic  Helloword getinstance () {                 return New Helloword ();    }}

3. Testing

/*** * Create objects with static Factory mode * <bean id= "HelloWord2" class= "Com.spring.createobject.method.factory.HelloWord        Factory "factory-method=" getinstance "></bean> Spring container internally called getinstance method in Hellowordfactory to create object     The process of the specific new object is done by the programmer. */@Test Public voidtestcreateobject_staticfactory () {//start the spring containerApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); //extract the beans from the spring container according to the IDHelloword Helloword = (Helloword) context.getbean ("HelloWord2");    Helloword.hello (); }

Output:

Create Object
Hello word!

Three: Instance factory creation Object

1.applicationcontext.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5. XSD ">                      <BeanID= "Hellowordfactory"class= "Com.spring.createobject.method.factory.HelloWordFactory2"></Bean>        <!--Factory-bean is a factory bean that invokes the object that Hellowordfactory points to, and then calls GetInstance to create the object Factory-method is a factory method -        <BeanID= "Helloword3"Factory-bean= "Hellowordfactory"Factory-method= "GetInstance"></Bean>                </Beans>

2. Test class

 Package Com.spring.createobject.method;  Public class Helloword {        public  Helloword () {        System.out.println ("Create object");    }          Public void Hello () {        System.out.println ("Hello word!" );    }}

 Package com.spring.createobject.method.factory; Import Com.spring.createobject.method.HelloWord;  Public class HelloWordFactory2 {        public  helloword getinstance () {                return  New  Helloword ();    }}

3. Testing

/**      * Instance Factory method      */     @Test    publicvoid  Testcreateobject_instancecfactory () {                // start Spring container        applicationcontext context=                New classpathxmlapplicationcontext ("Applicationcontext.xml");                 // extract the beans from the spring container according to the ID        Helloword Helloword = (Helloword) context.getbean ("Helloword3");        Helloword.hello ();    }

Output:

Create Object
Hello word!

How SPRINGIOC_ objects are created

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: info-contact@alibabacloud.com 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.