欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring3之 bean idref?

程序员文章站 2022-05-23 10:19:58
...

很是奇怪idref是干什么的。只是拿另一个的id名?

com.spring305.test.beanInit.cpo.IdrefA.java

public class IdrefA {

	private String idrefAStr;

	public IdrefA(){
		System.out.println(IdrefA.class+"_"+idrefAStr);
	}
	
	public String getIdrefAStr() {
		return idrefAStr;
	}

	public void setIdrefAStr(String idrefAStr) {
		this.idrefAStr = idrefAStr;
	}

}

 

com.spring305.test.beanInit.cpo.IdrefB.java

public class IdrefB {

	private String idrefBStr;

	public IdrefB(){
		System.out.println(IdrefB.class+"_"+idrefBStr);
	}
	
	public String getIdrefBStr() {
		return idrefBStr;
	}

	public void setIdrefBStr(String idrefBStr) {
		this.idrefBStr = idrefBStr;
	}
	
	public void test(){
		System.out.println(idrefBStr);
	}
}

 

com.spring305.test.beanInit.TestIdref.java

@Test
	public void InitSingeBean() {
		ApplicationContext context =  new ClassPathXmlApplicationContext("testIdref.xml");
		IdrefB idrefb = (IdrefB) context.getBean("idrefB");
		idrefb.test();
		
		IdrefA idrefA = context.getBean(idrefb.getIdrefBStr(),IdrefA.class);
		System.out.println(idrefA.getIdrefAStr());
		
	}

src/testIdref.xml

<bean id="idrefAa" class="com.spring305.test.beanInit.cpo.IdrefA">
	<property name="idrefAStr"><value>in A class</value></property>
</bean>
<bean id="idrefB" class="com.spring305.test.beanInit.cpo.IdrefB">
	<property name="idrefBStr">
		<idref local="idrefAa"/>
		<!-- <idref bean="idrefAa"/> -->
	</property>
</bean>

 神马情况:

打印的结果:

class com.spring305.test.beanInit.cpo.IdrefA_null
class com.spring305.test.beanInit.cpo.IdrefB_null
idrefAa
in A class

 前二句是po的构造方法中的,而第三个打印中IdrefA bean的ID...然后第四句是用这个ID拿到对象打印出来的注入的值。

那么这个idref有什么作用?

Using <idref/>
elements when you specify the interceptor names prevents you from misspelling an interceptor id.

  为了不误写错interceptor ID?????疑问中。。。待解决。。。

 

相关标签: Bean XML