Several ways that spring creates objects
- The default construct
- Static Factory
- 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