Three ways to implement bean instantiation
First: Create using the parameterless construction method of the class (common importance)
The first instantiation method is most commonly used, and the class is instantiated by calling the parameterless construction method to create it. The sample code is as follows:
Package Spring.com.userservice;public class UserService {public UserService () {//The method is an argument-free method}public void AddUser () { System.out.println ("Add ..... ................)
XML configuration:
<?xml version= "1.0" encoding= "UTF-8"? ><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-3.0.xsd "><bean id=" UserService "class=" Spring.com.UserService.UserService "></bean> </beans>
Unit tests:
Package Spring.com.test;import static Org.junit.assert.*;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Spring.com.userservice.userservice;public class Test {@org. junit.testpublic void Test () {ApplicationContext context= New Classpathxmlapplicationcontext ("Applicationcontext.xml"); UserService s= (UserService) Context.getbean ("UserService"); S.adduser ();}}
Results:
Method performs normally
If you manually add a method that has a parameter constructor, you will not find an parameterless construction method when you create it; Sample code:
Package Spring.com.userservice;public class UserService {public UserService (String name) {//The method is an argument method}public void AddUser () {System.out.println ("Add ... ..... ...............)
Results:
The parameterless constructor method cannot be found, so it cannot be instantiated. You must manually add an parameterless construction method
Package Spring.com.userservice;public class UserService {public UserService () {//The method is an argument-free method}public UserService (String Name) {//The method is an argument method}public void AddUser () {System.out.println ("Add ... ...... .............
Second: Create with static factory (you know)
Example code:
Create a factory class to use to create an instance of a class
Package Spring.com.userservice;public class Beanfactory {public static UserService getbeanfactory () {return new UserService ();}}
XML configuration:
<?xml version= "1.0" encoding= "UTF-8"? ><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-3.0.xsd "><bean id=" Factory "class=" Spring.com.UserService.BeanFactory "factory-method=" Getbeanfactory "></bean><!--Create a factory Create object--></beans>
Unit tests:
Package Spring.com.test;import static Org.junit.assert.*;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Spring.com.userservice.beanfactory;import Spring.com.userservice.userservice;public class Test {@ org.junit.Testpublic void Test () {ApplicationContext context=new classpathxmlapplicationcontext (" Applicationcontext.xml "); UserService s= (UserService) Context.getbean ("Factory"); S.adduser ();}}
Results:
Third: Create with instance factory (you know)
Example code:
Package Spring.com.userservice;public class Beanfactory {//Common factory method public UserService Getbeanfactory () {return new UserService ();}}
XML configuration:
<?xml version= "1.0" encoding= "UTF-8"? ><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-3.0.xsd "><bean id=" Bean "class=" spring.com.UserService.BeanFactory "></bean><bean Id= "Factory" factory-bean= "Bean" factory-method= "getbeanfactory" ></bean></beans>
Unit tests:
Package Spring.com.test;import static Org.junit.assert.*;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Spring.com.userservice.beanfactory;import Spring.com.userservice.userservice;public class Test {@ org.junit.Testpublic void Test () {ApplicationContext context=new classpathxmlapplicationcontext (" Applicationcontext.xml "); UserService s= (UserService) Context.getbean ("Factory"); S.adduser ();}}
Results:
Three ways to instantiate a Spring Bean manager bean