引用其它Bean
一、构造器注入方式:
(1)通过” <constructor-arg>”标签的ref属性来引用其他Bean
(2)通过” <constructor-arg>”标签的子<ref>标签来引用其他Bean,使用bean属性来指定引用的Bean
二、setter注入方式:
(1)通过” <property>”标签的ref属性来引用其他Bean
(2)通过” <property>”标签的子<ref>标签来引用其他Bean,使用bean属性来指定引用的Bean
public class HelloDiBean implements HelloApi{
private HelloApi helloApi;
private HelloApi helloApi2;
public HelloDiBean(HelloApi helloApi){
this.helloApi = helloApi;
}
public void sayHello() {
helloApi.sayHello();
helloApi2.sayHello();
}
public HelloApi getHelloApi2() {
return helloApi2;
}
public void setHelloApi2(HelloApi helloApi2) {
this.helloApi2 = helloApi2;
}
}
配置注入引用其他的bean
<!-- 引用其他的bean进行注入 --> <bean id="helloBean" class="com.dilist.HelloDiBean"> <constructor-arg index="0" ref="mapBean" /> <property name="helloApi2"> <ref bean="properBean" /> </property> </bean>
其他引用bean 的高级用法:
/**
* Spring还提供了另外两种更高级的配置方式,<ref local=””/>和<ref parent=””/>:
* (1)<ref local=””/>配置方式:用于引用通过<bean id=”beanName”>方式中通过id属性指定的Bean,
* 它能利用XML解析器的验证功能在读取配置文件时来验证引用的Bean是否存在。
* 因此如果在当前配置文件中有相互引用的Bean可以采用<ref local>方式从而如果配置错误能在开发调试时就发现错误。
* (2)<ref parent=””/>配置方式:用于引用父容器中的Bean,不会引用当前容器中的Bean,
* 当然父容器中的Bean和当前容器的Bean是可以重名的,获取顺序是直接到父容器找。
*/
public class HelloHigh implements HelloApi{
private HelloApi helloApi;
private HelloApi helloApi2;
public HelloHigh(HelloApi helloApi){
this.helloApi = helloApi;
}
public void sayHello() {
helloApi.sayHello();
System.out.println("");
helloApi2.sayHello();
}
public HelloApi getHelloApi2() {
return helloApi2;
}
public void setHelloApi2(HelloApi helloApi2) {
this.helloApi2 = helloApi2;
}
}
helloworld.xml:
<!-- 注入properties类型 --> <bean id="properBean" class="com.dilist.HelloDiProperties"> <property name="properties"> <props value-type="int" merge="default"><!-- 虽然指定value-type,但是不起作用 --> <prop key="1">1sss</prop> <!-- Properties 建和值都是String类型 --> <prop key="2">2</prop> </props> </property> <property name="properties2"> <value> <!-- 分隔符可以是 “换行”、“;”、“,” 不建议该方式,优先选择第一种方式 --> 1=11 2=22;<!-- 这样的分隔符好像没用 --> 3=33, 4=44 </value> </property> </bean> <!-- Spring还提供了另外两种更高级的配置方式,<ref local=””/>和<ref parent=””/> --> <bean id="helloHigh" class="com.dilist.HelloHigh"> <constructor-arg index="0"><ref local="properBean" /></constructor-arg> <property name="helloApi2"><ref parent="properBean" /></property> </bean>
helloworldParent.xml:
<!-- 注入properties类型 --> <bean id="properBean" class="com.dilist.HelloDiProperties"> <property name="properties"> <props value-type="int" merge="default"><!-- 虽然指定value-type,但是不起作用 --> <prop key="1">2dss</prop> <!-- Properties 建和值都是String类型 --> <prop key="2">3aas</prop> </props> </property> <property name="properties2"> <value> <!-- 分隔符可以是 “换行”、“;”、“,” 不建议该方式,优先选择第一种方式 --> 1=111 2=222;<!-- 这样的分隔符好像没用 --> 3=333, 4=444 </value> </property> </bean>
调用处 利用加载父容器的方式,注入父容器中的Bean:
@Test
public void testDiBeanHigh() {
// 以classes为根目录算起
// 读取配置文件实例化一个Ioc容器
// 初始化父容器
ApplicationContext parentContext = new ClassPathXmlApplicationContext(
"helloworldParent.xml");
// 初始化当前容器
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "helloworld.xml" }, parentContext);
// 构造 + setter注入 引用其他的bean注入
HelloApi helloApi = context.getBean("helloHigh", HelloApi.class);
helloApi.sayHello();
}