spring factory-method和factory-bean 使用__ssh

來源:互聯網
上載者:User
配置工廠Bean 通常由應用程式直接使用new建立新的對象,為了將對象的建立和使用相分離,採用原廠模式,即應用程式將對象的建立及初始化職責交給工廠對象. 一般情況下,應用程式有自己的工廠對象來建立bean.如果將應用程式自己的工廠對象交給Spring管理,那麼Spring管理的就不是普通的bean,而是工廠Bean. 調用getBean()方法,Spring返回的不是直接建立的Bean的執行個體,而是由工廠Bean建立的Bean執行個體. 一般在Spring中配置工廠Bean,有3中不同類型的工廠Bean的配置. 1.靜態工廠 建立具體Bean執行個體的是靜態方法
import java.util.Random; public class StaticFactoryBean { public static Integer createRandom() { return new Integer(new Random().nextInt());
}
} 將其納入Spring容器來管理,需要通過factory-method指定靜態方法名稱 <bean id="random"
class="example.chapter3.StaticFactoryBean"
factory-method="createRandom" //createRandom方法必須是static的,才能找到
scope="prototype"
/> 測試: public static void main(String[] args) {

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
System.out.println(factory.getBean("random").toString()); //StaticFactoryBean sfb = (StaticFactoryBean)factory.getBean("random");
//System.out.println(sfb.createRandom().toString()); //調用getBean()時,返回隨機數.如果沒有指定 factory-method,會返回StaticFactoryBean的執行個體,即返回工廠Bean的執行個體
}
2.執行個體工廠 建立具體Bean執行個體的是執行個體,不是靜態方法
import java.text.SimpleDateFormat;
import java.util.Date; public class InstanceFactoryBean { private String format = "yy-MM-dd HH:mm:ss"; public void setFormat(String format) {
this.format = format;
} public String createTime() {
return new SimpleDateFormat(format).format(new Date());
}
} 設定檔需要配置兩個bean:第一個Bean和普通的Bean沒有區別,第二個bean定義如何通過工廠Bean擷取Bean,需要指定 工廠Bean的名稱和方法名稱 <bean id="instanceFactoryBean" class="example.chapter3.InstanceFactoryBean">
<property name="format" value="yyyy-MM-dd HH:mm:ss" />
</bean> <bean id="currentTime"
factory-bean="instanceFactoryBean"
factory-method="createTime"
/> 測試: public static void main(String[] args) {

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
System.out.println(factory.getBean("currentTime"));

} 3.實現FactoryBean介面 public class PiFactoryBean implements FactoryBean { public Object getObject() throws Exception {
return new Double(3.14159265358979);
} public Class getObjectType() {
return Double.class;
} public boolean isSingleton() {
return true;
} } 實現了FactoryBean介面的Bean,不再被視為普通的Bean.Spring會自動檢測. <bean id="pi" class="example.chapter3.PiFactoryBean" /> 測試 public static void main(String[] args) throws Exception {

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("config.xml"));
System.out.println(factory.getBean("pi"));//返回PiFactoryBean 的Factory 方法getObject返回的Double對象執行個體
//PiFactoryBean p = (PiFactoryBean)factory.getBean("&pi"); //加"&"返回工廠Bean的執行個體.
//System.out.println(p.getObject());
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.