ref與idref的區別
阿新 • • 發佈:2018-11-20
package com.zking.spring01_1.pojo; /** * * @ClassName: Person * @Description: Person物件實體類 * @author 夏 * @date 2018年9月17日 下午6:47:50 * */ public class Person { private String pid; private String pname; private Card card; public Card getCard() { return card; } public void setCard(Card card) { this.card = card; } public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Person(String pid, String pname) { super(); this.pid = pid; this.pname = pname; } public Person() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Person [pid=" + pid + ", pname=" + pname + "]"; } } ------------------------------------------------------------------------------- package com.zking.spring01_1.pojo; /** * * @ClassName: Card * @Description: Card物件實體類 * @author 夏 * @date 2018年9月17日 下午8:29:13 * */ public class Card { private String cid; private String cname; public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public Card(String cid, String cname) { super(); this.cid = cid; this.cname = cname; } public Card() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Card [cid=" + cid + ", cname=" + cname + "]"; } }
bean.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" xmlns:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <!-- ref --> <bean id="personref" class="com.zking.spring01_1.pojo.Person" scope="prototype"> <property name="card"> <ref bean="card01"/> <!-- 引用bean物件card01--> </property> </bean> <!-- idref --> <bean id="personidref" class="com.zking.spring01_1.pojo.Person" scope="prototype"> <property name="pname"> <idref bean="card01"/> <!-- 引用bean物件card01 --> </property> </bean> <!-- 建立一個bean --> <bean id="card01" class="com.zking.spring01_1.pojo.Card"> <property name="cid" value="cid001"></property> <property name="cname" value="cname001"></property> </bean> </beans>
測試類:
package com.zking.spring01_1.action; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zking.spring01_1.pojo.Person; /** * * @ClassName: PersonAction * @Description: 測試類 * @author 夏 * @date 2018年9月17日 下午6:48:05 * */ public class PersonAction { @Test public void createBean() { //靜態方法注入bean ApplicationContext act=new ClassPathXmlApplicationContext("bean.xml"); //ref引用bean物件 Person personref=(Person) act.getBean("personref"); System.out.println("ref:"+personref.getCard()); //idref 引用 Person personidref=(Person) act.getBean("personidref"); System.out.println("idref:"+personidref.getPname()); } }
結果: