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. html

 <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. java

 <ref local="someBean"/>

This is from the Spring source reference here spring

value-ref, key-ref 則是面向map應用的。 ide

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. ui

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. this

相關文章
相關標籤/搜索