项目目录:
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
paraBean.AnoBean@40c06c8b
paraBean.CtBean@19163ae3
-----------------------------
-----------------------------
i am ctbean
i am 属性字符串
属性字符串1
属性字符串2
paraBean.AnoBean@40c06c8b
paraBean.CtBean@19163ae3
现在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
paraBean.AnoBean@75227d3
paraBean.CtBean@22b49166
-----------------------------
-----------------------------
i am ctbean
i am p string
注入集合1
注入集合2
paraBean.AnoBean@75227d3
paraBean.CtBean@22b49166