java - @ConfigurationProperties 不使用 PropertySourcesPlaceholderConfigurer
问题描述
PropertySourcesPlaceholderConfigurer
适用于我@Value
的 s 但不适用于以下广告数据源配置
@Bean
@ConfigurationProperties(prefix = "datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
我自定义PropertySourcesPlaceholderConfigurer
解码配置文件中的密码,但在这个确切的地方没有触发解码功能,而它在其他地方工作。您能否提一些建议?
解决方案
默认情况下,Spring 将使用简单/非包装ConfigurationPropertySource
而不是更复杂的PropertySourcesPlaceholderConfigurer
,它包含多个PropertySource
s。
可以在其内部找到一个DataSourceBuilder
示例
private void bind(DataSource result) {
ConfigurationPropertySource source = new MapConfigurationPropertySource(this.properties);
ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases();
aliases.addAliases("url", "jdbc-url");
aliases.addAliases("username", "user");
Binder binder = new Binder(source.withAliases(aliases));
binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(result));
}
对于该片段,通常this.properties
使用DataSourceProperties
Bean填充,这是一个带@ConfigurationProperties
注释的类
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {
问题是,@ConfigurationProperties
将 1:1 映射到属性文件,这是非常固执的。
@Value
是另一种野兽。
我在这个答案中解决了一个完全自定义的实现。
你可能会觉得它很有价值。
推荐阅读
- laravel - err_too_many_redirects 和此页面在 laravel 我的中间件中不起作用
- html - 如何为 CSS img 封面设置锚点
- javascript - 滚动时表中的输入字段“保留”同一行中的编辑数字(JSView)
- r - 从不同文件夹导入多个 csv 文件并将文件名提取为附加列:标题和多文件夹案例
- arangodb - Arango特别慢,为什么?
- google-cloud-platform - Google Cloud - 将数据从存储桶下载到实例
- angular - 以编程方式更改 RadDataForm Switch 编辑器的状态
- jquery - Ajax 加载数据,完全打印整个表格,所有 DataTables 功能都不起作用
- node.js - NodeJS spawn `open` 实际上并没有打开文件
- networking - TCP 在后端 FIN、ACK 后仍然传输数据