代码之家  ›  专栏  ›  技术社区  ›  Mike

@ConfigurationProperties不使用PropertySourcesPlaceholderConfigurer

  •  0
  • Mike  · 技术社区  · 7 年前

    PropertySourcesPlaceholderConfigurer 为我的工作 @Value

    @Bean
    @ConfigurationProperties(prefix = "datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
    

    我定制的 属性资源占位符配置程序

    0 回复  |  直到 7 年前
        1
  •  1
  •   LppEdd    7 年前

    默认情况下,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 豆子 @ConfigurationProperties 注释类

    @ConfigurationProperties(prefix = "spring.datasource")
    public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {
    

    问题是, 将1:1映射到属性文件,这是非常自以为是的。
    @Value 是另一种野兽。


    我在 this
    你可能会觉得它很有价值。