如何在spring boot中从属性文件中读取限定符?

时间:2018-05-07 06:13:03

标签: java spring-boot properties qualifiers

我有一个限定符,我从

读取
public class TestController{
     @Autowired
     @Qualifier("jdbc")
     private JdbcTemplate jtm;
     //.....
}

限定符" jdbc"是定义为

的bean
@Bean(name = "jdbc")
@Autowired
public JdbcTemplate masterJdbcTemplate(@Qualifier("prod") DataSource prod) {
            return new JdbcTemplate(prod);
        }

这是返回该限定符的数据源并且工作正常。

现在我想要从application.properties中读取限定符名称。所以我将代码更改为

public class TestController{
     @Autowired
     @Qualifier("${database.connector.name}")
     private JdbcTemplate jtm;
     //.....

}

我的application.properties中的database.connector.name=jdbc

但是当我这样做时会抛出错误

  

申请失败

     

说明

     

main.java.rest.TestController中的字段userService需要一个bean   输入' org.springframework.jdbc.core.JdbcTemplate'无法找到。

     

动作:

     

考虑定义类型' org.springframework.jdbc.core.JdbcTemplate'的bean。在   你的配置。

感谢任何帮助。

3 个答案:

答案 0 :(得分:5)

限定符无法解析占位符。您可以将TestController课程编写为

public class TestController {

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

    private JdbcTemplate jtm;

    @Autowired
    public void setJdbcTemplate(ApplicationContext context) {

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

答案 1 :(得分:3)

正如@Hemant已经提到的,默认QualifierCandidateResolver无法解析属性。

但你可以制作一个:

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());
    }
}

通过这种方式,您可以按照@Qualifier

的方式使用@Qualifier("${database.connector.name}")

完整示例:

@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());
    }
}

使用spring.application.bean=impl1

运行
com.stackoverflow.java.SO50208018Application$MyBeanImpl1

使用spring.application.bean=impl2

运行
com.stackoverflow.java.SO50208018Application$MyBeanImpl2

答案 2 :(得分:0)

如果你保留@Qualifier("jdbc"), 您可以通过提供不同的测试配置文件并为每个测试类加载所需的测试配置文件来改变注入的bean。