@PropertySource和@ConfigurationProperties区别

简介: @PropertySource和@ConfigurationProperties区别

@ConfigurationProperties


@ConfigurationProperties是springboot中注解,用于将主配置文件(application.properties或者“application.yml” )中的属性,映射到实体类中对应的属性。 意思就是把主配置文件中配置属性设置到对应的Bean属性上。

常见使用方式:

  1. @ConfigurationProperties + @Component注解到bean定义类上
  2. @ConfigurationProperties + @Bean注解在配置类的bean定义方法上
  3. @ConfigurationProperties注解到普通类然后通过 @EnableConfigurationProperties定义为bean

@Bean,@Component这种我们可以理解,两个注解都能标识一个Bean.

@EnableConfigurationProperties是如何让其成为一个bean的呢?

@EnableConfigurationProperties源码分析:

@EnableConfigurationProperties注解会导入一个EnableConfigurationPropertiesImportSelector类,其selectImports方法返回的类,将注册为beandefinition。

@Override
  public String[] selectImports(AnnotationMetadata metadata) {
    获取@EnableConfigurationProperties的参数信息
    MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(
        EnableConfigurationProperties.class.getName(), false);
    Object[] type = attributes == null ? null
        : (Object[]) attributes.getFirst("value");
    参数没有值,注册1个
    if (type == null || type.length == 0) {
      return new String[] {
          ConfigurationPropertiesBindingPostProcessorRegistrar.class
              .getName() };
    }
    参数有值,注册两个
    return new String[] { ConfigurationPropertiesBeanRegistrar.class.getName(),
        ConfigurationPropertiesBindingPostProcessorRegistrar.class.getName() };
  }

总的来说会注册两个类:

  • ConfigurationPropertiesBindingPostProcessorRegistrar
  • ConfigurationPropertiesBeanRegistrar

1.ConfigurationPropertiesBindingPostProcessorRegistrar: 看名字我们也可以看出ConfigurationPropertiesBindingPostProcessor注册器,没错就是用来注册ConfigurationPropertiesBindingPostProcessor这个BeanPostProcessor的。BeanPostProcessor会在创建bean的时候,被执行处理Bean相关信息。ConfigurationPropertiesBindingPostProcessor这个BeanPostProcessor是就是用来解析Bean上的@ConfigurationProperties注解的。

ConfigurationPropertiesBindingPostProcessor会检查Bean是否有 @ConfigurationProperties,如果有就绑定相关配置属性到本bean的相关属性上。

2.ConfigurationPropertiesBeanRegistrar: 看名字,ConfigurationPropertiesBean注册器,ConfigurationPropertiesBeanRegistrar是在@ConfigurationProperties的value属性有值的情况下注册的。 例如:

@EnableConfigurationProperties({ EurekaDashboardProperties.class,
    InstanceRegistryProperties.class })

ConfigurationPropertiesBeanRegistrar实现了ImportBeanDefinitionRegistrar接口,所以他的registerBeanDefinitions方法,也会注册Bean. 注册就是@EnableConfigurationProperties的value值,例如例子中EurekaDashboardProperties.class,InstanceRegistryProperties.class

由此我们可以看出:@Component, @Bean , ConfigurationPropertiesBeanRegistrar都是为了告诉spring,对应的类是一个Bean,spring要去加载,解析,创建Bean. 而ConfigurationPropertiesBindingPostProcessor的工作就是完成配置信息到Bean属性的绑定工作。

@EnableConfigurationProperties的目的

  • 保证容器中有解析ConfigurationProperties注解的BeanPostProcessor
  • 向容器内注入value对应的BeanDefinition

idea中,在主配置文件ctrl+鼠标 能看到这对应的属性被哪个@ConfigurationProperties修饰的属性类使用

@ConfigurationProperties是针对主配置文件的。但是我们不想所有的配置都放到主配置文件中怎么办????


@PropertySource


@PropertySource 是spring的注解 加载指定的属性文件的配置到 Spring 的 Environment 中。可以配合 @Value 和 @ConfigurationProperties 使用。

用法:

  1. @Configuration+ @PropertySource+Environment
  2. @Configuration+ @PropertySource+@Value
  3. @Configuration+ @PropertySource+@ConfigurationProperties

@Configuration本质也是告诉spring这是一个bean.

my.name=helloworld
my.age=8

@Configuration+ @PropertySource+Environment

@Configuration
@PropertySource("classpath:hellword.properties")
public class HelloWorldConfig {
   @Autowired
    private Environment env;
}

@Configuration+ @PropertySource+@Value

@Configuration
@PropertySource("classpath:hellword.properties")
public class HelloWorldConfig {
  @Value(${my.name})
  private String name;
}

@Configuration+ @PropertySource+@ConfigurationProperties@PropertySource指定加载哪个文件,@ConfigurationProperties指定加载文件中的哪一类属性。@PropertySource+@ConfigurationProperties在一起解决了@ConfigurationProperties只能加载主文件内属性问题。

@Configuration
@PropertySource("classpath:hellword.properties")
@ConfigurationProperties(prefix = "my")
public class HelloWorldConfig {
  private String name;
}


总结:


灵活利用这两个注解,能够把配置信息安排有层次感。


相关文章
|
SQL 运维 关系型数据库
在OceanBase数据库中,你可以通过以下几个途径来查看慢SQL和等待事件
在OceanBase数据库中,你可以通过以下几个途径来查看慢SQL和等待事件
712 1
|
5月前
|
Arthas 监控 Java
Arthas vmtool(从 jvm 里查询对象,执行 forceGc)
Arthas vmtool(从 jvm 里查询对象,执行 forceGc)
343 16
|
8月前
|
机器学习/深度学习 数据可视化 算法
YOLOv9改进目录一览 | 涉及卷积层、轻量化、注意力、损失函数、Backbone、SPPF、Neck、检测头等全方位改进
YOLOv9改进目录一览 | 涉及卷积层、轻量化、注意力、损失函数、Backbone、SPPF、Neck、检测头等全方位改进
637 5
YOLOv9改进目录一览 | 涉及卷积层、轻量化、注意力、损失函数、Backbone、SPPF、Neck、检测头等全方位改进
|
设计模式 Java Spring
spring源码设计模式分析(六)-模板方法模式
spring源码设计模式分析(六)-模板方法模式
|
Shell
esp32入门笔记
这篇文章是关于ESP32 S3入门的笔记,包括了安装编译工具、下载ESP-IDF框架、设置工具和环境变量、以及烧录固件的步骤说明。
324 5
|
存储 Java 数据库连接
解锁Spring Boot的强大配置功能:@ConfigurationProperties与@PropertySources详解
解锁Spring Boot的强大配置功能:@ConfigurationProperties与@PropertySources详解
3533 0
|
安全 Java Spring
如何让 Spring Security 放行所有接口
如何让 Spring Security 放行所有接口
1032 0
|
安全 Java Maven
关于代码混淆,看这篇就够了
关于代码混淆,看这篇就够了
199 0
|
机器学习/深度学习 算法 数据可视化
数学建模资料整理
数学建模中有三类团队: 第一类:拿到题目,讨论,然后建模手开始建模,编程手开始处理数据,写作手开始写作。 第二类:拿到题目,团内大佬,开始建模,然后编程,然后写作。剩下两人负责探听别队的消息和带饭。 第三类:拿到题目,三个人一脸懵逼,不求同年同月同日生,但求同年同月同日死数学建模又称语文建模
659 0
|
Java 关系型数据库 MySQL
【从0配置JAVA项目相关环境1】jdk + VSCode运行java + mysql + Navicat + 数据库本地化 + 启动java项目
【从0配置JAVA项目相关环境1】jdk + VSCode运行java + mysql + Navicat + 数据库本地化 + 启动java项目
594 0