Continued from the previous article: Spring core technology (3)
version 5.1.8.RELEASE
1.4.2 Dependency and configuration details
As mentioned in the previous section, you can define bean properties and constructor parameters as the reference to other managed beans (collaborators) Reference, or as a value defined inline. Spring’s XML-based configuration metadata supports the sub-elements
and
for this purpose.
Direct value (basic type, string, etc.)
The value
attribute specifies an attribute or constructor parameter as a readable string. Spring’s conversion service is used to convert these values from String
to the actual types of attributes or parameters. The following example shows the various values to be set:
Below The example uses the p namespace for more concise XML configuration:
The preceding XML is more concise. However, unless you use an IDE that supports automatic property completion when creating the bean definition (such as IntelliJ IDEA or Spring Tool Suite), you will find spelling errors at runtime rather than design time. It is strongly recommended to use such IDE help.
You can also configure the java.util.Properties
instance, as shown below:
jdbc.driver.className=com.mysql.jdbc .Driver jdbc.url=jdbc:mysql://localhost:3306/mydb
The Spring container uses the JavaBeans PropertyEditor
mechanism Convert the text in the
element to an instance of java.util.Properties
. This is a great shortcut and one of the few places where the Spring team recommends nesting
elements instead of value
attributes.
idref
element
idref
element is just a way to prevent The id
attribute (string value-not reference) of a bean is passed to the
or
element. The following example shows how to use it:
The preceding bean definition code snippet is exactly equivalent to the following code snippet (runtime) :
The first form is better than the second form, because the use of the idref
tag allows the container to verify the referenced during deployment Whether the named bean exists. In the second variant, no validation is performed on the value passed to the client
bean targetName
property. Spelling errors are only found when the client
bean is actually instantiated (most likely a fatal result). If the client
bean is a prototype bean, it may take a long time to discover this error and the resulting exception after the container is deployed.
4.0 beans XSD no longer supports the local
attribute of the idref
element, because it no longer provides other than regular bean
Value other than reference. When upgrading to version 4.0, you need to change the existing idref local
reference to idref bean
.
One of the common places (at least earlier than Spring 2.0), the value of the
element is the ProxyFactoryBean
bean definition The configuration items of the AOP interceptor. Use the
element when specifying the interceptor name to prevent misspelled interceptor IDs.
quoting other Bean (collaborator)
ref
The element is
or
defines the final element inside the element. Here, you set the value of the bean specified property as a reference to another bean (collaborator) managed by the container. The referenced bean is a dependency of the bean whose property is to be set, and it is initialized as needed before setting the property. (If the collaborator is a singleton bean, it may have been initialized by the container.) All references are ultimately references to another object. The scope and validity depend on whether the ID or name of another object is specified through the bean
, local
, or parent
attribute.
Specifying the target bean through the bean
attribute of the
tag is the most common way to allow creation of any object in the same container or parent container. A reference to the bean, whether it is in the same XML file or not. The value of the bean
attribute can be the same as the id
attribute of the target bean, or it can be the same as one of the name
attributes of the target bean. The following example shows how to use the ref
element:
via parent The code> attribute specifying the target bean will create a reference to the bean in the parent container of the current container. The value of the parent
attribute can be the same as the id
attribute of the target bean, or it can be the same as one of the name
attributes of the target bean. The target bean must be in the parent container of the current bean. When the container has a hierarchical structure and wants to wrap the existing beans in the parent container in the same way as the parent bean, the bean reference variable should be used mainly. The following pair of lists demonstrate how to use the parent
attribute:
class="org.springframework.aop.framework.ProxyFactoryBean">
4.0 beans XSD no longer supports the local
attribute of the ref
element because it no longer provides values other than regular bean
references. When upgrading to the 4.0 architecture, you need to change the existing ref local
reference to ref bean
.
internal Bean
or The
element in the code> element defines an inner bean, as shown in the following example:
< !-- this is the inner bean --> bean>
Internal bean definition does not need to define ID or name. Even if specified, the container does not use it as an identifier. The container also ignores the scope
flag when it is created, because the inner bean is always anonymous and is always created with the outer bean. Inner beans cannot be individually accessed or injected into cooperative beans.
As an extreme case, you can receive a destruction callback from a custom scope, such as an inner bean whose scope is request contained in a singleton bean. The creation of an internal bean instance is associated with the bean that contains it, but the destruction callback allows it to participate in the life cycle of the request scope. This is not a common situation. Inner beans usually just share the scope of the bean that contains it.
collection
,
,