在下面的Spring类中,我通过@PropertySource加载app.properties文件,并使用属性文件中的配置构建两个不同的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的人,您可以按以下方式自动使用环境:
@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();
}
}发布于 2014-06-01 16:07:36
如果您正在使用(或愿意使用) Spring,那么您可以使用@ConfigurationProperties注释。
下面是Spring源代码中的一个示例:
@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。
对于您正在演示的特定DataSource示例,1.1.0.M1版本的Spring提供了基于@ConfigurationProperties的DataSourceBuilder,并极大地简化了您试图实现的配置类型。请参阅文档这里
在您的例子中,代码是:
@Bean
@ConfigurationProperties(prefix="txn")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="rpt")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}https://stackoverflow.com/questions/23981670
复制相似问题