Spring Annotations Summarize----@Autowired and @qualifier, @Resource

Source: Internet
Author: User

Prefacebecause of the limited capacity, will not be too in-depth discussion of spring annotations, will only explain the basic use of annotations@Autowired

@Autowired can help us inject a property, usually on a common method (or on a variable or a constructor).

@Autowired are matched by type, so an error occurs if there are two parameters of the same type

Look at the following example

public class Bean2 {@Overridepublic String toString () {return ' Bean2 ...}}

public class Bean1 {private Bean2 bean2;public Bean2 getBean2 () {return bean2;} @Autowiredpublic void SetBean2 (Bean2 bean2) {this.bean2 = bean2;} Public String toString () {return ' Bean1 ....}}
This is a bean, and in Bean1 there is an attribute Bean2, which is injected with @autowired to inject this attribute

The corresponding XML definition is as follows

<!--use  this scan to use annotated packages so that annotations are useful--><context:component-scan base-package= "Bean"/><bean id= "Bean1" Class= "Bean. Bean1 "/><bean id=" bean2 "class=" bean. Bean2 "/>
Then the test class
public static void Main (string[] args) {ApplicationContext ctx = new Classpathxmlapplicationcontext (" Applicationcontext.xml "); Bean1 bean1 = (Bean1) ctx.getbean ("Bean1"); System.out.println (bean1);}
At this time print out is bean2 ....

Prove the injection was successful.

Since @autowired is injected by type, it can be injected successfully if you remove the ID from the XML file.


@Qualifier can narrow the range of matches

@Qualifier common use with @autowired to resolve multiple type matching problems when using @autowired

Use as follows (configuration file and test code are the same as above)

public class Bean1 {private Bean2 bean2;public Bean2 getBean2 () {return bean2;} @Autowired @qualifier ("bean2") public void SetBean2 (Bean2 bean2) {this.bean2 = bean2;} Public String toString () {return ' Bean1 ....}}

After using @autowired for injection, the @qualifier is used to narrow the match, that is, the type Bean2 is injected with the name Bean2, and an error occurs if it is not found.

For multiple parameter methods, if you need to inject, then @qualifier can be used before the parameters, so that you can inject a number of parameters

The code is as follows (BEAN3 and Bean2)

public class Bean1 {private Bean2 bean2;private Bean3 bean3;public Bean2 getBean2 () {return bean2;} Public Bean3 getBean3 () {return bean3;} @Autowiredpublic void Set (@Qualifier ("bean2") Bean2 bean2, @Qualifier ("Bean3") Bean3 bean3) {this.bean2 = bean2; THIS.BEAN3 = bean3;} Public String toString () {return ' Bean1 ....}}
when @autowired injection is used, a bean named bean2 is injected with the first parameter, and the bean with the name bean3 is injected into the second parameter so that multiple parameters can be injected (bean3 need to be declared in the XML file)

@Resource@Resource can act on a property or method, unlike @autowired, @Resource by default is injected by name, specifying a name when used, so that the corresponding bean injection is found, and if the corresponding bean is not found, it is injected according to the type

Use the following (the other code is the same as above)

public class Bean1 {private Bean2 bean2;public Bean2 getBean2 () {return bean2;} @Resource (name = "bean2") public void SetBean2 (Bean2 bean2) {this.bean2 = bean2;} Public String toString () {return ' Bean1 ....}}
This way, a bean named bean2 will inject

@Autowired and @resource each have their own usage, if there are multiple parameters in the method to be injected, you can use @autowired, and in other cases, you can use the @resource

Spring Annotations Summary [email protected] and @qualifier, @Resource

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.