Reputation: 1057
In the below Spring configuration class, I'm loading app.properties file via @PropertySource and constructing 2 different DBCP data sources using the configurations from the properties file.
Though everything works fine, I don't like declaring a variable for each configuration property with an annotation in order to construct the data source. I tried to autowire 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 take that back about Environment.getProperty() not working. It indeed works. I was giving property names incorrectly. For those who don't want to use Spring Boot, you could autowire Environment as use it as follows:
@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();
}
}
Upvotes: 2
Views: 14923
Reputation: 63991
If you are using (or willing to use) Spring Boot then you can use the @ConfigurationProperties
annotation.
Here is an example from the 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 this does is map the properties spring.activemq.*
to their respective properties.
Using the previous kind of code spares you from needed to use @Value
on each field.
For the specific DataSource example you are showing, Spring Boot as of version 1.1.0.M1
provides the DataSourceBuilder
which build on @ConfigurationProperties
and vastly simplifies the kind of configuration you are trying to achieve. See the documentation here
In your case the code would be:
@Bean
@ConfigurationProperties(prefix="txn")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="rpt")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
Upvotes: 4