Spring4.0 xml顯示配置(構造器引數注入,屬性注入)
阿新 • • 發佈:2018-12-31
專案目錄:
package paraBean;
public class AnoBean {
void play(){
System.out.println("i am abbean");
}
}
package paraBean;
public class CtBean {
void play(){
System.out.println("i am ctbean");
}
}
package paraBean;
public interface Disk {
void play();
}
package paraBean; import java.util.List; public class JayDiskImpl implements Disk { //注入bean,字串,集合三種類型 private CtBean cb; private AnoBean ab; private String str; private List<String>strlist; private List<Object>blist; //以上用於構造器注入屬性 //以下用於setter屬性注入 private CtBean cbn; private String setStr; private List<String>setStrlist; private List<Object>setObjlist; public CtBean getCbn() { return cbn; } public void setCbn(CtBean cbn) { this.cbn = cbn; } public String getSetStr() { return setStr; } public void setSetStr(String setStr) { this.setStr = setStr; } public List<String> getSetStrlist() { return setStrlist; } public void setSetStrlist(List<String> setStrlist) { this.setStrlist = setStrlist; } public List<Object> getSetObjlist() { return setObjlist; } public void setSetObjlist(List<Object> setObjlist) { this.setObjlist = setObjlist; } public JayDiskImpl(CtBean cb,AnoBean ab,String str,List<String>strlist,List<Object>blist){ this.cb=cb; this.ab=ab; this.str=str; this.strlist=strlist; this.blist=blist; } public void play() { cb.play(); ab.play(); System.out.println("I am anotation "+str); for(int i=0;i<strlist.size();i++){ System.out.println(strlist.get(i)); } for(int i=0;i<blist.size();i++){ System.out.println(blist.get(i)); } System.out.println("-----------------------------"); System.out.println("-----------------------------"); cbn.play(); System.out.println(setStr); for (int i = 0; i <setStrlist.size(); i++) { System.out.println(setStrlist.get(i)); } for (int i = 0; i <setObjlist.size(); i++) { System.out.println(setObjlist.get(i)); } } }
package paraBean; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml"); ac.getBean("jd", JayDiskImpl.class).play(); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="cb" class="paraBean.CtBean"> </bean> <bean id="ab" class="paraBean.AnoBean"> </bean> <bean id="jd" class="paraBean.JayDiskImpl"> <constructor-arg ref="cb"/> <constructor-arg ref="ab"/> <constructor-arg value="注入的字串"/> <constructor-arg> <list> <value>我是注入集合1</value> <value>我是注入集合2</value> </list> </constructor-arg> <constructor-arg> <list> <ref bean="ab"/> <ref bean="cb"/> </list> </constructor-arg> <property name="cbn" ref="cb"/> <property name="setStr" value="i am 屬性字串"/> <property name="setStrlist"> <list> <value>屬性字串1</value> <value>屬性字串2</value> </list> </property> <property name="setObjlist"> <list> <ref bean="ab"/> <ref bean="cb"/> </list> </property> </bean> </beans>
執行test 控制檯輸出:
i am ctbean
i am abbean
I am anotation 注入的字串
我是注入集合1
我是注入集合2
[email protected]
[email protected]
-----------------------------
-----------------------------
i am ctbean
i am 屬性字串
屬性字串1
屬性字串2
[email protected]
[email protected]
現在xml中的配置用c 和p標籤分別來代替 construct 和property 屬性標籤:
此時xml頭部為
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
c和p標籤無法像construct和property 那樣直接嵌入集合,那麼就使用一種折中的方法,將list 轉成bean物件,然後在c和p標籤中直接引用:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="cb" class="paraBean.CtBean">
</bean>
<bean id="ab" class="paraBean.AnoBean">
</bean>
<util:list id="strlist">
<value>注入集合1</value>
<value>注入集合2</value>
</util:list>
<util:list id="blist">
<ref bean="ab"/>
<ref bean="cb"/>
</util:list>
<bean id="jd" class="paraBean.JayDiskImpl" c:cb-ref="cb"
c:ab-ref="ab" c:str="c string" c:strlist-ref="strlist"
c:blist-ref="blist" p:cbn-ref="cb" p:setStr="i am p string"
p:setStrlist-ref="strlist" p:setObjlist-ref="blist">
<!--<constructor-arg ref="cb"/>
<constructor-arg ref="ab"/>
<constructor-arg value="注入的字串"/>
<constructor-arg>
<list>
<value>我是注入集合1</value>
<value>我是注入集合2</value>
</list>
</constructor-arg>
<constructor-arg>
<list>
<ref bean="ab"/>
<ref bean="cb"/>
</list>
</constructor-arg>
<property name="cbn" ref="cb"/>
<property name="setStr" value="i am 屬性字串"/>
<property name="setStrlist">
<list>
<value>屬性字串1</value>
<value>屬性字串2</value>
</list>
</property>
<property name="setObjlist">
<list>
<ref bean="ab"/>
<ref bean="cb"/>
</list>
</property> -->
</bean>
</beans>
最後輸出:
i am ctbean
i am abbean
I am anotation c string
注入集合1
注入集合2
[email protected]
[email protected]
-----------------------------
-----------------------------
i am ctbean
i am p string
注入集合1
注入集合2
[email protected]
[email protected]