[SpringBoot源码分析三]:@PropertySource和@ConfigurationProperties

  1. 使用场景

    首先我们先了解下这两个注解的使用场景,看下他们到底是干什么的,下面就是他们的一个简单的例子,是为了将user.yml中以user前缀开头的值设置到当前这个对象中

    image.png

    虽然网上很多例子是从配置文件中读取属性的时候只使用了@ConfigurationProperties,但这是因为读取的这些属性都是在application.yml等默认注册到容器中的配置文件中的,而一旦我们的属性是在自定义的配置类文件,那就需要使用到@PropertySource

    1.1 @PropertySource

    @PropertySource:用于导入配置文件,比如说.yml文件

    public @interface PropertySource {
    
    String name() default "";
    
    /**
    * 指示要加载的属性文件的资源位置
    */
    String[] value();
    
    /**
    * 指示是否应该忽略查找属性资源失败
    */
    boolean ignoreResourceNotFound() default false;
    
    /**
    * 编码格式,例如:UTF-8
    */
    String encoding() default "";
    
    Class