Use @ resource annotation to complete property assembly

Source: Internet
Author: User

Use Field Injection (for annotation)

Manual assembly or automatic assembly can be used to inject dependent objects. manual assembly is recommended in actual applications.

You can also use @ autowired or @ resource to configure the following information in the XML configuration file:

<Beans xmlns = http://www.springframework.org/schema/beans

Xmlns: xsi = 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/beans/spring-beans-2.5.xsd

Http://www.springframework.org/schema/context

Http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<Context: annotation-config/>

</Beans>

This configuration implicitly registers multiple processors for annotation parsing: autowiredannotationbeanpostprocessor, commonannotationbeanpostprocessor, persistenceannotationbeanpostprocessor, requiredannotationbeanpostprocessor

Note: @ resource annotation in the spring installation directory Lib \ J2EE \ common-annotations.jar

 

@ Autowired and @ resource annotation assembly, the difference is:

@ Autowired is assembled by type by default, and @ resource is assembled by name by default. Only beans with the same name cannot be found will be assembled by type.

 

@ Autowired

Private persondao is used on fields

@ Autowired

Public void setorderdao (orderdao) {on the setter method used for the attribute

This. orderdao = orderdao;

}

@ Autowired Zhu explains how to assemble dependency objects by type. By default, dependency objects must exist. If null values are allowed, set the required attribute to false, if we use name-based assembly, we can use it together with the @ qualifier annotation. As follows:

@ Autowired @ qualifier ("persondaobean ")

Private persondao;

 

@ Resource annotation (recommended, is a class in the J2EE package) and @ autowired (is a class in spring) can also be marked on the setter method of the field or attribute, however, 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, the annotation is blocked on the field. By default, the field name is used as the bean name to find the dependent object, block the annotation in the setter method of the attribute. By default, the attribute name is used as the bean name to find the dependent object.

@ Resource (name = "persondaobean ")

Private persondao // used for fields

 

Note: If the name attribute is not specified and the dependent object cannot be found by default name, the @ resource annotation will be rolled back to the Assembly by type, but once the name attribute is specified, you can only assemble by name.

Http://blog.163.com/ky_199/blog/static/14317602009225520045/

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.