Spring Boot系列之读取配置
使用SpringBoot框架开发,读取配置是少不了的,那么你会读取配置吗?你会写配置吗?List?Map?
1 目的
本节我们要解决如下几个问题:
- 如何使用Spring Boot读取配置文件?有哪些方式?
- 常用的几种数据结构,如字符串、整数、List、Map,如何配置?如何读取?
- 如何自定义配置文件的路径?
2 读配置文件
<br />Spring Boot默认的配置文件有两种格式: application.properties
和 application.yml
。
查找顺序是首先从application.properties
查找,如果找不到,再查找 application.yml
。<br />
<br />优先级:application.properties > application.yml
。
2.1 使用 @Value 读取配置
配置如下:
erwin.name=冯文议 erwin.age=20 erwin.sex=男 erwin.english-name=Erwin Feng erwin.birthday=1992/02/26 erwin.like=movie,game,music,tea,travel erwin.visitedCities=巴中,揭阳,广州,从化,成都,三亚,上海,杭州,北京 erwin.moreOther={myWeb:'https://fengwenyi.com',github:'https://github.com/fengwenyi'}
代码如下:
package com.fengwenyi.spring_boot_config_sample.config; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import java.util.*; /** * @author Erwin Feng * @since 2020/8/11 */ @Data @Configuration public class ReadConfigByValue { @Value("${erwin.name}") private String name; @Value("${erwin.age}") private Integer age; @Value("${erwin.sex}") private String sex; @Value("${erwin.english-name}") private String englishName; @Value("${erwin.birthday}") private Date birthday; @Value("${erwin.like}") private List<String> likes; @Value(&qu ffa ot;#{'${erwin.visitedCities}'.split(',')}") private List<String> visitedCities; @Value("#{${erwin.moreOther}}") private Map<String, String> moreOther; }
<br />
2.2 使用 @ConfigurationProperties 读取配置
配置如下(properties格式)
author.name=冯文议 author.age=20 author.sex=男 author.english-name=Erwin Feng author.birthday=1992/02/26 author.like[0]=movie author.like[1]=game author.like[2]=music author.like[3]=tea author.like[4]=travel author.visitedCities=巴中,揭阳,广州,从化,成都,三亚,上海,杭州,北京 author.moreOther.myWeb=https://fengwenyi.com author.moreOther.github=https://github.com/fengwenyi
配置如下(yaml格式)
author: name: 冯文议-yml age: 20 sex: 男 english-name: Erwin Feng birthday: 1992/02/26 like: - movie - game - music - tea - travel visitedCities: 巴中,揭阳,广州,从化,成都,三亚,上海,杭州,北京 moreOther: myWeb: https://fengwenyi.com github: https://github.com/fengwenyi
<br />代码如下:
package com.fengwenyi.spring_boot_config_sample.config; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import java.io.Serializable; import java.util.Date; import java.util.List; import java.util.Map; /** * @author Erwin Feng * @since 2020/8/12 */ @Data @Configuration @ConfigurationProperties(prefix = "author") public class AuthorConfig implements Serializable { private static final long serialVersionUID = 9032405467573421607L; private String name; private Integer age; private String sex; private String englishName; @JsonFormat(pattern = "yyyy/MM/dd") private Date birthday; private List<String> like; private List<String> visitedCities; private Map<String, String> moreOther; }
<br />读取出来的数据展示:
{ "name":"冯文议", "age":20, "englishName":"Erwin Feng", "birthday":"1992/02/26", "likes":[ "movie", "game", "music", "tea", "travel" ], "visitedCities":[ "巴中", "揭阳", "广州", "从化", "成都", "三亚", "上海", "杭州", "北京" ], "moreOther":{ "myWeb":"https://fengwenyi.com", "github":"https://github.com/fengwenyi" } }
2.3 通过注入环境变量来获取配置信息
@Autowired private Environment environment;
3 @Value 用法
3.1 设置默认值
格式:@Value("${name:defaultValue}")
当配置文件找不到这个配置时,就会返回默认值,如果没有默认值,就会报错。
3.2 可以直接读取系统的属性值
如:@Value("${java.home}")
D:\Java\Java8\jdk1.8.0_251\jre
3.3 可以用在方法和参数上,当做单元测试
// 单元测试-读取配置文件的值 @Value("${erwin.like}") public void testReadLike(String like, @Value("${erwin.sex}") String sex) { System.out.println("1===>" + like); System.out.println("1===>" 1000 + sex); }
参数 like 会取 @Value("${erwin.like}") 的值 参数 sex 会取 @Value("${erwin.sex}") 的值 经过测试,多个方法,执行顺序是随机。 特别注意:这个只是在启动的时候执行,但是实际调用的时候,还是传入的值。
3.4 读取list的值
方法一:<br />配置文件:
erwin.like=movie,game,music,tea,travel
<br />Java代码:
@Value("${erwin.like}") private List<String> likes;
方法二:
erwin.visitedCities=巴中,揭阳,广州,从化,成都,三亚,上海,杭州,北京
Java代码:
@Value("#{'${erwin.visitedCities}'.split(',')}") private List<String> visitedCities;
3.5 读取Map的值
配置文件:
erwin.moreOther={myWeb:'https://fengwenyi.com',github:'https://github.com/fengwenyi'}
Java代码:
@Value("#{${erwin.moreOther}}") private Map<String, String> moreOther;
<br />
3.6 读取系统属性
@Value("#{systemProperties}") private Map<String, String> systemPropertiesMap;
3.7 给私有属性赋值
@Component @PropertySource("classpath:values.properties") public class PriorityProvider { private String priority; @Autowired public PriorityProvider(@Value("${priority:normal}") String priority) { this.priority = priority; } // standard getter }
4 @ConfigurationProperties
package com.fengwenyi.spring_boot_config_sample.config; import com.fengwenyi.spring_boot_config_sample.support.YamlPropertySourceFactory; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import java.io.Serializable; /** * @author Erwin Feng * @since 2020/8/13 */ @Data @Configuration @ConfigurationProperties(prefix = "db") //@PropertySource({"classpath:config/db.properties"}) @PropertySource(value = {"classpath:config/db.yml"}, factory = YamlPropertySourceFactory.class) public class DBConfig implements Serializable { private static final long serialVersionUID = -6527591545525817929L; /** 服务器地址 */ private String host; /** 端口 */ private Integer port; /** 数据库名 */ private String dbName; /** 用户名 */ private String username; /** 密码 */ private String password; }
配置文件:
db: host: localhost port: 3306 db-name: test username: root password: 123456
说明: 1、@Configuration 表明这是一个Spring配置,会注入到Spring容器中。 2、@ConfigurationProperties(prefix = "db") 表示这个类与配置文件关联,其中prefix指明前缀。 3、@PropertySource 这个指明配置文件的位置,如果没有这个配置,则会从默认的配置文件读取。默认的配置文件:application.properties > application.yml。另外,这个默认只支持properties类型的文件,所以,需要配置factory。 4、@PropertySource也可以与@Value结合使用。
<br />
参考链接
- @Value用法:https://www.baeldung.com/spring-value-annotation
- 自定义配置 只读properties 不读 yml文件:https://blog.csdn.net/WTUDAN/article/details 1000 /103313167
- Spring配置文件user.name配置无效:https://blog.csdn.net/u013536829/article/details/80027391
关于
我是冯文议(Erwin Feng),Java Developer,专注于程序设计与开发。开源项目:JavaLib、api-result。喜欢电影、游戏、音乐、茶、旅行。<br />我的个人网站:https://fengwenyi.com<br />我的Github:https://github.com/fengwenyi
- springboot系列教程2:springboot配置文件读取
- SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)
- 补习系列(10)-springboot 之配置读取
- Spring Boot系列 - 4. 读取配置文件(application.yml)中的属性值
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- Spring Boot干货系列:(二)配置文件解析
- SpringBoot系列之配置文件占位符使用
- 全套SpringBoot2.x入门到项目实战课程系列(第3章 Spring Boot 核心配置)
- Spring Boot系列教程五:使用properties配置文件实现多环境配置
- dubbo的Filter如何读取Spring Boot配置文件值
- 第十四章 springboot + profile(不同环境读取不同配置)
- Spring Boot干货系列:(二)配置文件解析
- SpringBoot系列1-----springboot入门,配置,日志
- Spring-boot中读取config配置文件的两种方式
- SpringBoot系列——Java配置(SpringMVC配置)
- SpringBoot系列——Java配置(SpringMVC配置)
- Springboot 之 自定义配置文件及读取配置文件
- spring-boot读取props和yml配置文件的方法
- springBoot使用Controller读取配置文件两种方式&读取自定义配置方法
- SpringBoot配置读取不到 PropertySourcesPlaceholderConfigurer和PropertyPlaceholderConfigurer