value-ref, key-ref, ref local, ref bean

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"/>

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"/>

This is from the Spring source reference here

value-ref, key-ref 则是面向map应用的。

The map element will create a java.util.Map that can be injected into a constructor or property. The map element expects an entry element inside it to define key/value pairs for theMap. There are a few different ways to create the entry element. One way is to have a value element, ref element, or bean element nested inside the key element for the key or inside the entry element for the value.

Creating beans this way is very verbose, but there are shortcut attributes on the entry element. The key attribute uses the attribute as a value and the key-ref uses the attribute value as a reference. Just like the ref attribute on the constructor-arg and property elements inside a bean definition, the key-ref attribute is equivalent to a ref element's bean attribute. There isn't a shortcut for the ref element's local attribute. There are also value and value-ref attributes with the same behavior as the key and key-ref attributes, but these obviously set the value for each entry.



转载于:https://my.oschina.net/u/138995/blog/187906

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值