简体   繁体   English

Spring bean idref的用例是什么?

[英]What can be the use case for Spring bean idref?

I know that idref is used to pass the id of a bean (ie String value but not the reference). 我知道idref用于传递bean的ID(即字符串值,但不传递引用)。 I was asked in an interview, what can be the use case of idref ? 在一次采访中有人问我, idref的用例是idref Why and when it should be used? 为什么以及何时应使用?

idref just adds a layer of security by validating at deployment time that the referenced, named bean actually exists. idref只是通过在部署时验证所引用的命名bean实际存在而增加了一层安全性。

A very nice example with appropriate explanation is provided in this link 此链接中提供了一个很好的示例,带有适当的说明

Example

<bean id="abc" class="..."/>

<bean id="xyz" class="...">
  <property name="abcName">
      <idref bean="abc" />
  </property>
</bean>

can also be written as 也可以写成

<bean id="abc" class="..." />

<bean id="xyz" class="...">
  <property name="abcName" value="abc" />
</bean>

The advantage of using first way is already described. 已经描述了使用第一种方法的优点。

If you use the second method then no validation is performed on the value that is passed to the abcName property of the xyz bean ie during the loading of the context. 如果使用第二种方法,则不会对传递给xyz bean的abcName属性的值执行验证,即在加载上下文期间。

Errors are only discovered when the xyz bean is instantiated. 仅在实例化xyz bean时发现错误。 So, we can never know if any typos have been injected by the developer during the instantiation phase. 因此,我们永远无法知道开发人员在实例化阶段是否输入了任何错别字。

Note : It is just a recommended approach, not a mandatory approach 注意: 这只是推荐方法,不是强制方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM