ref元素,用来将bean中指定属性的值设置为对容器中的另外一个bean的引用。该引用bean将被作用依赖注入,而且在注入之前会被初始化(如果是singleton bean则已被容器初始化)。
ref有三种形式,不同的形式将决定如何处理作用域及验证:
1、 <ref bean="someBean">,通过该标签可以引用同一容器或父容器内的任何bean(无论是否在同一XML文件中)。XML bean元素的值既可以是指定bean的id值也可以是name值。
2、<ref locale="someBean">,它可以利用xml解析器来验证所引用的bean是否存在同一文件中。locale属性值必须是目标bean的id属性值。如果在同一配置文件中没有找到引用的bean,xml解析器将抛出一个例外。如果目标bean是在同一文件内,使用locale方式就是最好的选择。
3、<ref parent="someBean">通过parent属性来引用当前容器的父容器中的bean。parent属性值既可以是目标bean的id值,也可以是name属性值。
而且目标bean必须在当前容器的父容器中。
使用parent属性的主要用途是为了用某个与父容器中的bean同名的代理来包装父容器中的一个bean。
例子:
<!--in the parent context-->
<bean id="accountService" class="com.foo.SimpleAccountService"></bean>
ref有三种形式,不同的形式将决定如何处理作用域及验证:
1、 <ref bean="someBean">,通过该标签可以引用同一容器或父容器内的任何bean(无论是否在同一XML文件中)。XML bean元素的值既可以是指定bean的id值也可以是name值。
2、<ref locale="someBean">,它可以利用xml解析器来验证所引用的bean是否存在同一文件中。locale属性值必须是目标bean的id属性值。如果在同一配置文件中没有找到引用的bean,xml解析器将抛出一个例外。如果目标bean是在同一文件内,使用locale方式就是最好的选择。
3、<ref parent="someBean">通过parent属性来引用当前容器的父容器中的bean。parent属性值既可以是目标bean的id值,也可以是name属性值。
而且目标bean必须在当前容器的父容器中。
使用parent属性的主要用途是为了用某个与父容器中的bean同名的代理来包装父容器中的一个bean。
例子:
<!--in the parent context-->
<bean id="accountService" class="com.foo.SimpleAccountService"></bean>
<!--in the child context-->
<bean id="accountService" <!--the name of this bean is the same as the name of the 'parent' bean-->
class="org.springframework.ProxyFactoryBean">
<property name="target">
<ref parent="accountService"/>
</property>
</bean>