@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");
		参数没有值,注册1if (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;
}

总结:

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

  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值