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());
}
}
结果:
由此可得:
ref引用的是bean对象,而idref引用的只是bean对象的name也就是这里的id而不是bean实例,同时使用idref容器在部署的时候还会验证这个名称的bean是否真实存在。其实idref就跟value一样,如果我们只需要传递bean的name的话,用idref,若引用bean实例用ref