@PropertySource使用场景

一、简单介绍

在日常开发中,你有没有遇到过这样一种场景:项目中需要编写很多配置文件,将一些系统信息配置化,此时,往往需要编写专门的工具类或者方法来读取并解析这些配置文件,将配置文件中的配置项内容加载到系统内存中。后续在使用这些配置项时,可以直接通过工具类或者方法获取加载到内存中的配置项。
@PropertySource注解就是Spring中提供的一个可以加载配置文件的注解,并且可以将配置文件中的内容存放到Spring的环境变量中。

二、注解说明

@PropertySource注解是Spring中提供的一个通过指定配置文件位置来加载配置文件的注解,并且可以将配置文件中的内容存放到Spring的环境变量中。除了可以通过Spring的环境变量读取配置项之外,还可以通过@Value注解获取配置项的值。
另外,Spring中还提供了一个@PropertySources注解,在@PropertySources注解中,可以引入多个@PropertySource注解。

1. 注解源码

Spring中提供了@PropertySource和@PropertySources两个注解来加载配置文件。

① @PropertySource注解

@PropertySource注解只能标注到类上,能够通过指定配置文件的位置来加载配置文件,@PropertySource注解除了可以加载properties配置文件外,也可以加载xml配置文件和yml配置文件。如果加载yml配置文件时,可以自定义PropertySourceFactory实现yml配置文件的解析操作。

@PropertySource注解的源码详见:

/**
 * @author Chris Beams
 * @author Juergen Hoeller
 * @author Phillip Webb
 * @author Sam Brannen
 * @since 3.1
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {

 String name() default "";
 String[] value();
 /**
  * @since 4.0
  */
 boolean ignoreResourceNotFound() default false;
 /**
  * @since 4.3
  */
 String encoding() default "";
 /**
  * @since 4.3
  */
 Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}

从源码可以看出,@PropertySource注解是从Spring3.1版本开始提供的注解,注解中各个属性的含义如下所示:

  • name:表示加载的资源的名称,如果为空,则会根据加载的配置文件自动生成一个名称。
  • value:表示加载的资源的路径,这个路径可以是类路径,也可以是文件路径。
  • ignoreResourceNotFound:表示当配置文件未找到时,是否忽略文件未找到的错误。默认值为false,也就是说当未找到配置文件时,Spring启动就会报错。
  • encoding:表示解析配置文件使用的字符集编码。
    factory:表示读取对应配置文件的工厂类,默认的工厂类是PropertySourceFactory。

② @PropertySources注解

除了@PropertySource注解,Spring中还提供了一个@PropertySources注解。

/**
 * @author Phillip Webb
 * @since 4.0
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PropertySources {
 PropertySource[] value();
}

从源码可以看出,@PropertySources是从Spring4.0版本开始提供的注解,在@PropertySources注解中,只提供了一个PropertySource数组类型的value属性。所以,@PropertySources注解可以引入多个@PropertySource注解。

2. 注解使用场景

  • 在基于Spring的注解开发项目的过程中,由于不再使用Spring的XML文件进行配置,如果将配置项直接写到类中,就会造成配置项与类的紧耦合,后续对于配置项的修改操作非常不方便,不利于项目的维护和扩展。此时,可以将这些配置项写到properties文件或者yml文件中,通过@PropertySource注解加载配置文件。
  • 另外,如果项目本身就存在大量的properties配置文件或者yml配置文件,也可以统一由Spring的@PropertySource注解进行加载。

3. 使用案例

本节,主要实现一个通过@PropertySource注解加载properties配置文件,将properties配置文件中的配置项加载到Spring的环境变量中,获取Spring环境变量中配置项的值,并进行打印。案例的具体实现步骤如下所示。

(1)新增test.properties文件

resources目录下新增test.properties文件,文件内容如下所示:

name=lwk
age=18

(2)新增PropertySourceConfig类

@Configuration
@PropertySource(value = "classpath:test.properties")
public class PropertySourceConfig {
}

可以看到,PropertySourceConfig类是Spring的配置类,并且使用@PropertySource注解指定了test.properties配置文件的路径。

(3)新增PropertySourceTest类

public class PropertySourceTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PropertySourceConfig.class);
        ConfigurableEnvironment environment = context.getEnvironment();
        System.out.println(environment.getProperty("name") + " ====>>> " + environment.getProperty("age"));
    }
}

可以看到,在PropertySourceTest类的main()方法中,通过AnnotationConfigApplicationContext类的对象获取到ConfigurableEnvironment类型的环境变量对象environment,然后通过environment对象获取配置文件中的name和age的值并进行打印。

(4)运行PropertySourceTest类

可以看到,正确的输出了配置文件中的值。

lwk ====>>> 18

说明:使用@PropertySource注解可以加载properties配置文件中的配置项,并将配置项加载到Spring的环境变量中,通过Spring的环境变量就可以获取到配置项的值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值