< bean id = "userDAOProxy"
class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
< property name = "transactionManager" >
< ref bean = "transactionManager" />
</ property >
< property name = "target" >
< ref local = "UserDAO" />
</ 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
分享到:
相关推荐
其中,`ref`用于引用同一工厂中的Bean,`local`仅限于同一XML文件,`parent`用于引用模板Bean。 - **<list>/<map>/<set>/<props>`元素**:用于配置集合类型属性,如List、Map、Set和Properties。 #### 总结 ...
14. `<ref>` 元素:用于建立Bean之间的依赖关系,`bean`属性引用BeanFactory中的Bean,`local`属性则限制在当前XML配置文件内查找Bean。 这些配置选项提供了高度的灵活性,使得Spring能够管理各种复杂的应用程序...
<ref local="sessionFactory"/> </bean> ``` 2. **服务层Bean的配置** 通常情况下,业务逻辑会封装在一个或多个服务层Bean中。例如,以下代码片段定义了一个名为`fundService`的服务层Bean,它包含了多个DAO...
3. 使用标签来配置Bean的构造函数,例如:<constructor-arg> <ref local="sessionFactory" /> 4. 使用标签来配置Bean的集合属性,例如:<list> <value>com/alonely/vo/User.hbm.xml</value> </list> 在...
Bean之间可以通过ejb-ref和ejb-local-ref进行通信。ejb-ref用于引用远程实体Bean或会话Bean,而ejb-local-ref则用于引用本地Bean。 **ejb-link** ejb-link属性允许在部署描述符中明确指定引用的Bean实例,增强了...
- 形式二:`<ref local="someBean">`,仅在同一XML文件内引用bean,且要求引用的是bean的ID。 - 形式三:`<bean parent="someBean">`,通过`parent`属性引用父容器中的bean。 4. **内部bean(inner bean)**: ...
<!-- 定时任务 方式2(集群方式)--> <bean id="timerJob" class="job.TimerJob"> </bean> <bean id="timerJobProxy" class="frameworkx.spring... <ref local="timerJobTrigger" /> </bean>
<ref local="transactionManager"/> *">PROPAGATION_REQUIRED *">PROPAGATION_REQUIRED *">PROPAGATION_REQUIRED *">PROPAGATION_REQUIRED </bean> ``` 这部分定义了一个事务代理Bean,它主要用于...
<ref local="sessionFactory"/> </bean> ``` 这里配置了一个`HibernateTransactionManager`实例,通过`sessionFactory`属性关联了一个Hibernate的`SessionFactory`。 ##### DAO层的事务代理配置 ```xml <!-- ...
<ref bean="job1"/> <value>jobMethod1 </bean> <bean id="jobDetail_2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <ref bean="job2"/> <value>...
Spring 的配置文件通常是 XML 文件,该文件定义了应用程序中的各种 Bean 和它们之间的依赖关系。下面是一个简单的 Spring 配置文件示例: ```xml <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ...
总结起来,Spring的`applicationContext.xml`配置文件通过定义bean及其相互依赖关系,实现了对整个应用的组件管理和控制流程的搭建。这种方式让开发者能够灵活地配置和管理应用的各个部分,同时降低了组件之间的耦合...
<ref local="sessionFactory"/> </bean> ``` - **说明**:这里配置了一个 Hibernate 的事务管理器 `transactionManager`,它需要一个 `SessionFactory` 的引用。 **2.2 定义 DAO 层** ```xml <!— DAO层接口...
### JBoss Seam 2.01GA REF DOC #### 引言:JBoss Seam概览与功能介绍 JBoss Seam 是一个为简化企业级 Java 应用开发而...以上是对 JBoss Seam 2.01GA REF DOC 的详细解读,希望对您理解和掌握 JBoss Seam 有所帮助。
- **byName引用**:通过Bean的名称来引用,使用`@Resource(name="beanName")`或`<ref local="beanName"/>`。 - **byType引用**:通过Bean的类型来引用,使用`@Autowired`注解(默认按类型匹配)或`<ref bean="bean...
`ref local`和`ref bean`的区别在于,`local`只在当前XML配置文件内查找bean,而`bean`则会在整个应用上下文中查找。 5. 基本数据类型和集合的注入 - Spring支持对基本数据类型、数组、集合(如List、Set、Map)等...
<property name="dataSource"><ref bean="dataSource" /> <!-- 配置映射文件 --> <value>com/alonely/vo/User.hbm.xml</value> </bean> ``` `sessionFactory`引用了之前配置的数据源,并指定Hibernate...
<ref local="scheduledTadk"/> </bean> ``` - `<bean id="MyTimer" class="com.test.MyTimer"></bean>`:定义了一个名为`MyTimer`的Bean实例。 - `<bean id="methodInvokingTask" class="org.springframework....