0
点赞
收藏
分享

微信扫一扫

Spring 6.0官方文档示例(10): idref 的用法


package cn.edu.tju.domain;

public class TheTargetBean {
public static String getString(){
return "yes,it is";
}
}

package cn.edu.tju.domain;

public class TheClientBean {
private String theTargetBean;

public String getTheTargetBean() {
return theTargetBean;
}

public void setTheTargetBean(String theTargetBean) {
this.theTargetBean = theTargetBean;
}
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">


<bean id="theTargetBean" class="cn.edu.tju.domain.TheTargetBean" factory-method="getString"/>


<bean id="theClientBean" class="cn.edu.tju.domain.TheClientBean" >
<property name="theTargetBean">
<idref bean="theTargetBean"/>
</property>
</bean>
<bean id="theClientBean2" class="cn.edu.tju.domain.TheClientBean">
<property name="theTargetBean" value="theTargetBean"/>
</bean>


</beans>

theClientBean和theClientBean2两者是等价的。
通过idref注入String 类型的其它bean的名字,能够提供错误检查机制,防止拼写错误


举报

相关推荐

0 条评论