How to read Qualifier from property file in spring boot?

南笙酒味 提交于 2019-12-01 04:29:28

Qualifier doesn't resolve placeholder. You can write your TestController class as

public class TestController {

    @Value("${database.connector.name}")
    private String name;

    private JdbcTemplate jtm;

    @Autowired
    public void setJdbcTemplate(ApplicationContext context) {

        jtm = (JdbcTemplate) context.getBean(name);
    }
}
Bohdan Levchenko

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

If you keep the @Qualifier("jdbc"), you can vary the bean that is injected by providing different test configuration files and loading the desired one for each test class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!