As @Hemant already mentioned default QualifierCandidateResolver
does not resolve properties.
But you can make one, which does:
import java.lang.annotation.Annotation;
import org.springframework.beans.TypeConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.stereotype.Component;
@Component
public static class AutowireCandidateResolverConfigurer implements BeanFactoryPostProcessor {
private static class EnvironmentAwareQualifierAnnotationAutowireCandidateResolver extends QualifierAnnotationAutowireCandidateResolver {
private static class ResolvedQualifier implements Qualifier {
private final String value;
ResolvedQualifier(String value) { this.value = value; }
@Override
public String value() { return this.value; }
@Override
public Class<? extends Annotation> annotationType() { return Qualifier.class; }
}
@Override
protected boolean checkQualifier(BeanDefinitionHolder bdHolder, Annotation annotation, TypeConverter typeConverter) {
if (annotation instanceof Qualifier) {
Qualifier qualifier = (Qualifier) annotation;
if (qualifier.value().startsWith("${") && qualifier.value().endsWith("}")) {
DefaultListableBeanFactory bf = (DefaultListableBeanFactory) this.getBeanFactory();
ResolvedQualifier resolvedQualifier = new ResolvedQualifier(bf.resolveEmbeddedValue(qualifier.value()));
return super.checkQualifier(bdHolder, resolvedQualifier, typeConverter);
}
}
return super.checkQualifier(bdHolder, annotation, typeConverter);
}
}
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
DefaultListableBeanFactory bf = (DefaultListableBeanFactory) beanFactory;
bf.setAutowireCandidateResolver(new EnvironmentAwareQualifierAnnotationAutowireCandidateResolver());
}
}
With that you will be able to use @Qualifier
in a way you've asked @Qualifier("${database.connector.name}")
Full example:
@SpringBootApplication
public class SO50208018Application {
public static void main(String[] args) { SpringApplication.run(SO50208018Application.class, args); }
interface MyBean { }
static class MyBeanImpl1 implements MyBean { }
static class MyBeanImpl2 implements MyBean { }
@Bean @Qualifier("impl1")
MyBean bean1() { return new MyBeanImpl1(); }
@Bean @Qualifier("impl2")
MyBean bean2() { return new MyBeanImpl2(); }
@Component
public static class AutowireCandidateResolverConfigurer implements BeanFactoryPostProcessor {
// configurer from above
}
@Bean
CommandLineRunner run(@Qualifier("${spring.application.bean}") MyBean bean) {
return (args) -> System.out.println(bean.getClass().getName());
}
}
Run with spring.application.bean=impl1
:
com.stackoverflow.java.SO50208018Application$MyBeanImpl1
Run with spring.application.bean=impl2
:
com.stackoverflow.java.SO50208018Application$MyBeanImpl2