• 【SpringBoot】理解配置背后的 PropertySource 抽象


    PropertySource

    添加 PropertySource

    • PropertySourcesPlaceholderConfigurer

      • PropertyPlaceholderConfigurer
    • @PropertySource
    • @PropertySources

    Spring Boot 中的 @ConfigurationProperties

    • 可以将属性绑定到结构化对象上
    • ⽀持 Relaxed Binding
    • ⽀持安全的类型转换
    • @EnableConfigurationProperties

    定制 PropertySource

    主要步骤

    • 实现 PropertySource
    • 从 Environment 取得 PropertySources
    • 将⾃⼰的 PropertySource 添加到合适的位置

    切⼊位置

    • EnvironmentPostProcessor
    • BeanFactoryPostProcessor

    @PropertySource

    官方文档地址:https://docs.spring.io/spring-framework/docs/5.3.23/javadoc-api/org/springframework/context/annotation/PropertySource.html

    注解为向Spring的 Environment中添加PropertySource提供了一种方便的声明性机制。与@Configuration类一起使用。

    使用demo

    给定一个包含键/值对testbean.name=myTestBean的文件app.properties,下面的@Configuration类使用@PropertySourceapp.properties添加到Environment的propertysources集合。

     @Configuration
     @PropertySource("classpath:/com/myco/app.properties")
     public class AppConfig {
    
         @Autowired
         Environment env;
    
         @Bean
         public TestBean testBean() {
             TestBean testBean = new TestBean();
             testBean.setName(env.getProperty("testbean.name"));
             return testBean;
         }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    注意,Environment对象@Autowired到配置类中,然后在填充TestBean对象时使用。根据上面的配置,对testBean.getName()的调用将返回“myTestBean”。

    解析和@Value注解中的${…}占位符

    为了解析出和@Value注解中的${…}占位符,以使用PropertySource中的配置,必须确保在ApplicationContext使用的BeanFactory中注册了适当的嵌入式值解析器(an appropriate embedded value resolver)。当在XML中使用context:property-placeholder时,会自动进行注册。

    在使用@Configuration类时,可以通过静态@Bean方法显式注册PropertySourcesPlaceholderConfigurer来实现这一点。但是请注意,通过静态@Bean方法显式注册PropertySourcesPlaceholderConfigurer通常只有在需要定制配置(如占位符语法等)时才需要。可参见@Configuration的javadocs的“使用外部化值”部分和@Bean的javadocs的“关于beanfactorypostprocessor -返回@Bean方法的说明”了解详细信息和示例。

    解析@PropertySource资源位置中的${…}占位符

    任何${…}占位符出现在@PropertySource资源位置中,将根据已经在环境中注册的property sources进行解析。例如:

     @Configuration
     @PropertySource("classpath:/com/${my.placeholder:default/path}/app.properties")
     public class AppConfig {
    
         @Autowired
         Environment env;
    
         @Bean
         public TestBean testBean() {
             TestBean testBean = new TestBean();
             testBean.setName(env.getProperty("testbean.name"));
             return testBean;
         }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    假设“我的。”my.placeholder”出现在已经注册的property sources中——例如,系统属性或环境变量——占位符将被解析为相应的值。如果不是,那么"default/path"将被用作默认值。默认值(由冒号“:”分隔)是可选的。如果没有指定默认值且属性无法解析,则会抛出IllegalArgumentException异常。

    关于使用@PropertySource重写属性的说明

    在给定的属性键存在于多个.properties文件中的情况下,处理的最后一个@PropertySource注解将“胜出”并覆盖之前的同名键。

    例如,给定两个属性文件a.propertiesb.properties,考虑以下两个使用@PropertySource注解引用它们的配置类:

     @Configuration
     @PropertySource("classpath:/com/myco/a.properties")
     public class ConfigA { }
    
     @Configuration
     @PropertySource("classpath:/com/myco/b.properties")
     public class ConfigB { }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    覆盖顺序取决于这些类在应用程序上下文中注册的顺序。

     AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
     ctx.register(ConfigA.class);
     ctx.register(ConfigB.class);
     ctx.refresh();
    
    • 1
    • 2
    • 3
    • 4

    在上面的场景中,b.properties中的属性将覆盖a.properties中存在的任何副本,因为ConfigB是最后注册的。

    在某些情况下,在使用@PropertySource注解时,严格控制属性源顺序可能是不可能或不实际的。例如,如果上面的@Configuration类是通过组件扫描注册的,那么排序就很难预测。在这种情况下——如果重写很重要——建议用户回到使用编程的PropertySource API。详细信息可参见ConfigurableEnvironmentMutablePropertySources 的javadocs。

    注意:根据Java 8约定,该注解是可重复的。但是,所有这样的@PropertySource注解都需要在同一层声明:要么直接在配置类上声明,要么作为元注解在相同的自定义注解上声明。不建议混合使用直接注解和元注解,因为直接注解将有效地覆盖元注解。

  • 相关阅读:
    idea 配置ssm项目后配置文件的简要解析及功能类之间的联系
    AI 技术创意图像编辑器:Luminar Neo Mac
    217页12万字某智慧城市政务云平台项目建设方案2022年
    SpringBoot集成Kafka
    SSM学习——bean实例化(2)
    基础知识——进制 与 进制转换 (C++ 程序)
    Spring系列25:Spring AOP 切点详解
    Scala重要知识
    【算法挨揍日记】day16——525. 连续数组、1314. 矩阵区域和
    还在拼冗长的WhereIf吗?100行代码解放这个操作
  • 原文地址:https://blog.csdn.net/qq_46128473/article/details/128010008