閱讀817 返回首頁    go 阿裏雲 go 技術社區[雲棲]


《Spring攻略(第2版)》——1.3 調用構造程序創建Bean

本節書摘來自異步社區《Spring攻略(第2版)》一書中的第1章,第1.3節,作者: 【美】Gary Mak , Josh Long , Daniel Rubio著,更多章節內容可以訪問雲棲社區“異步社區”公眾號查看

1.3 調用構造程序創建Bean

1.3.1 問題
你想要調用構造程序在Spring IoC容器中創建一個Bean,這是創建Bean最常見和直接的方法。這和Java中使用new操作符創建對象相同。

1.3.2 解決方案
通常,當你為一個Bean指定了class屬性,就將要求Spring IoC容器調用構造程序創建Bean實例。

1.3.3 工作原理
假定你打算開發一個在線銷售產品的購物應用程序。首先,你創建一個Product類,這個類有多個屬性,比如產品名稱和價格。因為商店中有許多類型的產品,所以你定義Product類為抽象類,用於不同產品子類的擴展。

package com.apress.springrecipes.shop;

public abstract class Product {

   private String name;
   private double price;

   public Product() {}

   public Product(String name, double price) {
     this.name = name;
     this.price = price;
   }

   // Getters and Setters
   ...

   public String toString() {
     return name + " " + price;
   }
}

然後你創建兩個產品子類:Battery和Disc。每個類都有自己的屬性。

package com.apress.springrecipes.shop;

public class Battery extends Product {

   private boolean rechargeable;

   public Battery() {
     super();
   }

   public Battery(String name, double price) {
     super(name, price);
   }
   // Getters and Setters
   ...
}

package com.apress.springrecipes.shop;

public class Disc extends Product {

   private int capacity;

   public Disc() {
     super();
   }

   public Disc(String name, double price) {
     super(name, price);
   }
   // Getters and Setters
   ...
   }

為了在Spring IoC容器中定義一些產品,創建如下Bean配置文件:

<beans xmlns="https://www.springframework.org/schema/beans"
   xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="https://www.springframework.org/schema/beans
     https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean  >
     <property name="name" value="AAA" />
     <property name="price" value="2.5" />
     <property name="rechargeable" value="true" />
   </bean>

   <bean  >
     <property name="name" value="CD-RW" />
     <property name="price" value="1.5" />
     <property name="capacity" value="700" />
   </bean>
</beans>

如果沒有指定元素,將會調用默認的不帶參數的構造程序。然後對於每個元素,Spring將通過設值方法注入值。前述的Bean配置等價於如下代碼片段:

Product aaa = new Battery();
aaa.setName("AAA");
aaa.setPrice(2.5);
aaa.setRechargeable(true);

Product cdrw = new Disc();
cdrw.setName("CD-RW");
cdrw.setPrice(1.5);
cdrw.setCapacity(700);

相反,如果有一個或者多個元素,Spring將調用匹配參數的最合適的構造程序。

<beans ...>
   <bean  >
     <constructor-arg value="AAA" />
     <constructor-arg value="2.5" />
     <property name="rechargeable" value="true" />
   </bean>

   <bean  >
     <constructor-arg value="CD-RW" />
     <constructor-arg value="1.5" />
     <property name="capacity" value="700" />
   </bean>
</beans>

因為Product類和子類在構造程序上沒有歧義,前述的Bean配置等價於下麵的代碼片段:

Product aaa = new Battery("AAA", 2.5);
aaa.setRechargeable(true);

Product cdrw = new Disc("CD-RW", 1.5);
cdrw.setCapacity(700);
你可以編寫下麵的Main類從Spring IoC容器讀取產品進行測試:

package com.apress.springrecipes.shop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

   public static void main(String[] args) throws Exception {
     ApplicationContext context =
        new ClassPathXmlApplicationContext("beans.xml");
     Product aaa = (Product) context.getBean("aaa");
     Product cdrw = (Product) context.getBean("cdrw");
     System.out.println(aaa);
     System.out.println(cdrw);
   }
}

最後更新:2017-05-31 15:01:37

  上一篇:go  結合案例深入解析orphan socket產生與消亡(一)
  下一篇:go  關於DELL EqualLogic PS6100存儲及數據恢複辦法的研究