Ambiguity of automatic assembly processing in spring-

Source: Internet
Author: User

Automatic assembly can be a great help for dependency injection because it reduces the number of explicit assemblies required to assemble application components.

However, automatic assembly is only valid if only one bean matches the desired result. If not only one bean can match, this ambiguity will hinder spring's automatic assembly of attributes, constructor parameters or method parameters. However, when ambiguity does occur, spring provides a variety of options to address such problems. You can set one of the optional beans as the preferred (primary) bean, or use qualifiers (qualifier) to help spring narrow the range of optional beans to just one bean.

Javaconfig mode:

    • Implicit
      1 @Component 2 @Primary 3  Public class Implements Mediaplayer{...}
    • An explicit
      1  Public class Magicbean {2     @Bean// explicitly declares a bean,id default to method name 3    @Primary  4public       magicbean Magicbean () {5         return New Magicbean (); 6     }7 }

XML Schema:

<id= "CDPlayer"  class= "Soundsystem.cdplayer"  Primary  = "true"></bean>

However, if you set primary with two similar interfaces that inherit the same interface, there will still be ambiguity, so introduce qualifier. Qualifier annotations are the primary way to use qualifiers, which can be used in conjunction with @autowired and @inject to specify which bean to inject in the injection. For example, we want to make sure that icecream is injected into Setdessert ():

1 @AutoWired 2 @Qualifier ("icecream")3publicvoid  setdessert (dessert dessert) {  4this       . dessert =dessert;   5 }

The parameter set here is the ID of the bean you want to inject. All classes that use the @component annotation declaration are created as beans, and the bean's ID first becomes a lowercase class name, so @qualifier ("icecream") Points to the bean that was created when the component scanned and the bean is an instance of the Icecream class.

However, arguments of the string type are unsafe, and if you modify the class name, it is also affected, so we need to create a custom qualifier, just add @qualifier ("cold") to the Icecream class:

1 @Component 2 @Qualifier ("cold")3publicclassimplements dessert{...}

Or in an explicit mode:

1 @Bean 2 @Qualifier ("cold")3 public Dessert icecream () {4          returnnew  icecream (); 5 }

The corresponding setter method also sets the parameter to cold.

However, if another bean also uses the cold qualifier, there will be ambiguity, and Java does not allow multiple annotations of the same type on the same item, or the compiler will error, so we need to create a custom qualifier annotation, Use such annotations to express the attributes that the bean wants to qualify.

1  PackageCom.myapp;2 3 ImportJava.lang.annotation.ElementType;4 Importjava.lang.annotation.Retention;5 ImportJava.lang.annotation.RetentionPolicy;6 ImportJava.lang.annotation.Target;7 8 ImportOrg.springframework.beans.factory.annotation.Qualifier;9 Ten @Target ({elementtype.constructor,elementtype.field,elementtype.method,elementtype.type}) One @Retention (retentionpolicy.runtime) A @Qualifier -  Public@InterfaceCold {}

So we create a note called cold, which is added to its class as a qualifier note:

1 @Component 2 @Cold 3  Public class Implements Dessert{...}

This allows the qualification to be added multiple times so that only one of the beans is eventually qualified to qualify for the condition. Yuci Also, custom annotations are more type-safe than using the original @qualifier and specifying qualifiers with a string type.

Ambiguity of automatic assembly processing in spring-

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.