Spring @Qualifier Example

Source: Internet
Author: User

@Qualifier often appear in an interface, a number of implementation class calls in the case, in some choice processing, give the use of examples, realize this kind of beauty.

package spring;import org.springframework.beans.factory.annotation.autowired;import  org.springframework.beans.factory.annotation.qualifier;import  org.springframework.context.annotation.annotationconfigapplicationcontext;import  org.springframework.context.annotation.componentscan;import  org.springframework.context.annotation.configuration;import org.springframework.stereotype.component; import javax.annotation.postconstruct;import java.lang.annotation.elementtype;import  java.lang.annotation.retention;import java.lang.annotation.retentionpolicy;import  Java.lang.annotation.target;import static spring. Spring.platform, @Configuration @componentscanpublic class spring {    public  static void main (String[] args)  {        new  annotationconfigapplicationcontext (spring.class);     }    @autowired     @Platform (Platform.OperatingSystems.ANDROID)     private  MarketPlace android;     @Autowired      @Platform ( Platform.OperatingSystems.IOS)     private MarketPlace ios;      @PostConstruct     public void qualifythetweets ()  {         system.out.println ("ios:"  + this.ios);         system.out.println ("android:"  + this.android);    }     // the type has to be public!     @Target ({ Elementtype.field,            elementtype.method,             ElementType.TYPE,          &nbsP;  elementtype.parameter})      @Retention (retentionpolicy.runtime)       @Qualifier     public static  @interface  Platform {         operatingsystems value ();         public static enum OperatingSystems {             IOS,             android        }    }}

Interface:

Interface MarketPlace {}

Implementing Class One:

@Component @platform (Platform.OperatingSystems.IOS) class Applemarketplace implements MarketPlace {@Override public S    Tring toString () {return ' Apple '; }}

Implementing Class Two:

@Component @platform (Platform.OperatingSystems.ANDROID) class Googlemarketplace implements MarketPlace {@Override pub    Lic String toString () {return ' Android '; }}


Spring @Qualifier Example

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.