Spring bean configuration method (Factory method and Factorybean), beanfactorybean
Configure bean through factory Method
Create a bean by calling the static factory Method
Creating a bean using the static factory method encapsulates the object creation process into the static method. When the client needs an object, it simply needs to call the static method, regardless of the details of the object creation.
To declare a bean created by using a static method, you must specify the class of the method that owns the factory in the class attribute of the bean, and notify you to specify the name of the factory method in the factory-method attribute,
Finally, use the <constructor-arg> element to pass the method parameters for this method.
Create bean by calling the instance factory Method
Instance factory method: encapsulate the object creation process in another object instance method. When the client needs to request an object, it simply needs to call the instance method without worrying about the creation details of the object.
To declare the bean created using the instance factory method:
-Specify the bean that owns the factory method in the factory-bean attribute of bean.
-Specify the name of the factory method in the factory-method attribute.
-Use the constructor-arg element to pass the method parameters for the factory Method
Static factory class:
1 package com. yl. factory; 2 3 import java. util. hashMap; 4 import java. util. map; 5 6/** 7 * Static factory method: you can directly call the static method of a class to access the instance 8 * @ author yul 9*10 */11 public class StaticCarFactory {12 13 private static Map <String, car> cars = new HashMap <String, Car> (); 14 15 static {16 cars. put ("audi", new Car ("audi", 300000); 17 cars. put ("ford", new Car ("ford", 300000 )); 18} 19/** 20 * static factory Method 21 * @ param name22 * @ return23 */24 public static Car getCar (String name) {25 26 return cars. get (name); 27} 28}
Instance factory type:
1 package com. yl. factory; 2 3 import java. util. hashMap; 4 import java. util. map; 5/*** instance factory method: the instance factory method, that is, to create the factory itself, return bean instance 7 * @ author yul 8*9 */10 public class InstanceCarFactory {11 12 private Map <String, car> cars = new HashMap <String, Car> (); 13 14 public InstanceCarFactory () {15 cars = new HashMap <String, Car> (); 16 cars. put ("audi", new Car ("audi", 300000); 17 cars. put ("ford", new Car ("ford", 400000); 18} 19 20 public Car getCar (String brand) {21 return cars. get (brand); 22} 23}
Configuration file of the factory method:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <! -- Configure the bean through the static factory method. Note that the bean instance is configured instead of the static factory Method Instance. --> 7 <! -- 8 class attribute: indicates the full class Name of the static factory method 9 factory-method: refers to the static factory method name 10 constructor-arg: If the static factory method needs to input parameters, use constructor-arg to configure the parameter 11 --> 12 <bean id = "car1" 13 class = "com. yl. factory. staticCarFactory "14 factory-method =" getCar "> 15 <constructor-arg value =" audi "> </constructor-arg> 16 </bean> 17 18 <! -- Configure the factory instance --> 19 <bean id = "carFactory" class = "com. yl. factory. InstanceCarFactory"> </bean> 20 21 <! -- 22 factory-bean: bean23 factory-method pointing to the instance factory method: pointing to the instance factory method name 24 constructor-arg: If the instance factory method needs to input parameters, use constructor-arg to configure 25 --> 26 <! -- Configure bean through the instance factory method --> 27 <bean id = "car2" factory-bean = "carFactory" factory-method = "getCar"> 28 <constructor-arg value = "ford"> </constructor-arg> 29 </bean> 30 </beans>
Configure bean through Factorybean
Inherit the implementation class of Factorybean:
1 package com. yl. factorybean; 2 3 import org. springframework. beans. factory. factoryBean; 4 // The Custom Factorybean needs to implement the FactoryBean interface 5 public class CarFactoryBean implements FactoryBean <Car> {6 7 private String brand; 8 9 public void setBrand (String brand) {10 this. brand = brand; 11} 12 13/** 14 * return the bean object 15 */16 @ Override17 public Car getObject () throws Exception {18 // TODO Auto-generated method stub19 Return new Car ("BMW", 600000); 20} 21/** 22 * return bean type 23 */24 @ Override25 public Class <?> GetObjectType () {26 // TODO Auto-generated method stub27 return Car. class; 28} 29 30 @ Override31 public boolean isSingleton () {32 // TODO Auto-generated method stub33 return true; 34} 35 36}
Configuration file:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 <! -- 6 configure the bean instance through Factorybean 7 class: point to the full class name of Factorybean 8 property: configure the attribute 9 of Factorybean 10 but the actually returned instance is the getObject () of Factorybean () 11 --> 12 <bean id = "car" class = "com. yl. factorybean. carFactoryBean "> 13 <property name =" brand "value =" BMW "> </property> 14 </bean> 15 </beans>