構(gòu)造函數(shù)注入:
Set注入法的缺點(diǎn)是,它無(wú)法清晰的表示出哪些屬性是必須的,哪些是可選的。而構(gòu)造函數(shù)注入法的優(yōu)勢(shì)是通過構(gòu)造函數(shù)來(lái)強(qiáng)制依賴關(guān)系。
使用Set注入時(shí),我們通過<property>元素來(lái)注入屬性的值。構(gòu)造函數(shù)注入也一樣,只不過是通過<bean>元素下的<constructor-arg>元素來(lái)指定實(shí)例化這個(gè)bean的時(shí)候需要傳遞的參數(shù)。constructor-arg沒有name屬性。
解決構(gòu)造函數(shù)參數(shù)的不確定性:
有2種方法可以用來(lái)處理構(gòu)造函數(shù)的不確定性:通過序號(hào)和類型。<constructor-arg>元素有一個(gè)可選的index屬性,可以用它來(lái)指定構(gòu)造函數(shù)的順序。
<bean id="foo" class="com.springinaction.Foo">
<constructor-arg index="1">
<value>http://www.manning.com</value>
</constructor>
<constructor-arg index="0">
<value>http://www.springinaction.com</value>
</constructor>
</bean>
另一種方法是使用type屬性。可以通過type屬性確定參數(shù)的類型
<bean id="foo" class="com.springinaction.Foo">
<constructor-arg type="java.lang.String">
<value>http://www.manning.com</value>
</constructor>
<constructor-arg type="java.net.URL">
<value>http://www.springinaction.com</value>
</constructor>
</bean>
使用構(gòu)造函數(shù)注入的理由:
1、構(gòu)造函數(shù)注入強(qiáng)制使用依賴契約。就是如果沒有提供所有需要的依賴,一個(gè)Bean就無(wú)法被實(shí)例化。
2、由于Bean的依賴都通過它的構(gòu)造函數(shù)設(shè)置了,所以沒有必要再寫多余的Set方法。
3、因?yàn)橹荒芡ㄟ^構(gòu)造函數(shù)設(shè)置類的屬性,這樣你有效的保證了屬性的不可變性。
Set注入的依據(jù):
1、如果Bean有很多依賴,那么構(gòu)造函數(shù)的參數(shù)列表會(huì)很長(zhǎng)。
2、構(gòu)造函數(shù)只能通過參數(shù)的個(gè)數(shù)和類型來(lái)區(qū)分。
3、如果構(gòu)造函數(shù)的參數(shù)中有2個(gè)以上是相同類型的,那么很難確定每個(gè)參數(shù)的用途。
4、構(gòu)造函數(shù)注入不利于自身的繼承。
自動(dòng)裝配:
你可以讓Spring自動(dòng)裝配,只要設(shè)置需要自動(dòng)裝配的<bean>中的autowire屬性。
<bean id="foo" class="com.springinaction.Foo" autowire="autowire type"/>
byName-視圖在容器中尋找和需要自動(dòng)裝配的屬性名相同的Bean.
byType-視圖在容器中尋找一個(gè)與需要自動(dòng)配置的屬性類型相同的Bean.
constructor-視圖在容器中查找與需要自動(dòng)裝配的Bean的構(gòu)造參數(shù)一致的一個(gè)或多個(gè)Bean.
autodetect-首先嘗試使用congstructor來(lái)自動(dòng)裝配,然后使用byType方式。
使用Spring的特殊Bean
編寫一個(gè)后處理Bean,Spring為你提供了2次機(jī)會(huì),讓你切入到Bean的生命周期中,檢查或者修改它的配置,這叫做后處理,后處理實(shí)在Bean實(shí)例化以及裝配完成之后發(fā)生的。postProcessBeforeInitialization()方法在Bean初始化之前被調(diào)用。postProcessAfterInitialization()方法在初始化之后馬上被調(diào)用。
package com.wyq.hibernate;
import java.lang.reflect.Field;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class Fuddifier implements BeanPostProcessor {
/**
* postProcessAfterInitialization()方法循環(huán)Bean的所有屬性,尋找java.lang.String類型的屬性。對(duì)于每個(gè)String屬性,把它傳遞給
* fuddify()方法,這個(gè)方法將String將變成嘮叨用語(yǔ)。
*/
public Object postProcessAfterInitialization(Object bean, String name)
throws BeansException {
Field[] fields = bean.getClass().getDeclaredFields();
try{
for(int i=0;i<fields.length;i++){
if(fields[i].getType().equals(java.lang.String.class)){
fields[i].setAccessible(true);
String original = (String)fields[i].get(bean);
fields[i].set(bean,fuddify(original));
}
}
}catch(IllegalAccessException e){
e.printStackTrace();
}
return bean;
}
/**
* postProcessBeforeInitialization()方法沒有做任何有意義的工作,它只是簡(jiǎn)單的返回沒有修改過的Bean.
*/
public Object postProcessBeforeInitialization(Object bean, String name)
throws BeansException {
return bean;
}
private String fuddify(String orig){
if(orig==null)return orig;
return orig.replaceAll("(r|l)", "w").replaceAll("(R|L)", "W");
}
}
注冊(cè)后處理Bean:如果你的應(yīng)用系統(tǒng)運(yùn)行在Bean工廠中,你需要調(diào)用工廠的addBeanPostProcessor()方法來(lái)注冊(cè)BeanPostProcessor.
BeanPostProcessor fuddifier = new Fuddifier();
factory.addBeanPostProcessor(fuddifier);
如果你是使用應(yīng)用上下文,你只需要像注冊(cè)其他Bean那樣注冊(cè)后處理Bean.
posted on 2009-10-29 14:59
王永慶 閱讀(260)
評(píng)論(0) 編輯 收藏 所屬分類:
SPRING