一:pom.xml文件添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
二:創建自定義配置文件 person.properties
內容:
person.age=18
person.name=名字
person.birth=1994
三:創建配置文件綁定對象類 Person
/**
*@ClassName Person
*@Description @PropertySource 這個註解表示讀取項目環境下的person.properties 文件
* 而這個註解@ConfigurationProperties 表示 映射到前綴是person開頭的配置
* 如果沒有設置 @PropertySource註解,則默認讀取spring boot 的默認配置文件 application.properties
* 而且@PropertySource註解 不支持.yml 類型的配置文件
*@Author Ni Klaus
*@Date 2019/10/5 0005 上午 11:26
*@Version 1.0
*/
@PropertySource(value = {"classpath:person.properties"})
@ConfigurationProperties(prefix = "person")
@Component
@Data
public class Person {
private String name;
private int age;
private String birth;
}
四:用spring boot 的單元測試類進行測試
測試結果:
拓展:
person.yml內容
person:
age: 19
name: 名字2
birth: 1996
@PropertySource註解 不支持.yml 類型的配置文件
@PropertySource 不支持yml文件的對象轉換,原因如下,看源碼:他的默認構造工廠是PropertySourceFactory
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {
String name() default "";
String[] value();
boolean ignoreResourceNotFound() default false;
String encoding() default "";
Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}
而PropertySourceFactory默認就一個實現:實現properties配置文件的加載解析
public class DefaultPropertySourceFactory implements PropertySourceFactory {
public DefaultPropertySourceFactory() {
}
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
return name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource);
}
}
所以,我們只要實現一個yml文件的工廠類就可以了
代碼實現:
package com.sgl.springbootconfig.yaml;
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;
/**
*@ClassName YamlPropertySourceFactory
*@Description TODO
*@Author Ni Klaus
*@Date 2019/10/8 0008 下午 17:53
*@Version 1.0
*/
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 = YamlPropertySourceFactory.class, value = "classpath:person.yml")
@ConfigurationProperties(prefix = "person")
@Component
@Data
測試結果:
親測成功!!!
參考地址:https://mdeinum.github.io/2018-07-04-PropertySource-with-yaml-files/