@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;
}


总结:


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


相关文章
|
6月前
|
Java 开发者 Spring
@ConfigurationProperties
@ConfigurationProperties
|
Java 程序员 数据库
【小家Spring】Spring中读取配置的方式,@Value、@PropertySource、@ConfigurationProperties使用详解(下)
【小家Spring】Spring中读取配置的方式,@Value、@PropertySource、@ConfigurationProperties使用详解(下)
【小家Spring】Spring中读取配置的方式,@Value、@PropertySource、@ConfigurationProperties使用详解(下)
|
XML Dubbo Java
三、那些高曝光的Annotation(@ComponentScan、@PropertySource与@PropertySources、@Import与ImportResource)
我们可以通过basePackages等属性来细粒度地定制@ComponentScan自动扫描的范围,如果不指定,则默认Spring框架实现会从声明@ComponentScan所在类的package进行扫描。
72 0
|
Java Spring
@ConfigurationProperties注解的理解和使用
@ConfigurationProperties注解的理解和使用
一文深入了解ConfigurationProperties注解
一文深入了解ConfigurationProperties注解
327 0
一文深入了解ConfigurationProperties注解
|
Java 微服务 Spring
Spring中@Value和@ConfigurationProperties区别
Spring中@Value和@ConfigurationProperties区别
176 0
Spring中@Value和@ConfigurationProperties区别
|
Java Spring 容器
spring学习笔记(一)@ConfigurationProperties注解
spring学习笔记(一)@ConfigurationProperties注解
110 0
|
Java 开发者 Spring
@PropertySource、@ImportResource、@Bean | 学习笔记
快速学习@PropertySource、@ImportResource、@Bean
94 0
|
Java 数据库 开发者
@ConfigurationProperties 与 @Value区别 | 学习笔记
快速学习 @ConfigurationProperties 与 @Value 区别
129 0
@ConfigurationProperties 与 @Value区别 | 学习笔记
|
开发框架 Java Spring
Spring - 属性注入之注解(@Autowired、@Qualifier、@Resource)
Spring - 属性注入之注解(@Autowired、@Qualifier、@Resource)
218 0
Spring - 属性注入之注解(@Autowired、@Qualifier、@Resource)