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}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM