< bean id = "userDAOProxy"
class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
< property name = "transactionManager" >
[color=red]< ref bean = "transactionManager" />[/color]
</ property >
< property name = "target" >
[color=red]< ref local = "UserDAO" />[/color]
</ property >
</ bean >
1 、用 local 属性指定目标 bean 可以利用 xml 解析器的能力在同一个文件中验证 xml id 引用(即是否存在). 如果在同一个文件中没有匹配的元素 ,xml 解析器就会产生一个 error, 所以如果目标 bean 在同一个xml 文件中,那么用 local 形式是最好的选择.
2 、可以这么说,<ref bean 是寻找全局中的 bean; <ref local 是寻找本 xml 文件中的 bean
Spring官方文档的解释:
3.3.3.2. References to other beans (collaborators)
The ref element is the final element allowed inside a <constructor-arg/> or <property/> definition element. It is used to set the value of the specified property to be a reference to another bean managed by the container (a collaborator). As mentioned in a previous section, the referred-to bean is considered to be a dependency of the bean who's property is being set, and will be initialized on demand as needed (if it is a singleton bean it may have already been initialized by the container) before the property is set. All references are ultimately just a reference to another object, but there are 3 variations on how the id/name of the other object may be specified, which determines how scoping and validation is handled.
Specifying the target bean by using the bean attribute of the <ref/> tag is the most general form, and will allow creating a reference to any bean in the same container (whether or not in the same XML file), or parent container. The value of the 'bean' attribute may be the same as either the 'id' attribute of the target bean, or one of the values in the 'name' attribute of the target bean.
<ref bean="someBean"/>
Specifying the target bean by using the local attribute leverages the ability of the XML parser to validate XML id references within the same file. The value of the local attribute must be the same as the id attribute of the target bean. The XML parser will issue an error if no matching element is found in the same file. As such, using the local variant is the best choice (in order to know about errors as early as possible) if the target bean is in the same XML file.
<ref local="someBean"/>
Specifying the target bean by using the 'parent' attribute allows a reference to be created to a bean which is in a parent container of the current container. The value of the 'parent' attribute may be the same as either the 'id' attribute of the target bean, or one of the values in the 'name' attribute of the target bean, and the target bean must be in a parent container to the current one. The main use of this bean reference variant is when you have a hierarchy of containers and you want to wrap an existing bean in a parent container with some sort of proxy which will have the same name as the parent bean.
<!-- in the parent context -->
<bean id="accountService" class="com.foo.SimpleAccountService">
<!-- insert dependencies as required as here -->
</bean>
<!-- in the child (descendant) context -->
<bean id="accountService" <-- notice that the name of this bean is the same as the name of the 'parent' bean
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref parent="accountService"/> <-- notice how we refer to the parent bean
</property>
<!-- insert other configuration and dependencies as required as here -->
</bean>
原文参照:
http://static.springframework.org/spring/docs/2.0.x/reference/beans.html#beans-ref-element
class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
< property name = "transactionManager" >
[color=red]< ref bean = "transactionManager" />[/color]
</ property >
< property name = "target" >
[color=red]< ref local = "UserDAO" />[/color]
</ property >
</ bean >
1 、用 local 属性指定目标 bean 可以利用 xml 解析器的能力在同一个文件中验证 xml id 引用(即是否存在). 如果在同一个文件中没有匹配的元素 ,xml 解析器就会产生一个 error, 所以如果目标 bean 在同一个xml 文件中,那么用 local 形式是最好的选择.
2 、可以这么说,<ref bean 是寻找全局中的 bean; <ref local 是寻找本 xml 文件中的 bean
Spring官方文档的解释:
3.3.3.2. References to other beans (collaborators)
The ref element is the final element allowed inside a <constructor-arg/> or <property/> definition element. It is used to set the value of the specified property to be a reference to another bean managed by the container (a collaborator). As mentioned in a previous section, the referred-to bean is considered to be a dependency of the bean who's property is being set, and will be initialized on demand as needed (if it is a singleton bean it may have already been initialized by the container) before the property is set. All references are ultimately just a reference to another object, but there are 3 variations on how the id/name of the other object may be specified, which determines how scoping and validation is handled.
Specifying the target bean by using the bean attribute of the <ref/> tag is the most general form, and will allow creating a reference to any bean in the same container (whether or not in the same XML file), or parent container. The value of the 'bean' attribute may be the same as either the 'id' attribute of the target bean, or one of the values in the 'name' attribute of the target bean.
<ref bean="someBean"/>
Specifying the target bean by using the local attribute leverages the ability of the XML parser to validate XML id references within the same file. The value of the local attribute must be the same as the id attribute of the target bean. The XML parser will issue an error if no matching element is found in the same file. As such, using the local variant is the best choice (in order to know about errors as early as possible) if the target bean is in the same XML file.
<ref local="someBean"/>
Specifying the target bean by using the 'parent' attribute allows a reference to be created to a bean which is in a parent container of the current container. The value of the 'parent' attribute may be the same as either the 'id' attribute of the target bean, or one of the values in the 'name' attribute of the target bean, and the target bean must be in a parent container to the current one. The main use of this bean reference variant is when you have a hierarchy of containers and you want to wrap an existing bean in a parent container with some sort of proxy which will have the same name as the parent bean.
<!-- in the parent context -->
<bean id="accountService" class="com.foo.SimpleAccountService">
<!-- insert dependencies as required as here -->
</bean>
<!-- in the child (descendant) context -->
<bean id="accountService" <-- notice that the name of this bean is the same as the name of the 'parent' bean
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref parent="accountService"/> <-- notice how we refer to the parent bean
</property>
<!-- insert other configuration and dependencies as required as here -->
</bean>
原文参照:
http://static.springframework.org/spring/docs/2.0.x/reference/beans.html#beans-ref-element