Spring注解系列——@PropertySource

打印 上一主题 下一主题

主题 529|帖子 529|积分 1587

在Spring框架中@PropertySource注解是非常常用的一个注解,其主要作用是将外部化配置解析成key-value键值对"存入"Spring容器的Environment环境中,以便在Spring应用中可以通过@Value或者占位符${key}的形式来使用这些配置。
使用案列
  1. // @PropertySource需要和@Configuration配个使用
  2. // @PropertySource加载的配置文件时需要注意加载的顺序,后面加载的配置会覆盖前面加载的配置
  3. // @PropertySource支持重复注解
  4. // value值不仅支持classpath表达式,还支持任意合法的URI表达式
  5. @Configuration
  6. @PropertySource(value = "classpath:/my.properties",encoding = "UTF8")
  7. @PropertySource(value = "classpath:/my2.properties",encoding = "UTF8",ignoreResourceNotFound = true)
  8. public static class PropertyConfig {
  9. }
  10. @Component
  11. public class App {
  12.     @Value("${key1:default-val}")
  13.     private String value;
  14.     @Value("${key2:default-val2}")
  15.     private String value2;
  16. }
复制代码
下面是配置文件my.properties和my2.properties的具体内容。
  1. # my.properties
  2. key1=自由之路
  3. # my2.properties
  4. key1=程序员
  5. key2=自由之路
复制代码
Spring容器启动时,会将my.properties和my2.properties的内容加载到Environment中,并在App类的依赖注入环节,将key1和key2的值注入到对应的属性。
自定义PropertySource工厂

阅读@PropertySource的源代码,我们发现还有一个factory属性。从这个属性的字面意思看,我们不难猜测出这个属性设置的是用于产生PropertySource的工厂。
  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Repeatable(PropertySources.class)
  5. public @interface PropertySource {
  6.         String name() default "";
  7.    
  8.         String[] value();
  9.        
  10.     boolean ignoreResourceNotFound() default false;
  11.         String encoding() default "";
  12.         Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
  13. }
复制代码
原理简析

到这边我们对@PropertySource已经有了一个感性的认识,知道了其主要作用是将各种类型的外部化配置文件以key-value的形式加载到Spring的Environment中。这个部分我们从源码的角度来分析下Spring是怎么处理@PropertySource这个注解的。分析源码可以加深我们对@PropertySource的认识(看源码不是目的,是为了加深理解,学习Spring的设计思想)。
@PropertySource注解的处理是在ConfigurationClassPostProcessor中进行触发的。最终会调用到ConfigurationClassParser的processPropertySource方法。
[code]// ConfigurationClassParser#processPropertySourceprivate void processPropertySource(AnnotationAttributes propertySource) throws IOException {    String name = propertySource.getString("name");    if (!StringUtils.hasLength(name)) {        name = null;    }    String encoding = propertySource.getString("encoding");    if (!StringUtils.hasLength(encoding)) {        encoding = null;    }    String[] locations = propertySource.getStringArray("value");    Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");    boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");    Class
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

耶耶耶耶耶

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表