Spring property configuration using @PropertySource

In the below Spring config class I load the app.properties file via @PropertySource and build 2 different DBCP datasources using the config from the properties file.

While everything works fine, I don't like to declare a variable for each annotated config property in order to build the datasource. I tried using Environment class like this

@Autowired Environment env;

      

However, when env.getProperty () returns null. Is there a better way to do this?

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

      

Edit . I believe Environment.getProperty () is not working. It really works. I was spelling the property names incorrectly. For those who don't want to use Spring Boot, you can autwire Environment how to use it like this:

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

}

      

+1


source to share


1 answer


If you are using (or want to use) Spring Boot then you can use annotation @ConfigurationProperties

.

Below is a sample Spring Boot source code:

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

}

      

Effectively what it does is map properties spring.activemq.*

to their respective properties.



Using the previous kind of spare parts code, you need to use @Value

for each field.

For the specific DataSource example you are showing, Spring Boot from version 1.1.0.M1

contains DataSourceBuilder

which builds on @ConfigurationProperties

and greatly simplifies the configuration you are trying to achieve. See documentation here

In your case, the code will look like this:

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

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

      

+3


source







All Articles