The @Resource annotation implements the property assembly code detail

  • 2020-12-18 01:50:11
  • OfStack

This article mainly explores the problem of using @Resource annotation to implement attribute assembly, which involves dependency injection - manual assembly, the difference between @Autowired and @Resource annotations, as follows.

Use Field injection (for annotation purposes) : The injection dependency objects can be assembled manually or automatically. In practical applications, manual assembly is recommended because automatic assembly can produce unknown situations where the developer cannot foresee the final assembly result.

Dependency injection - Manual assembly

Manual assembly of dependent objects, in this way there are two more ways of programming.

1. In the xml configuration file, bean node is configured, as follows:


<bean id="orderService" class="cn.itcast.service.OrderServiceBean">
  // Constructor injection 
  <constructor-arg index="0" type="java.lang.String" value="xxx"/>
  // Belong to setter Methods to inject 
  <property name="name" value="zhao"/>
</bean>

2. Use @Autowired or @Resource annotation for assembly in java code. But we need to configure the information under 1 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 handlers that parse annotations:

AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor

PersistenceAnnotationBeanProcessor,RequiredAnnotationBeanPostProcessor

3. The difference between

Assemble in java code using @Autowired or @ES38en annotations. The difference between the two annotations is that @Autowired defaults to assembling by type @Resource defaults to assembling by name and will only assemble by type when bean matching the name cannot be found


@Autowired
private PersonDao personDao;// For fields 
@Autowired
public void setOrderDao(OrderDao orderDao){
    this.orderDao = orderDao; // For attributes setter On the way    
}

The @Autowired annotation is to assemble dependent objects by type. By default, it requires that dependent objects must exist. If the null value is allowed, set its required attribute to false. If we want to use name assembly, we can use it in conjunction with @Qualfier annotation 1, as follows:


@Autowired@Qualifier("personDao")
private PersonDao personDao;

The @Resource annotation, like @Autowired1, can be attached to the setter method of a field or property, but it is assembled by name by default. The name can be specified by the name attribute of @Resource; If the name attribute is not specified, when the annotation is on the field, the default field name is found as the bean name; When an annotation is placed on the attribute setter method, the default attribute name is found as the bean name for 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 falls back to type-by-type assembly. But once the name attribute is specified, it can only be assembled by name.

conclusion

That's the end of this article on the @Resource annotation to implement the attribute assembly code in detail, and I hope you found it helpful. Those who are interested can continue to see this site:

Code Explanation of the Way to Instantiate bean

Instance Factory Method and Static Factory Method Instance Code of Spring

Spring Reading properties File Instance Analysis with Code

If there is any deficiency, please let me know. Thank you for your support!


Related articles: