何时使用< ref bean>以及何时使用< ref local>在春天? [英] When to use <ref bean> and when to use <ref local> in Spring?

查看:114
本文介绍了何时使用< ref bean>以及何时使用< ref local>在春天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时在Spring中使用<ref bean="service" />和何时使用<ref local="service" />?

When to use <ref bean="service" /> and when to use <ref local="service" /> in Spring?

推荐答案

使用ref标签的bean属性指定目标bean是最通用的形式,它将允许创建对同一BeanFactory/ApplicationContext(无论是否在同一XML文件中)或父对象中的任何bean的引用. BeanFactory/ApplicationContext. bean属性的值可以与目标bean的id属性或目标bean的name属性中的值之一相同.

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 BeanFactory/ApplicationContext (whether or not in the same XML file), or parent BeanFactory/ApplicationContext. 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"/>

通过使用local属性指定目标bean,利用了XML解析器验证同一文件中XML id引用的能力. local属性的值必须与目标bean的id属性相同.如果在同一文件中找不到匹配的元素,则XML解析器将发出错误.因此,如果目标Bean位于同一XML文件中,则最好使用局部变量(为了尽早知道错误).

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 are early as possible) if the target bean is in the same XML file.

<ref local="someBean"/>

这是从Spring参考资料中获得的这里

This is from the Spring source reference here

这篇关于何时使用&lt; ref bean&gt;以及何时使用&lt; ref local&gt;在春天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆