Detailed explanation of attribute assembly code through @ resource annotation

This paper mainly explores the problem of using @ resource annotation to realize attribute assembly, which involves dependency injection - manual assembly, and the difference between @ Autowired and @ resource annotation, as follows.

Use field injection (for annotation): the dependent objects can be injected by manual assembly or manual automatic assembly. In practical application, manual assembly is recommended, because automatic assembly will produce unknown situations and developers cannot predict the final assembly results.

Dependency injection - manual assembly

Manual assembly of dependent objects, in which there are two programming methods.

1. In the XML configuration file, configure through the bean node, such as:

2. Assemble using @ Autowired or @ resource annotation in Java code. But we need to configure the following information in the XML configuration file

This configuration implicitly registers multiple processors for parsing comments:

AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor

PersistenceAnnotationBeanProcessor,requiredAnnotationBeanPostProcessor

3. Differences

Assemble using @ Autowired or @ resource annotation in Java code. The difference between the two annotations is that @ Autowired assembles by type by default and @ resource assembles by name by default. Only when no bean matching the name can it be assembled by type

@Autowired annotation assembles dependent objects by type. By default, it requires that dependent objects must exist. If null value is allowed, its required property can be set to false; If we want to use the assembly name, we can use it together with the @ qualifier annotation, as follows:

@Like @ Autowired, the resource annotation can be marked on the setter method of a field or property, but it is assembled by name by default. The name can be specified through the name attribute of @ resource; If the name attribute is not specified, when the annotation is marked on the field, that is, the name of the default field is used as the bean name to find the dependent object; When the annotation is marked on the property setter method, the default is to take the property name as the bean name to find the dependent object

Note: if the name attribute is not specified and the object cannot be found according to the default name, @ resource annotation will fall back to assembly by type. However, once the name attribute is specified, it can only be assembled by name.

summary

The above is all about the detailed explanation of attribute assembly code through @ resource annotation. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Detailed explanation of spring bean instantiation method code

Spring instance factory method and static factory method instance code

Spring uses code to read properties file instance parsing

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>