一、使用@Value注解读取properties和yml的值
- properties
userproperties.name=gongj===properties
userproperties.age=11===properties
userproperties.sex=12===properties
userproperties.email=3333@163.com===properties
- yml
useryml:
name: gongjie
age: 23
sex: 1
email: 111@163.com
测试编码
@Value("${userproperties.name}")
private String name;
@Value("${userproperties.age}")
private String age;
@Value("${userproperties.sex}")
private String sex;
@Value("${userproperties.email}")
private String email;
@Value("${useryml.name}")
private String nameyml;
@Value("${useryml.age}")
private String ageyml;
@Value("${useryml.sex}")
private String sexyml;
@Value("${useryml.email}")
private String emailyml;
@Test
public void testValue(){
System.out.println("name=" + name + ",age=" + age + ",sex=" + sex + ",email" + email);
System.out.println("nameyml=" + nameyml + ",ageyml=" + ageyml + ",sexyml=" + sexyml + ",emailyml" + emailyml);
}
image.png
二、使用@ConfigurationProperties注解
注意:需要在启动类上面添加注解@EnableConfigurationProperties
编码测试
- properties
/**
* @program: demo
* @description:读取配置文件properties的值
* @author: gongj
* @Description: TODO
* @create: 2020-07-18 12:59
**/
@Component
@ConfigurationProperties(prefix = "userproperties")
public class ReadPropertiesValue {
private String name;
private String age;
private String email;
private String sex;
@Override
public String toString() {
return "ReadPropertiesValue{" +
"name='" + name + '\'' +
", age=" + age +
", email='" + email + '\'' +
", sex=" + sex +
'}';
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void setEmail(String email) {
this.email = email;
}
public void setSex(String sex) {
this.sex = sex;
}
}
- yml
/**
* @program: demo
* @description:读取配置文件yml的值
* @author: gongj
* @Description: TODO
* @create: 2020-07-17 18:34
**/
@Component
@ConfigurationProperties(prefix = "useryml")
public class ReadYmlValue {
private String name;
private String age;
private String email;
private String sex;
@Override
public String toString() {
return "ReadYmlValue{" +
"name='" + name + '\'' +
", age=" + age +
", email='" + email + '\'' +
", sex=" + sex +
'}';
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void setEmail(String email) {
this.email = email;
}
public void setSex(String sex) {
this.sex = sex;
}
}
@Autowired
ReadPropertiesValue readPropertiesValue;
@Autowired
ReadYmlValue readYmlValue;
@Test
public void readPropertiesValueTest(){
System.out.println(readPropertiesValue.toString());
System.out.println(readYmlValue.toString());
}
image.png
三、使用Environment
编码测试
@Autowired
Environment environment;
@Test
public void environmentTest(){
System.out.println(environment.getProperty("userproperties.name") + "==" + environment.getProperty("userproperties.age") + "==" + environment.getProperty("userproperties.sex") + "==" + environment.getProperty("userproperties.email"));
System.out.println(environment.getProperty("useryml.name") + "==" + environment.getProperty("useryml.age") + "==" + environment.getProperty("useryml.sex") + "==" + environment.getProperty("useryml.email"));
}
image.png
上面几种方式都是加载的默认配置文件
application.properties
与application.yml
。但在公司的正式开发肯定不可能就这几个文件。那这时候就要使用@PropertySource来指定加载配置文件。
新建
configValue.properties
与configValue.yml
配置文件
- configValue.properties
userconfigvalueproperties.name=yuanjConfigValue
userconfigvalueproperties.age=11ConfigValue
userconfigvalueproperties.sex=12ConfigValue
userconfigvalueproperties.email=3333@163.comConfigValue
- yml
userconfigvalueyml:
name: yuanjconfigvalueyml
age: 11configvalueyml
sex: 12configvalueyml
email: 3333@163.comconfigvalueyml
一、@PropertySource + @Value
编码测试
- properties
@Component
@PropertySource(value = "classpath:configValue.properties")
public class ReadPropertiesByValue {
@Value("${userconfigvalueproperties.name}")
public String name;
@Value("${userconfigvalueproperties.age}")
public String age;
@Value("${userconfigvalueproperties.sex}")
public String sex;
@Value("${userconfigvalueproperties.email}")
public String email;
@Override
public String toString() {
return "ReadPropertiesByValue{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", sex='" + sex + '\'' +
", email='" + email + '\'' +
'}';
}
}
- yml
@Component
@PropertySource(value = "classpath:configValue.yml")
public class ReadYmlByValue {
@Value("${userconfigvalueyml.name}")
public String name;
@Value("${userconfigvalueyml.age}")
public String age;
@Value("${userconfigvalueyml.sex}")
public String sex;
@Value("${userconfigvalueyml.email}")
public String email;
@Override
public String toString() {
return "ReadYmlByValue{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", sex='" + sex + '\'' +
", email='" + email + '\'' +
'}';
}
}
@Autowired
ReadPropertiesByValue readPropertiesByValue;
@Autowired
ReadYmlByValue readYmlByValue;
@Test
public void readPropertiesByValue(){
System.out.println(readPropertiesByValue.toString());
System.out.println(readYmlByValue.toString());
}
image.png
二、@PropertySource + @ConfigurationProperties
编码测试
- properties
@Component
@ConfigurationProperties(prefix = "userconfigvalueproperties")
@PropertySource(value = { "classpath:configValue.properties" })
public class ReadPropertiesConfigValue {
private String name;
private String age;
private String email;
private String sex;
@Override
public String toString() {
return "ReadPropertiesConfigValue{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", email='" + email + '\'' +
", sex='" + sex + '\'' +
'}';
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void setEmail(String email) {
this.email = email;
}
public void setSex(String sex) {
this.sex = sex;
}
}
- yml
@Component
@ConfigurationProperties(prefix = "userconfigvalueyml")
@PropertySource(value = { "classpath:configValue.yml" }, factory = CommPropertyResourceFactory.class)
public class ReadYmlConfigValue {
private String name;
private String age;
private String email;
private String sex;
@Override
public String toString() {
return "ReadYmlConfigValue{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", email='" + email + '\'' +
", sex='" + sex + '\'' +
'}';
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void setEmail(String email) {
this.email = email;
}
public void setSex(String sex) {
this.sex = sex;
}
}
由于@PropertySource默认不支持yaml读取,所以我们需要改造一下。
public class CommPropertyResourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
String resourceName = Optional.ofNullable(name).orElse(resource.getResource().getFilename());
if (resourceName.endsWith(".yml") || resourceName.endsWith(".yaml")) {
List<PropertySource<?>> yamlSources = new YamlPropertySourceLoader().load(resourceName, resource.getResource());
return yamlSources.get(0);
} else {
return new DefaultPropertySourceFactory().createPropertySource(name, resource);
}
}
}
在需要读取yml的时候,要改一下factory参数
@PropertySource(value = { "classpath:configValue.yml" }, factory = CommPropertyResourceFactory.class)
//===========指定配置文件加载
@Autowired
ReadPropertiesConfigValue readPropertiesConfigValue;
@Autowired
ReadYmlConfigValue readYmlConfigValue;
@Test
public void readPropertiesConfigValueTest(){
System.out.println(readPropertiesConfigValue.toString());
System.out.println(readYmlConfigValue.toString());
}