簡體   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