日积月累,水滴石穿 😄
前言
在 SpringBoot
中呢,我们经常要读取配置文件的值,那读取配置文件的值有哪几种方式呢?跟着笔者一起来看看吧!
一、使用@Value注解
先准备一下两个配置文件,名称为:application.properties
和application.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);
}
二、使用@ConfigurationProperties注解
- properties
@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;
}
}
prefix
:指定配置文件中键名称的前缀。
注意:ReadPropertiesValue
类上如果不用@Component
修饰。则在容器无法获取。需要在启动类上加@EnableConfigurationProperties(ReadPropertiesValue.class)
将其注册到容器中。
- yml
@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());
}
三、使用Environment
注意:这种方式是直接依赖注入Evnironment
对象,然后使用environment.getProperty("键名")
即可读取对应的值。
- 编码测试
@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"));
}
上面几种方式都是读取默认配置文件application.properties
与application.yml
。但在公司的正式开发中肯定不可能就这两个文件。一般都会有自定义的配置文件,这时候就要指定加载配置文件了。
读取自定义配置文件
创建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 + '\'' +
'}';
}
}
@PropertySource
注解中配置了一个属性,value
:指定配置文件的位置。
- 读取 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());
}
二、使用@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
读取的,所以我们需要改造一下。
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.lang.Nullable;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
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<org.springframework.core.env.PropertySource<?>> yamlSources =
new YamlPropertySourceLoader().load(resourceName, resource.getResource());
return yamlSources.get(0);
} else {
return new DefaultPropertySourceFactory()
.createPropertySource(name, resource);
}
}
}
在需要读取yaml
的时候,要增加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());
}
- 如你对本文有疑问或本文有错误之处,欢迎评论留言指出。如觉得本文对你有所帮助,欢迎点赞和关注。