Spring学习--静态工厂方法、实例工厂方法创建 Bean

通过调用静态工厂方法创建 bean:

  • 调用静态工厂方法创建 bean 是将对象创建的过程封装到静态方法中 , 当客户端需要对象时 , 只需要简单地调用静态方法 , 而不需要关心创建对象的细节。
  • 要声明通过静态方法创建的 bean , 需要在 bean 的 class 属性里面指定拥有该工厂的方法的类 , 同时在 factory-method 属性里指定工厂方法的名称。最后 , 使用 <constructor-arg> 元素为该方法传递方法参数。
 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     <bean id="car" class="com.itdoc.spring.factory.StaticFactory"
 7           factory-method="getCar">
 8         <constructor-arg value="Maserati"/>
 9     </bean>
10 
11 </beans>
 1 package com.itdoc.spring.factory;
 2 
 3 /**
 4  * http://www.cnblogs.com/goodcheap
 5  *
 6  * @author: Wáng Chéng Dá
 7  * @create: 2017-03-02 19:30
 8  */
 9 public class Car {
10 
11     private String brand;
12 
13     private double price;
14 
15     public String getBrand() {
16         return brand;
17     }
18 
19     public void setBrand(String brand) {
20         this.brand = brand;
21     }
22 
23     public double getPrice() {
24         return price;
25     }
26 
27     public void setPrice(double price) {
28         this.price = price;
29     }
30 
31     public Car(String brand, double price) {
32         this.brand = brand;
33         this.price = price;
34     }
35 
36     public Car() {
37     }
38 
39     @Override
40     public String toString() {
41         return "Car{" +
42                 "brand='" + brand + '\'' +
43                 ", price=" + price +
44                 '}';
45     }
46 }
 1 package com.itdoc.spring.factory;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 /**
 7  * 静态工厂方法
 8  * http://www.cnblogs.com/goodcheap
 9  *
10  * @author: Wáng Chéng Dá
11  * @create: 2017-03-02 19:27
12  */
13 public class StaticFactory {
14 
15     public static Map<String, Car> cars = new HashMap<String, Car>();
16 
17     static {
18         cars.put("Ferrari", new Car("Ferrari", 25000000));
19         cars.put("Maserati", new Car("Maserati", 2870000));
20     }
21 
22     public static Car getCar(String name) {
23         return cars.get(name);
24     }
25     
26 }
 1 package com.itdoc.spring.factory;
 2 
 3 import org.springframework.beans.factory.FactoryBean;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 /**
 8  * http://www.cnblogs.com/goodcheap
 9  *
10  * @author: Wáng Chéng Dá
11  * @create: 2017-03-02 19:41
12  */
13 public class Main {
14 
15     public static void main(String[] args) {
16 
17         ApplicationContext ctx = new ClassPathXmlApplicationContext("factory-beans.xml");
18         Car car = (Car) ctx.getBean("car");
19         System.out.println(car);
20 
21     }
22 }

 

控制台输出:

Car{brand='Maserati', price=2870000.0}

 

通过调用实例工厂方法创建 bean:

  • 实例工厂方法:将对象的创建过程封装到另外一个对象实例的方法里。当客户端需要请求对象时 , 只需要简单的调用该实例方法而不需要关心对象的创建细节。
  • 要声明通过实例工厂方法创建的 bean
  1. 在 bean 的factory-bean 属性里指定拥有该工厂方法的bean。
  2. 在 factory-method 属性里指定该工厂方法的名称。
  3. 使用 <constructor-arg> 元素为工厂方法传递方法参数。
 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     <bean id="carFactory" class="com.itdoc.spring.factory.InstanceFactory"></bean>
 7 
 8     <bean id="car2" factory-bean="carFactory" factory-method="getCar">
 9         <constructor-arg value="Ferrari"/>
10     </bean>
11 
12 </beans>
 1 package com.itdoc.spring.factory;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 /**
 7  * 实例工厂方法
 8  * http://www.cnblogs.com/goodcheap
 9  *
10  * @author: Wáng Chéng Dá
11  * @create: 2017-03-02 20:02
12  */
13 public class InstanceFactory {
14 
15     private Map<String, Car> cars = null;
16 
17     public InstanceFactory() {
18         cars = new HashMap<String, Car>();
19         cars.put("Ferrari", new Car("Ferrari", 25000000));
20         cars.put("Maserati", new Car("Maserati", 2870000));
21     }
22 
23     public Car getCar(String name) {
24         return cars.get(name);
25     }
26 }
 1 package com.itdoc.spring.factory;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 /**
 7  * http://www.cnblogs.com/goodcheap
 8  *
 9  * @author: Wáng Chéng Dá
10  * @create: 2017-03-02 19:41
11  */
12 public class Main {
13 
14     public static void main(String[] args) {
15 
16         ApplicationContext ctx = new ClassPathXmlApplicationContext("factory-beans.xml");
17        
18         car = (Car) ctx.getBean("car2");
19         System.out.println(car);
20 
21     }
22 }

 

控制台输出:

Car{brand='Ferrari', price=2.5E7}
posted @ 2017-03-02 20:23  Chinda  阅读(7107)  评论(1编辑  收藏  举报