首先先直白的解释,再详细介绍
区别:
ref:注入的是bean的实例
idref:注入的是string。
测试案例:
package com.pojo;
public class Card {
private String cno;
private String cpwd;
public String getCno() {
return cno;
}
public void setCno(String cno) {
this.cno = cno;
}
public String getCpwd() {
return cpwd;
}
public void setCpwd(String cpwd) {
this.cpwd = cpwd;
}
public Card(String cno, String cpwd) {
super();
this.cno = cno;
this.cpwd = cpwd;
}
public Card() {
super();
// TODO Auto-generated constructor stub
}
}
第二个实体
package com.pojo;
import java.util.List;
import java.util.Set;
public class Person {
private String name;
private String address;
private String pid;
private List<String> lStrings;
private Set<Card> sCards;
private Card card;
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
public Set<Card> getsCards() {
return sCards;
}
public void setsCards(Set<Card> sCards) {
this.sCards = sCards;
}
public List<String> getlStrings() {
return lStrings;
}
public void setlStrings(List<String> lStrings) {
this.lStrings = lStrings;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
@Override
public String toString() {
return "Person [name=" + name + ", address=" + address + ", pid=" + pid + "]";
}
public void initPerson() {
System.out.println("初始化的方法");
}
}
配置文件:
<?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-4.3.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">
<!-- 标签的解释: xmlns: XML Name space xmlns: 关于初始化bean的格式文件地址 xmlns:xsi 辅助初始化bean
xmlns:aop aop (面向切面) 标签的 约束 xsi:schemaLocation 用于声明目标的名称空间的模式文档 xmlns:context
关于spring上下文,包括加载资源文件 -->
<!-- 产生一个springbean 因为我所有的产生的bean的对象全部都是写在xml配置文件中 那么就以为着 我需要加载这一个配置文件信息applicationContext
.xml 加载xml配置文件信息,那就意味着解析xml ,解析xml 就意味着使用dom4j 解析 class属性值的时候 根据class的值进行反射
class.fromName(class); IOC 无非就是使用dom4j +反射 hibernate(缓存机制 ) struts2(拦截器 >解析struts.xml文件并且
反射 web.xml) -->
<!-- scope 不写 默认单例 bean-> person person1=new person() -->
<bean id="person1" class="com.pojo.Person" scope="prototype" init-method="initPerson">
<property name="sCards">
<set>
<ref bean="card" />
</set>
</property>
<property name="card" ref="card"></property>
<!--idref 验证数据有效性 -->
<property name="card" >
<idref bean="c"/>
</property>
</bean>
<bean id="card" class="com.pojo.Card">
<property name="cno" value="123"></property>
<property name="cpwd" value="123"></property>
</bean>
</beans>
最后是temp:
用的Junit
package com.temp;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import com.zking.pojo.Card;
import com.zking.pojo.Person;
public class SpringTemp {
@Test
public void createBean() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=(Person) applicationContext.getBean("person1");
System.out.println(person.getCard().getCno()+".........."+person.getCard().getCpwd());
}
}
测试结果:
<property name="card">中设置了<idref bean="c"/> 而card这个bean在当前的容器中并不存在所以出错。
将<idref bean="card"/> 改为<idref bean="card"/>顺利运行.