In addition to creating a bean by configuring a full-class name using reflection, you can also use the factory method to create the bean. This will be used when the framework is integrated.
1. Static Factory method
Calling the static factory method to create the bean is the process of encapsulating the object creation into a static method . When a client needs an object, it simply calls 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 class of the method that owns the factory in the Bean's class attribute, and specify the name of the factory method in the Factory-method attribute. Finally, use the <constrctor-arg> element to pass method parameters for the method.
1.1 Creating a Bean:
Packagespring.demo_01.factory; Public classCar {PrivateString brand; Private DoublePrice ; PublicString Getbrand () {returnbrand; } Public voidSetbrand (String brand) { This. Brand =brand; } Public DoubleGetPrice () {returnPrice ; } Public voidSetprice (DoublePrice ) { This. Price =Price ; } PublicCar (String brand,DoublePrice ) { Super(); This. Brand =brand; This. Price =Price ; } @Override PublicString toString () {return"Car [brand=" + Brand + ", price=" + Price + "]"; }}
View Code
1.2 Creating a static factory class:
Packagespring.demo_01.factory;ImportJava.util.HashMap;ImportJava.util.Map;/*** Static Factory method*/ Public classStaticcarfactory {Private StaticMap<string,car> cars =NewHashmap<string,car>(); Static{cars.put ("Ford",NewCar ("Ford", 120000)); Cars.put ("Dazhong",NewCar ("Dazhong", 100000)); } Public StaticCar Getcar (String name) {returncars.get (name); }}
1.3 Configuration Bean:
<?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" > <!--Configuring The Bean with a static factory method requires that you configure the instance of the static factory class instead of the instance of the bean. - <!--Class : A full class name pointing to a static factory Factory-method: Static Factory method for performing static classes Constructor-arg: Configure the parameters that the static factory method needs to be incorporated into - <BeanID= "Car1"class= "Spring.demo_01.factory." Staticcarfactory "Factory-method= "Getcar"> <Constructor-argvalue= "Ford"></Constructor-arg> </Bean> <BeanID= "Car2"class= "Spring.demo_01.factory." Staticcarfactory "Factory-method= "Getcar"> <Constructor-argvalue= "Dazhong"></Constructor-arg> </Bean></Beans>
1.4 Test class:
Packagespring.demo_01.factory;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classApp { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Beans-factory.xml"); Car car1= Ctx.getbean ("Car1", Car.class); System.out.println (CAR1); }}
View Code
2. Instance Factory method
The instance factory method encapsulates the creation of an object into a method of another object instance . When a client needs to request an object, it simply calls the instance method without needing to care about the object's creation details.
To declare a Bean created through an instance factory method:
- Specify the bean that owns the factory method in the Bean's Factory-bean attribute
- Specify the name of the factory method in the Factory-method attribute
- Passing method parameters for factory methods using the Construtor-arg element
Note: You need to create an instance of the instance factory class before you can create the bean.
2.1 Instance factory class:
Packagespring.demo_01.factory;ImportJava.util.HashMap;ImportJava.util.Map;/*** Instance Factory method, before you get the bean, you need to create an instance of the factory first.*/ Public classInstancecarfactory {PrivateMap<string,car>cars; Publicinstancecarfactory () {Cars=NewHashmap<string,car>(); Cars.put ("Ford",NewCar ("Ford", 120000)); Cars.put ("BMW",NewCar ("BMW", 320000)); } PublicCar Getcar (String name) {returncars.get (name); }}
2.2 Bean Configuration:
<!--Configure a factory instance - <BeanID= "Instancecarfactory"class= "Spring.demo_01.factory." Instancecarfactory "></Bean> <!--Configuring beans through factory instances - <BeanID= "Car3"Factory-bean= "Instancecarfactory"Factory-method= "Getcar"> <Constructor-argvalue= "BMW"></Constructor-arg> </Bean>
2.3 Test class:
Packagespring.demo_01.factory;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classApp { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Beans-factory.xml"); Car car1= Ctx.getbean ("Car3", Car.class); System.out.println (CAR1); }}
Spring4-2-bean Configuring the Bean by factory method