Spring- Bean的實例化
阿新 • • 發佈:2018-06-03
3.0 構造 sage HA 不能 const xmla 指定 info
傳統應用程序可以通過反射方式進行實例化Bean,而Spring Ioc 容器則需要根據Bean定義的配置元數據使用反射機制來創建Bean。在Spring Ioc 容器中主要有以下幾種創建Bean實例的方式:
使用構造器實例化Bean
使用靜態工廠方式實例化Bean
使用實例工廠方法實例化Bean
使用空構造器實例化時,該類必須含有空參構造器,如果不存在的話在實例化過程中將會拋出異常。
樣例結構
HelloWorld接口
package com.zc.spring.chapter04.instance; public interface HelloWorld { public void sayHello(); }
接口實現類
package com.zc.spring.chapter04.instance; public class HelloWorldImpl implements HelloWorld { private String message; /** * 空構造器 */ public HelloWorldImpl() { this.message = "hello world!"; } /** * 帶參構造器 * @param message */ public HelloWorldImpl(String message) { this.message = message; } @Override public void sayHello() { System.out.println(message); } }
src下的conf/conf-instance.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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-3.0.xsd"> <!-- 使用默認構造參數 --> <bean id="helloWorldWithNoArgs" class="com.zc.spring.chapter04.instance.HelloWorldImpl" /> <!-- 使用有參構造參數 --> <bean id="helloWorldWithArgs" class="com.zc.spring.chapter04.instance.HelloWorldImpl" > <!-- 指定構造器參數 --> <constructor-arg index="0" value="Hello Args!" /> </bean> <!-- 靜態工廠方法 --> <bean id="helloWorldStaticFactory" class="com.zc.spring.chapter04.instance.HelloWorldStaticFactory" factory-method="newInstance"> <!-- 指定構造器參數 --> <constructor-arg index="0" value="Hello Static Factory!" /> </bean> <!-- 1.定義實例工廠Bean --> <bean id="helloWorldInstanceFactory" class="com.zc.spring.chapter04.instance.HelloWorldInstanceFactory"/> <!-- 2.使用實例工廠Bean創建Bean --> <bean id="helloWorldInstance" factory-bean="helloWorldInstanceFactory" factory-method="newInstance" > <constructor-arg index="0" value="Hello Instance Factory!"></constructor-arg> </bean> </beans>
靜態工廠
使用靜態工廠的方式除了指定必須的class屬性,還要指定factory-method屬性來指定實例化Bean的方法,而且使用靜態工廠方法也允許指定方法參數,Spring Ioc容器將調用此屬性指定的方法來獲取Bean。
package com.zc.spring.chapter04.instance; public class HelloWorldStaticFactory { /** * 工廠方法 * @param message * @return */ public static HelloWorld newInstance(String message) { //返回需要的Bean實例 return new HelloWorldImpl(message); } }
實例工廠
使用實例工廠方式不能指定class屬性,此時必須使用factory-bean屬性來指定工廠Bean,factory-method屬性指定實例化Bean的方法,而且使用實例化工廠方法允許指定方法參數,方式和使用構造器方式一樣。
package com.zc.spring.chapter04.instance; public class HelloWorldInstanceFactory { /** * 工廠方法 * @param message * @return */ public HelloWorld newInstance(String message) { // 返回需要的Bean實例 return new HelloWorldImpl(message); } }
Main
package com.zc.spring.chapter04.instance; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { sayHelloWithNoArgs(); sayHelloWithArgs(); sayHelloStaticFactory(); helloWorldInstanceFactory(); } //使用無參數構造器來實例化Bean public static void sayHelloWithNoArgs() { BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-instance.xml"); HelloWorld helloWorld = beanFactory.getBean("helloWorldWithNoArgs",HelloWorld.class); helloWorld.sayHello(); } //使用有參數構造器來實例化Bean public static void sayHelloWithArgs() { BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-instance.xml"); HelloWorld helloWorld = beanFactory.getBean("helloWorldWithArgs",HelloWorld.class); helloWorld.sayHello(); } //使用靜態工廠方法來實例化Bean public static void sayHelloStaticFactory() { // 1.讀取配置文件實例化一個IOC容器 BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-instance.xml"); // 2.從容器中獲取Bean,註意此處完全“面向接口編程,而不是面向實現” HelloWorld helloWorld = beanFactory.getBean("helloWorldStaticFactory",HelloWorld.class); // 3.執行業務邏輯 helloWorld.sayHello(); } // 使用實例工廠方法來實例化Bean public static void helloWorldInstanceFactory() { // 1.讀取配置文件實例化一個IOC容器 BeanFactory beanFactory = new ClassPathXmlApplicationContext("conf/conf-instance.xml"); // 2.從容器中獲取Bean,註意此處完全“面向接口編程,而不是面向實現” HelloWorld helloWorld = beanFactory.getBean("helloWorldInstance",HelloWorld.class); // 3.執行業務邏輯 helloWorld.sayHello(); } }
Spring- Bean的實例化