Java 使用@PropertySource 配置 Spring 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23981670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring properties configuration using @PropertySource
提问by MFIhsan
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.
在下面的 Spring 配置类中,我通过 @PropertySource 加载 app.properties 文件并使用属性文件中的配置构建 2 个不同的 DBCP 数据源。
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?
但是,当 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();
}
}
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:
编辑:我收回关于 Environment.getProperty() 不起作用的说法。它确实有效。我错误地给出了属性名称。对于那些不想使用 Spring Boot 的人,您可以按如下方式自动装配环境使用它:
@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();
}
}
采纳答案by geoand
If you are using (or willing to use) Spring Boot then you can use the @ConfigurationProperties
annotation.
如果您正在使用(或愿意使用)Spring Boot,那么您可以使用@ConfigurationProperties
注释。
Here is an example from the Spring Boot source code:
这是 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;
}
}
Effectively what this does is map the properties spring.activemq.*
to their respective properties.
这实际上是将属性映射spring.activemq.*
到它们各自的属性。
Using the previous kind of code spares you from needed to use @Value
on each field.
使用前一种代码使您无需@Value
在每个字段上使用。
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
对于您显示的特定数据源示例,Spring Boot 版本1.1.0.M1
提供了DataSourceBuilder
构建@ConfigurationProperties
并大大简化了您尝试实现的配置类型。请参阅此处的文档
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();
}