Spring 之 @PropertySource 注解的使用
Spring 之 @PropertySource 注解的使用
1、简介
@ PropertySource 注解来加载指定配置文件,实现配置文件与Java Bean 类的注入。
注解使用场景
在基于Spring的注解开发项目的过程中,由于不再使用Spring的XML文件进行配置,如果将配置项直接写到类中,就会造成配置项与类的紧耦合,后续对于配置项的修改操作非常不方便,不利于项目的维护和扩展。此时,可以将这些配置项写到properties文件或者yml文件中,通过@PropertySource注解加载配置文件。另外,如果项目本身就存在大量的properties配置文件或者yml配置文件,也可以统一由Spring的@PropertySource注解进行加载。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {
//资源名称,为空则根据资源的描述符生成
String name() default "";
/**
*资源路径
*classpath:application.properties
*file:/
*/
String[] value();
//是否忽略资源不存在的情况,如果不忽略,当资源不存在时报错
boolean ignoreResourceNotFound() default false;
//指定资源文件的编码格式
String encoding() default "";
//资源工厂
Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}
2、@PropertySource读取.properties配置文件
person.properties 配置文件
我们新建一个 person.properties 配置文件,用来存放 Person 类的配置信息。接下来使用 @ PropertySource 注解,来实现通过读取该配置,实现配置文件与 Java Bean 类的注入操作。
person.id=111
person.name=爱学习啊
person.age=18
javaBean类
添加 @PropertySource(value = {“classpath:person.properties”})
注解,通过 value 属性让它去加载指定路径配置文件。代码如下:
@Component
@PropertySource(value = "classpath:person.properties",encoding = "UTF-8") //读取指定路径配置文件
public class Person {
private String id;
private String name;
private int age;
}
3、@PropertySource读取.yml配置文件
Spring Boot 默认不支持@PropertySource读取yaml 文件。
Spring 4.3 通过引入 PropertySourceFactory 接口使之成为可能。PropertySourceFactory 是PropertySource 的工厂类。默认实现是 DefaultPropertySourceFactory,可以构ResourcePropertySource 实例。
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.lang.Nullable;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
/**
* 自定义一个yml属性读取工厂类
*/
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
Properties propertiesFromYaml = loadYamlIntoProperties(resource);
String sourceName = name != null ? name : resource.getResource().getFilename();
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
}
private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {
try {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
} catch (IllegalStateException e) {
// for ignoreResourceNotFound
Throwable cause = e.getCause();
if (cause instanceof FileNotFoundException)
throw (FileNotFoundException) e.getCause();
throw e;
}
}
}
@PropertySource 注解有一个 factory
属性,通过这个属性来注入 PropertySourceFactory
,这里给出 YamlPropertySourceFactory
的例子。
@Component
@PropertySource(value = "classpath:person.yml", factory = YamlPropertySourceFactory.class, encoding = "UTF-8") //读取指定路径配置文件
public class Person {
private String id;
private String name;
private int age;
}