Itcast video-spring learning notes (use @ resource annotation to complete attribute Assembly)

Source: Internet
Author: User

ThanksItcastFree video released.

 

UseFieldInjection (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@ AutowiredOr@ ResourceWe needXMLConfigure the following information in the 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 that parse comments:Autowiredannotationbeanpostprocessor, commonannotationbeanpostprocessor, persistenceannotationbeanpostprocessor, requiredannotationbeanpostprocessor

Note:@ ResourceAnnotation inSpringInstallation DirectoryLib \ J2EE \ common-annotations.jar

 

@ AutowiredAnd@ ResourceAnnotation Method for assembly, the difference is:

@ AutowiredDefault assembly by type,@ ResourceBy default, names are assembled. If no matching name is foundBeanAssembly by type.

 

@ Autowired

Private persondaoUsed on fields

@ Autowired

Public void setorderdao (orderdao ){Used for AttributeSetterMethod

This. orderdao = orderdao;

}

@ AutowiredZhu explains to assemble dependent objects by type. By default, it requires that dependent objects exist.NullValue, you can set itsRequiredThe property isFalse,If we want to assemble by name, we can combine@ QualifierAnnotations are used together. As follows:

@ Autowired @ qualifier ("persondaobean ")

Private persondao;

 

@ Resource Annotation and @ Autowired You can also mark Setter Method, but it is assembled by name by default. The name can be @ Resource Of Name Attribute specified. If not specified Name Attribute to block the Annotation on the field, that is, the default field name is used Bean Find the dependent object by name to block Setter Method, that is, the default attribute name is used Bean Search for dependent objects by name.

@ Resource (name = "persondaobean ")

Private persondao //Used on fields

 

Note:NameAttribute, and the dependent object cannot be found according to the default name,@ ResourceThe annotation will be rolled back to the by-type assembly, but onceNameAttribute, you can only assemble 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.