java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring的@PropertySource注解

Spring中@PropertySource注解使用场景解析

作者:Super_Leng

这篇文章主要介绍了Spring中@PropertySource注解使用场景解析,@PropertySource注解就是Spring中提供的一个可以加载配置文件的注解,并且可以将配置文件中的内容存放到Spring的环境变量中,需要的朋友可以参考下

一、简单介绍

在日常开发中,你有没有遇到过这样一种场景:项目中需要编写很多配置文件,将一些系统信息配置化

此时,往往需要编写专门的工具类或者方法来读取并解析这些配置文件,将配置文件中的配置项内容加载到系统内存中。

后续在使用这些配置项时,可以直接通过工具类或者方法获取加载到内存中的配置项。

@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版本开始提供的注解,注解中各个属性的含义如下所示:

② @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的环境变量就可以获取到配置项的值。

到此这篇关于Spring中@PropertySource注解使用场景解析的文章就介绍到这了,更多相关Spring的@PropertySource注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文