内容简介:Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口。这三种注解可以配合着@PropertySource来使用,@PropertySource主要是用来指定具体的配置文件。新建两个配置文件config.properties和configs.properties,分别写入如下内容:新增一个类用来读取配置文件
Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口。这三种注解可以配合着@PropertySource来使用,@PropertySource主要是用来指定具体的配置文件。
@PropertySource解析
@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;
}
- value():指定配置文件
- encoding():指定编码,因为properties文件的编码默认是ios8859-1,读取出来是乱码
- factory():自定义解析文件类型,因为该注解默认只会加载properties文件,如果想要指定yml等其他格式的文件需要自定义实现。
一、@Value注解读取文件
新建两个配置文件config.properties和configs.properties,分别写入如下内容:
zhbin.config.web-configs.name=Java旅途 zhbin.config.web-configs.age=22
zhbin.config.web-configs.name=Java旅途 zhbin.config.web-configs.age=18
新增一个类用来读取配置文件
@Configuration
@PropertySource(value = {"classpath:config.properties"},encoding="gbk")
public class GetProperties {
@Value("${zhbin.config.web-configs.name}")
private String name;
@Value("${zhbin.config.web-configs.age}")
private String age;
public String getConfig() {
return name+"-----"+age;
}
}
如果想要读取yml文件,则我们需要重写DefaultPropertySourceFactory,让其加载yml文件,然后在注解
@PropertySource上自定factory。代码如下:
public class YmlConfigFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
String sourceName = name != null ? name : resource.getResource().getFilename();
if (!resource.getResource().exists()) {
return new PropertiesPropertySource(sourceName, new Properties());
} else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
Properties propertiesFromYaml = loadYml(resource);
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
} else {
return super.createPropertySource(name, resource);
}
}
private Properties loadYml(EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
}
}
@PropertySource(value = {"classpath:config.properties"},encoding="gbk",factory = YmlConfigFactory.class)
二、Environment读取文件
配置文件我们继续用上面的两个,定义一个类去读取配置文件
@Configuration
@PropertySource(value = {"classpath:config.properties"},encoding="gbk")
public class GetProperties {
@Autowired
Environment environment;
public String getEnvConfig(){
String name = environment.getProperty("zhbin.config.web-configs.name");
String age = environment.getProperty("zhbin.config.web-configs.age");
return name+"-----"+age;
}
}
三、@ConfigurationProperties读取配置文件
@ConfigurationProperties可以将配置文件直接映射成一个实体类,然后我们可以直接操作实体类来获取配置文件相关数据。
新建一个yml文件,当然properties文件也没问题
zhbin:
config:
web-configs:
name: Java旅途
age: 20
新建实体类用来映射该配置
@Component
@ConfigurationProperties(prefix = "zhbin.config")
@Data
public class StudentYml {
// webConfigs务必与配置文件相对应,写为驼峰命名方式
private WebConfigs webConfigs = new WebConfigs();
@Data
public static class WebConfigs {
private String name;
private String age;
}
}
- prefix = "zhbin.config"用来指定配置文件前缀
如果需要获取list集合,则做以下修改即可。
zhbin:
config:
web-configs:
- name: Java旅途
age: 20
- name: Java旅途2
age: 202
@Component
@ConfigurationProperties(prefix = "zhbin.config")
@Data
public class StudentYml {
private List<WebConfigs> webConfigs = new ArrayList<>();
@Data
public static class WebConfigs {
private String name;
private String age;
}
}
经验与坑
- properties文件默认使用的是iso8859-1,并且不可修改
- yml文件的加载顺序高于properties,但是读取配置信息的时候会读取后加载的
- @PropertySource注解默认只会加载properties文件
- @PropertySource注解可以与任何一种方式联合使用
- 简单值推荐使用@Value,复杂对象推荐使用@ConfigurationProperties
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 使用go读取配置文件
- Spring Boot 2 实战:常用读取配置的方式
- 原创 Wagon部署springboot项目读取配置文件错误问题
- Spring-boot读取properties/yaml配置文件
- Jboot 2.2.3 发布,完善文档并新增对加密配置内容读取的支持
- Java怎么从这四个位置读取配置文件Properties(普通文件系统-classpath-jar-URL)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
计算机组成:结构化方法
坦嫩鲍姆 / 刘卫东 / 人民邮电出版社 / 2006-1 / 65.00元
本书采用结构化方法来介绍计算机系统,书的内容完全建立在“计算机是由层次结构组成的,每层完成规定的功能”这一概念之上。作者对本版进行了彻底的更新,以反映当今最重要的计算机技术以及计算机组成和体系结构方面的最新进展。书中详细讨论了数字逻辑层、微体系结构层、指令系统层、操作系统层和汇编语言层,并涵盖了并行体系结构的内容,而且每一章结尾都配有丰富的习题。 本书适合作为计算机专业本科生计算机组......一起来看看 《计算机组成:结构化方法》 这本书的介绍吧!