Detailed description of the property assembly code using the @ Resource annotation and @ resource Annotation
This article mainly explores the problem of using @ Resource annotation to implement property assembly, which involves dependency injection-manual assembly, and the difference between @ Autowired and @ Resource annotation. The details are as follows.
Use Field Injection (for annotation): Injection dependent objects can be manually or automatically assembled. Manual assembly is recommended in practical applications, because automatic assembly will produce unknown conditions and developers cannot predict the final assembly results.
Dependency injection-manual assembly
Manually assemble dependent objects. There are two programming methods in this method.
1. Configure bean nodes in the xml configuration file, such:
<Bean id = "orderService" class = "cn. itcast. service. orderServiceBean "> // constructor injection <constructor-arg index =" 0 "type =" java. lang. string "value =" xxx "/> // setter method injection <property name =" name "value =" zhao "/> </bean>
2. Use @ Autowired or @ Resource annotation in java code for assembly. However, we need to configure the information in the xml configuration file.
<beans Xmlns="http://www.springframework.org/schema/beans" Xmlns="http://www.w3.org/2001/XMLSchema-instance" Xmlns:context="http://www.springframework.org/schema/context" Xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5xsd"></beans>
This configuration implicitly registers multiple processors that parse comments:
AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanProcessor, RequiredAnnotationBeanPostProcessor
3. Differences
Use @ Autowired or @ Resource annotation in java code for assembly. The difference between the two annotations is that @ Autowired is assembled by type by default. @ Resource is assembled by name by default. Only beans with the same name cannot be found will be assembled by type.
@ Autowiredprivate PersonDao personDao; // used on the field @ Autowiredpublic void setOrderDao (OrderDao orderDao) {this. orderDao = orderDao; // The setter method used for the attribute}
@ Autowired annotation is used to assemble dependent objects by type. By default, it requires that the dependent object must exist. If the null value is allowed, set its required attribute to false. If we want to assemble objects by name, it can be used with the @ Qualfier annotation as follows:
@Autowired@Qualifier("personDao")private PersonDao personDao;
The @ Resource annotation is the same as the @ Autowired annotation. It can be labeled on the setter method of the field or attribute, 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, that is, the property name is used as the bean name by default to find the dependent object.
@Resource(name="personDaoBean")private PersonDao personDao;
Note: If the name attribute is not specified and the object cannot be found by default name, the @ Resource annotation will be rolled back to assemble by type. However, once the name attribute is specified, it can only be assembled by name.
Summary
The above is all the details about how to use the @ Resource annotation to implement the property assembly code. I hope it will be helpful to you. Interested friends can continue to refer to this site:
《Code explanation of Spring bean instantiation Method"
《Spring instance factory method and static factory Method Instance code"
《Spring uses code to read properties file instance Parsing"
If you have any shortcomings, please leave a message. Thank you for your support!