The main issues in this article are used to implement attribute assembly using @Resource annotation, which involves dependency injection - manual assembly, the difference between @Autowired and @Resource annotations, etc., as follows.
Use Field Injection (for annotation method): Injecting dependent objects can be manually assembled or manually assembled automatically. Manual assembly is recommended in practical applications, because automatic assembly will cause unknown situations and developers cannot foresee the final assembly result.
Dependency injection - manual assembly
There are two programming methods for manually assembling dependent objects.
1. In the xml configuration file, configure it through the bean node, such as:
<bean id="orderService"> //Constructor injection<constructor-arg index="0" type="java.lang.String" value="xxx"/> //Property setter method injection<property name="name" value="zhao"/></bean>
2. Use @Autowired or @Resource annotation to assemble in java code. But 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:
Autowired AnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanProcessor, RequiredAnnotationBeanPostProcessor
3. Difference
Use @Autowired or @Resource annotation to assemble in java code. The difference between these two annotations is that @Autowired is assembled by type by default @Resource is assembled by name by default. Only when a bean matching the name cannot be found will it be assembled by type
@Autowiredprivate PersonDao personDao;//Used on the field @Autowiredpublic void setOrderDao(OrderDao orderDao){ this.orderDao = orderDao; //Used on the setter method of the property}The @Autowired annotation is to assemble dependent objects by type. By default, it requires that dependent objects must exist. If null values are allowed, it can be set to false; if we want to assemble with name, we can use it in conjunction with @Qualfier annotation, as follows:
@Autowired@Qualifier("personDao")private PersonDao personDao;The @Resource annotation, like @Autowired, can be annotated 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, the name of the default field is used as the bean name to look for the dependency object; when the annotation is marked on the property setter method, the attribute name is used as the bean name to look for the dependency object
@Resource(name="personDaoBean")private PersonDao personDao;
Note: If the name attribute is not specified and the object is still not found by default, the @Resource annotation falls back to assembly by type. But once the name attribute is specified, it can only be assembled by name.
Summarize
The above is all the detailed explanation of the implementation of attribute assembly code through @Resource annotation. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
" Detailed explanation of the method and code of Spring instantiation bean "
" Spring's instance factory method and static factory method example code "
" Spring uses code to read properties file instance analysis "
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!