spring中idref元素元素使用
2009-12-02 15:37
162 查看
idref元素用来将容器内其它bean的id传给
<constructor-arg/>或
<property/>元素,同时提供错误验证功能。
<bean id="theTargetBean" class="..."/> <bean id="theClientBean" class="..."> <property name="targetName"> <idref bean="theTargetBean" /> </property> </bean>
上述bean定义片段完全地等同于(在运行时)以下的片段:
<bean id="theTargetBean" class="..." /> <bean id="client" class="..."> <property name="targetName" value="theTargetBean" /> </bean>
第一种形式比第二种更可取的主要原因是,使用
idref标记允许容器在部署时 验证所被引用的bean是否存在。而第二种方式中,传给
clientbean的
targetName属性值并没有被验证。任何的输入错误仅在
clientbean实际实例化时才会被发现(可能伴随着致命的错误)。如果
clientbean 是prototype类型的bean,则此输入错误(及由此导致的异常)可能在容器部署很久以后才会被发现。
此外,如果被引用的bean在同一XML文件内,且bean名字就是bean id,那么可以使用
local属性,此属性允许XML解析器在解析XML文件时对引用的bean进行验证。
<property name="targetName"> <!-- a bean with an id of '[code]theTargetBean' must exist; otherwise an XML exception will be thrown -->
<idref local="theTargetBean"/>
</property>[/code]
上面的例子中,与在
ProxyFactoryBeanbean定义中使用<idref/>元素指定AOP interceptor的相同之处在于:如果使用<idref/>元素指定拦截器名字,可以避免因一时疏忽导致的拦截器ID拼写错误。
在
<constructor-arg/>或
<property/>元素内部还可以使用
ref元素。该元素用来将bean中指定属性的值设置为对容器中的另外一个bean的引用。如前所述,该引用bean将被作为依赖注入,而且在注入之前会被初始化(如果是singleton bean则已被容器初始化)。尽管都是对另外一个对象的引用,但是通过id/name指向另外一个对象却有三种不同的形式,不同的形式将决定如何处理作用域及验证。
第一种形式也是最常见的形式是通过使用
<ref/>标记指定
bean属性的目标bean,通过该标签可以引用同一容器或父容器内的任何bean(无论是否在同一XML文件中)。XML '
bean'元素的值既可以是指定bean的
id值也可以是其
name值。
<ref bean="someBean"/>
第二种形式是使用ref的
local属性指定目标bean,它可以利用XML解析器来验证所引用的bean是否存在同一文件中。
local属性值必须是目标bean的id属性值。如果在同一配置文件中没有找到引用的bean,XML解析器将抛出一个例外。如果目标bean是在同一文件内,使用local方式就是最好的选择(为了尽早地发现错误)。
<ref local="someBean"/>
第三种方式是通过使用ref的
parent属性来引用当前容器的父容器中的bean。
parent属性值既可以是目标bean的
id值,也可以是
name属性值。而且目标bean必须在当前容器的父容器中。使用parent属性的主要用途是为了用某个与父容器中的bean同名的代理来包装父容器中的一个bean(例如,子上下文中的一个bean定义覆盖了他的父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 [code]'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>[/code]
相关文章推荐
- Spring中 idref元素的使用
- SPRING.NET 1.3.2 学习6--idref元素
- 【Spring学习06】依赖配置:元素idref
- spring idref和ref的区别
- Spring bean中的properties元素内的name 和 ref都代表什么意思啊?
- Spring中的ref标签的使用
- 2_5 IDREF和IDREFS类型的属性的使用及作用
- Spring Bean中idref和ref区别
- js方法的命名不能使用表单元素的名称或ID
- spring中的idref和ref的区别
- Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别
- spring bean属性property、ref使用方式(转载)
- 在脚本中可以直接使用id名称调用元素
- 使用jQuery时Form表单元素ID和name命名大忌
- 在jQuery中动态创建id,如何使用该Id来访问这个元素
- springboot 使用thymeleaf 模板引擎时报错org.xml.sax.SAXParseException: 元素类型 "link" 必须由匹配的结束标记 "</link>" 终止
- 在spring的beans.xml文件中使用id和name的区别
- Spring的ID增长器使用示例(MySql数据库)
- Spring的ID增长器使用示例(MySql数据库)
- Spring中Bean的命名问题及ref和idref之间的区别