繁体   English   中英

使用@PropertySource的Spring属性配置

[英]Spring properties configuration using @PropertySource

在下面的Spring配置类中,我通过@PropertySource加载app.properties文件,并使用来自属性文件的配置构造2个不同的DBCP数据源。

尽管一切正常,但我不喜欢为每个配置属性都声明一个带有注释的变量以构造数据源。 我试图像这样自动连接环境类

@Autowired Environment env;

但是,当env.getProperty()返回null时。 有一个更好的方法吗?

@Configuration
@PropertySource("classpath:app.properties")
public class DAOConfig {
    @Value( "${txn.dbhost}" ) private String txnDbHost;
    @Value( "${txn.dbport}" ) private Integer txnDbPort;
    @Value( "${txn.dbservice}" ) private String txnDbService;
    @Value( "${txn.dbuser}" ) private String txnDbUser;
    @Value( "${txn.dbpwd}" ) private String txnDbPwd;

    @Value( "${rpt.dbhost}" ) private String rptDbHost;
    @Value( "${rpt.dbport}" ) private Integer rptDbPort;
    @Value( "${rpt.dbservice}" ) private String rptDbService;
    @Value( "${rpt.dbuser}" ) private String rptDbUser;
    @Value( "${rpt.dbpwd}" ) private String rptDbPwd;

    @Bean(destroyMethod = "close")
    public DataSource txnDataSource() {
        return new DataSources.Builder()
                .host(txnDbHost)
                .port(txnDbPort)
                .service(txnDbService)
                .user(txnDbUser)
                .pwd(txnDbPwd)
                .build();
    }

    @Bean(destroyMethod = "close")
    public DataSource rptDataSource() {
        return new DataSources.Builder()
                .host(rptDbHost)
                .port(rptDbPort)
                .service(rptDbService)
                .user(rptDbUser)
                .pwd(rptDbPwd)
                .build();
    }
}

编辑 :我收回有关Environment.getProperty()无法正常工作。 确实有效。 我没有正确输入属性名称。 对于那些不想使用Spring Boot的用户,可以按以下方式自动连接Environment:

@Configuration
@PropertySource("classpath:app.properties")
public class DAOConfig {
    @Autowired Environment env;

    @Bean(destroyMethod = "close")
    public DataSource txnDataSource() {
        return new DataSources.Builder()
                .host(env.getProperty("txn.dbhost"))
                .port(env.getProperty("txn.dbport"))
                .service(env.getProperty("txn.dbservice"))
                .user(env.getProperty("txn.dbuser"))
                .pwd(env.getProperty("txn.dbpwd"))
                .build();
    }

}

如果您正在使用(或愿意使用)Spring Boot,则可以使用@ConfigurationProperties批注。

这是Spring Boot源代码中的一个示例:

@ConfigurationProperties(prefix = "spring.activemq")
public class ActiveMQProperties {

    private String brokerUrl = "tcp://localhost:61616";

    private boolean inMemory = true;

    private boolean pooled = false;

    private String user;

    private String password;

    // Will override brokerURL if inMemory is set to true
    public String getBrokerUrl() {
        if (this.inMemory) {
            return "vm://localhost";
        }
        return this.brokerUrl;
    }

    public void setBrokerUrl(String brokerUrl) {
        this.brokerUrl = brokerUrl;
    }

    public boolean isInMemory() {
        return this.inMemory;
    }

    public void setInMemory(boolean inMemory) {
        this.inMemory = inMemory;
    }

    public boolean isPooled() {
        return this.pooled;
    }

    public void setPooled(boolean pooled) {
        this.pooled = pooled;
    }

    public String getUser() {
        return this.user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

有效的做法是将属性spring.activemq.*映射到它们各自的属性。

使用前一种代码@Value在每个字段上使用@Value

对于所显示的特定DataSource示例,Spring Boot从1.1.0.M1版本1.1.0.M1提供了DataSourceBuilder ,它基于@ConfigurationProperties构建,并大大简化了您尝试实现的配置类型。 请参阅此处的文档

在您的情况下,代码为:

@Bean
@ConfigurationProperties(prefix="txn")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="rpt")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM