Announcement Announcement Module
Collapse
No announcement yet.
@Autowired + @Qualifier + JavaConfig confusion... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Autowired + @Qualifier + JavaConfig confusion...

    Hello

    I have this class

    Code:
    @Configuration
    public class SpringBeansConfigVersion01 {
    
    	@Bean
    	public PersonaService personaImplVersion01(){
    		//PersonaImplVersion01 personaImplVersion01 = new PersonaImplVersion01();
    		PersonaService personaImplVersion01 = new PersonaImplVersion01();
    		return personaImplVersion01;
    	}
    	
    	@Bean(name="beanPersonaImplVersion02")
    	public PersonaService personaImplVersion02(){
    		PersonaImplVersion02 personaImplVersion02 = new PersonaImplVersion02();
    		return personaImplVersion02;
    	}
    	
    }
    Since I have two @Bean's returning the same type PersonaService I am doing the follow...
    (see blue and red approaches)

    Code:
    @Configuration
    @Import(value={SpringBeansConfigVersion01.class})
    public class SpringBeansConfigVersion02 {
    
    	@Autowired
    	@Qualifier("personaImplVersion01")
    	private PersonaService personaImplVersion;
    	
    	
    	@Bean
    	public CasoDeUsoService casoDeUsoImplVersion01(){
    		CasoDeUsoService casoDeUsoImplVersion01 = new CasoDeUsoImplVersion01(personaImplVersion); 
    		return casoDeUsoImplVersion01;
    	}
    	
    	
    	@Bean
    	@Autowired
    	@Qualifier("beanPersonaImplVersion02")
    	public CasoDeUsoService casoDeUsoImplVersion02(PersonaService personaImplVersion){
    		CasoDeUsoService casoDeUsoImplVersion02 = new CasoDeUsoImplVersion02();
    		((CasoDeUsoImplVersion02) casoDeUsoImplVersion02).setPersonaService(personaImplVersion);
    		return casoDeUsoImplVersion02;
    	}
    	
    }
    For the red part I receive...

    Code:
    Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
    No qualifying bean of type [com.panamsource.manuel.servicio.interfaces.PersonaService] is defined: 
    expected single matching bean but found 2: personaImplVersion01,beanPersonaImplVersion02
    	at
    But If I do the follow work...

    Code:
    @Bean
    @Autowired
    public CasoDeUsoService casoDeUsoImplVersion02(@Qualifier("beanPersonaImplVersion02") PersonaService personaImplVersion){
    		CasoDeUsoService casoDeUsoImplVersion02 = new CasoDeUsoImplVersion02();
    		((CasoDeUsoImplVersion02) casoDeUsoImplVersion02).setPersonaService(personaImplVersion);
    		return casoDeUsoImplVersion02;
    	}
    After to do a quick research it has sense according with the @Qualifier API
    Wich says...

    Code:
    @Target(value={FIELD,METHOD,PARAMETER,TYPE,ANNOTATION_TYPE})
    @Retention(value=RUNTIME)
    @Inherited
    @Documented
    public @interface Qualifier
    
    This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring. 
    It may also be used to annotate other custom annotations that can then in turn be used as qualifiers.
    Ok, I have confirmed the blue parts in all the source code shown above

    Doubt1: but the orange part ...what means?

    Now... If I use

    Code:
    @Bean
    @Autowired
    public CasoDeUsoService casoDeUsoImplVersion02(@Qualifier("beanPersonaImplVersion02") PersonaService personaImplVersion){
    		CasoDeUsoService casoDeUsoImplVersion02 = new CasoDeUsoImplVersion02();
    		((CasoDeUsoImplVersion02) casoDeUsoImplVersion02).setPersonaService(personaImplVersion);
    		return casoDeUsoImplVersion02;
    	}
    or (commented)

    Code:
    @Bean
    //@Autowired
    public CasoDeUsoService casoDeUsoImplVersion02(@Qualifier("beanPersonaImplVersion02") PersonaService personaImplVersion){
    		CasoDeUsoService casoDeUsoImplVersion02 = new CasoDeUsoImplVersion02();
    		((CasoDeUsoImplVersion02) casoDeUsoImplVersion02).setPersonaService(personaImplVersion);
    		return casoDeUsoImplVersion02;
    	}
    has the same behavior, therefore @Autowired is not mandatory for config methods ......I am assuming that @Bean is part of the solution...Now in @Bean API, it is not explicit about this, I mean config methods expecting some Parameter...none example about that in the same API BTW , but in @Autowired API says

    Code:
    @Target(value={CONSTRUCTOR,FIELD,METHOD,ANNOTATION_TYPE})
    @Retention(value=RUNTIME)
    @Documented
    public @interface Autowired
    
    Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.
    Check the blue part..

    Doubt 02: When should be mandatory use @Autowired for a config method?
    My unique explanation would be for @Autowired(required=false).

    If you have some appreciations....share your knowledge

    Thank You!
Working...
X