How to configure a bean
Through the factory method (static factory method & Instance Factory method), Factorybean
To create a bean by calling the static factory method
Calling the static factory method to create the bean is the process of encapsulating the object creation into a static method, and when the client needs the object, simply call the static method without worrying about the details of the object being created.
To declare a bean created through a static method, you need to specify the method class owning the factory in the Bean's class attribute, specifying the name of the factory method in the Factory-method attribute, and finally, using the <constructor-arg> element to pass method parameters for the method.
Look at the code
Packagelogan.spring.study.factory; Public classCar {PrivateString brand; Private intPrice ; PublicString Getbrand () {returnbrand; } Public voidSetbrand (String brand) { This. Brand =brand; } Public intGetPrice () {returnPrice ; } Public voidSetprice (intPrice ) { This. Price =Price ; } @Override PublicString toString () {return"Car [brand=" + Brand + ", price=" + Price + "]"; } PublicCar (String brand,intPrice ) { Super(); This. Brand =brand; This. Price =Price ; }}
Packagelogan.spring.study.factory;ImportJava.util.HashMap;ImportJava.util.Map; Public classStaticcarfactory {Private StaticMap<string, car> cars =NewHashmap<string, car>(); Static{cars.put ("Audi",NewCar ("Audi", 3000000)); Cars.put ("Ford",NewCar ("Ford", 4000000)); } Public StaticCar Getcar (String name) {returncars.get (name); }}
<?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.xsd" > <!--Configure the Bean with a static factory method, note that instead of configuring a static factory method instance, configure the Bean instance - <BeanID= "Car1"class= "Logan.spring.study.factory.StaticCarFactory"Factory-method= "Getcar"> <Constructor-argvalue= "Audi"></Constructor-arg> </Bean></Beans>
Packagelogan.spring.study.factory;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMain { Public Static voidMain (string[] args) {//TODO auto-generated Method StubApplicationContext CTX=NewClasspathxmlapplicationcontext ("Beans-factory.xml"); Car car1= (Car) ctx.getbean ("Car1"); System.out.println (CAR1); }}
The following is the output result:
May 21, 2017 11:21:42 am org.springframework.context.support.ClassPathXmlApplicationContext Preparerefresh Info: refreshing org[email protected]7aec35a:startup Date [Sun May 11:21:42 CST 2017]; Root of the context hierarchy May 21, 2017 11:21:42 am Org.springframework.beans.factory.xml.XmlBeanDefinitionReader Loadbeandefinitions info: Loading XML Bean Definitions from class path resource [Beans-factory.xml]car [Brand=audi], price=30 00000]
See the example factory method below:
The instance factory method is the method of the instance factory,
Packagelogan.spring.study.factory;ImportJava.util.HashMap;ImportJava.util.Map; Public classInstancecarfactory {PrivateMap<string, car> cars =NULL; Publicinstancecarfactory () {Cars=NewHashmap<string,car>(); Cars.put ("Audi",NewCar ("Audi", 300000)); Cars.put ("Ford",NewCar ("Ford", 500000)); } PublicCar Getcar (String brand) {returnCars.get (brand); }}
Here is the 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"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <!--Configure the Bean with a static factory method, note that instead of configuring a static factory method instance, configure the Bean instance - <BeanID= "Car1"class= "Logan.spring.study.factory.StaticCarFactory"Factory-method= "Getcar"> <Constructor-argvalue= "Audi"></Constructor-arg> </Bean> <!--Configure an instance of the factory - <BeanID= "Carfactory"class= "Logan.spring.study.factory.InstanceCarFactory"></Bean> <!--to configure a bean through an instance factory method - <BeanID= "Car2"Factory-bean= "Carfactory"Factory-method= "Getcar"> <Constructor-argvalue= "Ford"></Constructor-arg> </Bean></Beans>
Packagelogan.spring.study.factory;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMain { Public Static voidMain (string[] args) {//TODO auto-generated Method StubApplicationContext CTX=NewClasspathxmlapplicationcontext ("Beans-factory.xml"); Car car1= (Car) ctx.getbean ("Car1"); System.out.println (CAR1); Car car2= (Car) ctx.getbean ("Car2"); System.out.println (CAR2); }}
Here are the output results
May 7:27:1819:27:18 CST, 7:27:18Class path resource [beans-
factory.xml]car [Brand=audi, price=3000000
]car [Brand=ford, price=500000]
Spring Introductory Lesson 12th