4.4用ref的屬性指定依賴的3種模式
(1) 用local屬性指定
如果一個Bean與被參考引用的Bean在同一個XML文件中而且被參考引用的Bean是用id來命名的,那么就可以使用ref的local屬性.這樣會使XML
解釋器更早地在XML文檔解釋時,驗證Bean的id.local屬性的值必須與被參考引用的Bean的id屬性一致
4.5 Bean自動裝配的5種模式
(1)使用byName模式
byName模式指的就是通過Bean的屬性名字進行自動裝配。在Spring的配置文檔XML中,查找一個與將要裝配的屬性同名字的Bean.
在配置文檔中,有一個id為HelloWorld的Bean被設置為通過byName自動裝配,Helloworld.java包含一個date變量,Spring就會查找一個叫
做date的Bean定義,然后用它來設置date屬性。
****HelloWorld*****
package com.gc.action;
import java.util.Date;
public class HelloWorld{
private String msg=null;
private Date date=null;
public void setMsg(String msg){
this.msg=msg;
}
public String getMsg(){
return this.msg;
}
public void setDate(Date date){
this.date=date;
}
public Date getDate(){
return this.date;
}
}
配置文檔config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="HelloWorld" class="com.gc.action.HelloWorld" autowire="byName">
<property name="msg">
<value>HelloWorld</value>
</property>
</bean>
<bean id="date" class="java.util.Date"/>
<beans>
(2)使用ByType模式
byType模式指的就是如果XML中正好有一個與類型一樣的Bean,就自動裝配這個屬性。如果有多于一個這樣的Bean,就拋出一個異常,指出可
能不能對那個Bean使用byType的自動裝配。
在配置文檔中,有一個id為HelloWorld的Bean被設置為通過byType自動裝配,Helloworld.java包含一個date變量,Spring就會查找一個類
型為date的Bean定義,然后用它來設置date屬性。
****HelloWorld*****
同上
配置文檔config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="HelloWorld" class="com.gc.action.HelloWorld" autowire="byType">
<property name="msg">
<value>HelloWorld</value>
</property>
</bean>
<bean id="date" class="java.util.Date"/>
<beans>
(3)使用constructor模式
constructor模式指的就是根據構造函數的參數進行自動裝配。在配置文檔中,有一個id為HelloWorld的Bean被設置為通過constructor自動
裝配,HelloWorld.java包含一個構造函數方法,spring就會根據構造函數的參數查找合適類型的Bean定義,然后用它來設置購置函數的參數的
值。
****HelloWorld*****
package com.gc.action;
import java.util.Date;
public class HelloWorld{
private String msg=null;
private Date date=null;
public HelloWorld(Date date)
this.date=date;
}
public void setMsg(String msg){
this.msg=msg;
}
public String getMsg(){
return this.msg;
}
public void setDate(Date date){
this.date=date;
}
public Date getDate(){
return this.date;
}
}
配置文檔config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="HelloWorld" class="com.gc.action.HelloWorld" autowire="constructor">
<property name="msg">
<value>HelloWorld</value>
</property>
</bean>
<bean id="date" class="java.util.Date"/>
<beans>
(4)autodetect模式指的就是通過對Bean檢查類的內部來選擇constructor或byType.如果先找到constructor就用constructor;如果沒有
constructor,而找到byType,就用byType.
****HelloWorld****
同上(3)
配置文件config.xml
autowire="autodetect"
(5)no模式指的就是不使用自動裝配。
****HelloWorld****
同上(3)
配置文件config.xml
autowire="no"
小結:對于大型的應用,不鼓勵使用自動裝配,因為它去除了參考依賴的透明性和清晰性。有了自動裝配后,可以減少開發人員的輸入問題,但
是卻使開發人員很難看出Bean的每個屬性都是否設定完成。
完成
4.6 Bean依賴檢查的4種模式
就像自動裝配功能一樣,依賴檢查能夠分別對每一個Bean應用或取消應用。默認的是不檢查依賴關系。使用Bean元素的dependency-check屬性
來指定Bean定義的依賴檢查共有4種模式:simple.object.all.none
借助依賴檢查來實現查看Bean的每個屬性是否都設定完成的功能
(1)使用simple模式
simple模式指的是對基本類型,字符串和集合進行依賴檢查。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="HelloWorld" class="com.gc.action.HelloWorld" autowire="autodetect" dependency-check="simple">
</bean>
<bean id="date" class="java.util.Date"/>
</beans>
(2)使用object模式
object模式指的是對依賴對象進行依賴檢查.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="HelloWorld" class="com.gc.action.HelloWorld" autowire="autodetect" dependency-check="object">
</bean>
<bean id="date" class="java.util.Date"/>
</beans>
(3)使用all模式
all模式的是對全部屬性進行依賴檢查
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="HelloWorld" class="com.gc.action.HelloWorld" autowire="autodetect" dependency-check="all">
</bean>
<bean id="date" class="java.util.Date"/>
</beans>
(4)使用none模式
none模式指的是不進行依賴檢查
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="HelloWorld" class="com.gc.action.HelloWorld" autowire="autodetect" dependency-check="none">
</bean>
<bean id="date" class="java.util.Date"/>
</beans>
小結:一般情況下,依賴檢查和自動裝配結合使用。當開發人員想看Bean的每個屬性是否都設定完成的時候,依賴檢查的作用顯得更大。當依
賴檢查和自動裝配結合使用時,依賴檢查會在自動裝配完后發生。當Bean的屬性都有默認的值,或者不需要對Bean的屬性是否都被設置到Bean
上檢查時,依賴檢查的作用就不是很大了,
4.7 集合的注入方式
(1).List
****HelloWorld****
package com.gc.action;
public class HelloWorld{
private List msg=null;
public void setMsg(List msg){
this.msg=msg;
}
public List getMsg(){
return this.msg;
}
}
配置文檔
<beans>
<bean id="HelloWorld" class=com.gc.action.HelloWorld">
<property name="msg">
<list>
<value>gf</value>
<value>gd</value>
<value>HelloWorld</value>
</list>
</property>
<beans>
(2).Set
****HelloWorld****
package com.gc.action;
public class HelloWorld{
private Set msg=null;
public void setMsg(Set msg){
this.msg=msg;
}
public Set getMsg(){
return this.msg;
}
}
配置文檔
<beans>
<bean id="HelloWorld" class=com.gc.action.HelloWorld">
<property name="msg">
<set>
<value>gf</value>
<value>gd</value>
<value>HelloWorld</value>
</set>
</property>
<beans>
(3).Map
****HelloWorld****
package com.gc.action;
public class HelloWorld{
private Map msg=null;
public void setMsg(Map msg){
this.msg=msg;
}
public Map getMsg(){
return this.msg;
}
}
配置文檔
<beans>
<bean id="HelloWorld" class=com.gc.action.HelloWorld">
<property name="msg">
<Map>
<entry key="gf">
<value>HelloWorld</value>
</entry>
<entry key="gd">
<value>HelloWorld</value>
</entry>
</Map>
</property>
<beans>
(4).Properties
****HelloWorld****
package com.gc.action;
public class HelloWorld{
private Properties msg=null;
public void setMsg(Properties msg){
this.msg=msg;
}
public Properties getMsg(){
return this.msg;
}
}
配置文檔
<beans>
<bean id="HelloWorld" class=com.gc.action.HelloWorld">
<property name="msg">
<props>
<prop key="gf">HelloWorld</prop>
<prop key="gd">HelloWorld</prop>
</props>
</property>
<beans>
小結:對于List.set.Map和Properties來說都是類似的,就是都要把要注入的信息注入到集合中去,然后在把集合注入到相關的Bean中.