本文讲解到的知识点是基于Springboot 2.1.5.RELEASE的版本。
首先先在resources目录下创建一个测试用的yml配置文件,内容如下:
PS:注意yml文件中key: value的冒号后面是有一个空格的
system:
user:
name: zjhuang
password: 123456
age: 25
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = "classpath:test.yml")
public class YamlProperties {
@Value("${system.user.name}")
private String name;
@Value("${system.user.password}")
private String password;
@Value("${system.user.age}")
private int age;
// Setter...
// Getter...
// toString...
}
后面的两种方式也共用这个测试用例代码,下面不再列出。
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestYamlLoader {
@Autowired
private YamlProperties yamlProperties;
@Test
public void test() {
System.out.println(yamlProperties.toString());
}
}
YamlProperties{name='zjhuang', password='123456', age=25}
添加了@ConfigurationProperties注解,并且设置了prefix
前缀属性的值,这样@Value只需要指定参数名称,省略了前缀路径。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = "classpath:test.yml")
@ConfigurationProperties(prefix = "system.user")
public class YamlProperties {
@Value("${name}")
private String name;
@Value("${password}")
private String password;
@Value("${age}")
private int age;
// Setter...
// Getter...
// toString...
}
YamlProperties{name='zjhuang', password='123456', age=25}
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
@Component
public class PropertySourcesPlaceholderConfigurerBean {
@Bean
public PropertySourcesPlaceholderConfigurer yaml() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("test.yml"));
configurer.setProperties(yaml.getObject());
return configurer;
}
}
这第三种方法去掉了@PropertySource 注解,改为定义一个PropertySourcesPlaceholderConfigurer类型的@Bean来加载配置文件信息。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
@Component
public class YamlProperties {
@Value("${system.user.name}")
private String name;
@Value("${system.user.password}")
private String password;
@Value("${system.user.age}")
private int age;
// Setter...
// Getter...
// toString...
}
YamlProperties{name='zjhuang', password='123456', age=25}
总结
以上就是springboot加载自定义yml配置信息的三种方法,大家按照自己喜好选择使用即可;第三种方法好像只能用于加载一个yml文件的情况,第一和第二种方法可以实现加载多个yml文件,因为@PropertySources的value参数是支持数组赋值的。
@PropertySource(value = {"classpath:test1.yml", "classpath:test2.yml"})
另外需要提一点的是,不管是加载几个yml文件,@Value修饰的所有参数都必须在yml文件中定义齐全,yml缺少配置的话会抛出无法解析占位符的异常。
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'age' in value "${age}"