【发布时间】:2015-07-24 14:40:36
【问题描述】:
谁能给我一些指导,告诉我实现这一目标的最佳方式。
我想扩展 Spring Boot Externalized Configuration 以便我有一个可以从我的应用程序的任何位置调用的方法。此方法将使用键检索属性值。此方法将首先查询一个数据库表,如果它没有找到指定的键,它将回退到 1 中描述的 PropertySource 顺序。
所以我会有类似的服务:
@Service
public class ConfigurationService {
private final ConfigurationRepository configurationRepository;
@Autowired
public ConfigurationService(ConfigurationRepository configurationRepository) {
this.configurationRepository = configurationRepository;
}
public String getValue(String key) {
Configuration configuration = configurationRepository.findOne(key);
// Add something here to get the property from application.properties if the key does not exist in the db
return configuration == null ? null : configuration.getValue();
}
}
我可以使用如下:
foo = configuration.getValue("my.property");
有没有更好的方法来解决这个问题?我是否缺少可以使用的 Spring Boot 功能?
编辑:我希望能够在应用程序运行时更改属性值并获取这些新值。
【问题讨论】:
-
根据您的项目/部署的规模,这听起来像是 Spring Cloud Config 的潜在案例。
-
写一个由数据库支持的
PropertySource,这样它就可以与系统的其余部分集成。或者简单地编写一个ApplicationInitializer,它从数据库中加载所有属性,将它们包装在PropertiesPropertySource中并将它们添加到环境中。至少您不想在要与默认机制集成的整个地方调用此方法。 -
如何使用与数据库链接的缓存来存储键值对
-
我认为由数据库支持的
PropertySource是我所需要的。如何确保在 application.properties 等之前考虑这一点。 -
将它放在另一个
PropertySources的顶部或您想要的位置。
标签: java spring properties configuration spring-boot