首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用@PropertySource配置Spring属性

使用@PropertySource配置Spring属性
EN

Stack Overflow用户
提问于 2014-06-01 15:55:24
回答 1查看 13.9K关注 0票数 2

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

尽管一切都很好,但我不喜欢为每个配置属性声明一个变量,并使用注释来构造数据源。我试着像这样自修环境课

代码语言:javascript
复制
@Autowired Environment env;

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

代码语言:javascript
复制
@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的人,您可以按以下方式自动使用环境:

代码语言:javascript
复制
@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();
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-01 16:07:36

如果您正在使用(或愿意使用) Spring,那么您可以使用@ConfigurationProperties注释。

下面是Spring源代码中的一个示例:

代码语言:javascript
复制
@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

对于您正在演示的特定DataSource示例,1.1.0.M1版本的Spring提供了基于@ConfigurationPropertiesDataSourceBuilder,并极大地简化了您试图实现的配置类型。请参阅文档这里

在您的例子中,代码是:

代码语言:javascript
复制
@Bean
@ConfigurationProperties(prefix="txn")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="rpt")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23981670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档