Use @ Resource annotation to complete attribute assembly and @ resource annotation assembly

Source: Internet
Author: User

Use @ Resource annotation to complete attribute assembly and @ resource annotation assembly

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:

1 <bean id = "orderService" class = "cn. itcast. service. orderServiceBean "> 2 // constructor injection 3 <constructor-arg index =" 0 "type =" java. lang. string "value =" xxx "/> 4 // setter method injection 5 <property name =" name "value =" zhao "/> 6 </bean>

2. Use @ Autowired or @ Resource annotation in java code for assembly. However, we need to configure the information in the xml configuration file.

1  <beans Xmlns="http://www.springframework.org/schema/beans"2              Xmlns="http://www.w3.org/2001/XMLSchema-instance"3              Xmlns:context="http://www.springframework.org/schema/context"4              Xsi:schemaLocation="http://www.springframework.org/schema/beans5                          http://www.springframework.org/schema/context6                          http://www.springframework.org/schema/context/spring-context-2.5xsd">7 </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.

1 @ Autowired2 private PersonDao personDao; // used on the field 3 @ Autowired4 public void setOrderDao (OrderDao orderDao) {5 this. orderDao = orderDao; // 6}

@ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.