使用@PropertySource的Spring属性配置 [英] Spring properties configuration using @PropertySource
问题描述
在下面的Spring配置类中,我通过@PropertySource加载app.properties文件,并使用属性文件中的配置构建两个不同的DBCP数据源。
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;
但是,当env.getProperty有没有更好的方法来做到这一点?
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();
}
}
编辑回到关于Environment.getProperty()不工作。它确实工作。我给的财产名称不正确。对于那些不想使用Spring Boot的用户,可以使用以下环境自动连接环境:
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();
}
}
推荐答案
如果您使用(或愿意使用)Spring Boot,那么您可以使用 @ConfigurationProperties
注释。
If you are using (or willing to use) Spring Boot then you can use the @ConfigurationProperties
annotation.
这是来自Spring Boot源代码的示例:
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;
}
}
使用上一种代码可以让你从需要的在每个字段中使用 @Value
。
Using the previous kind of code spares you from needed to use @Value
on each field.
对于您正在显示的特定DataSource示例, code> 1.1.0.M1 提供了建立在 @ConfigurationProperties
DataSourceBuilder >并大大简化了您尝试实现的配置类型。请参阅文档此处 a>
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();
}
这篇关于使用@PropertySource的Spring属性配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!