使用@PropertySource进行Spring属性配置

在下面的Spring配置类中,我通过@PropertySource加载app.properties文件,并使用属性文件中的配置构建2个不同的DBCP数据源。

虽然一切正常,但我不喜欢为带有注释的每个配置属性声明一个变量以构造数据源。 我试着像这样自动assembly环境类

@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 Boot的用户,可以使用以下方式自动assembly环境:

 @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注释。

以下是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; } } 

这样做的有效方法是将属性spring.activemq.*映射到它们各自的属性。

使用上一种代码@Value在每个字段上使用@Value

对于您显示的特定DataSource示例,Spring Boot从版本1.1.0.M1提供构建在@ConfigurationProperties上的DataSourceBuilder ,并极大地简化了您尝试实现的配置类型。 请参阅此处的文档

在您的情况下,代码将是:

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