使用SpringBoot框架开发,读取配置是少不了的,那么你会读取配置吗?你会写配置吗?List?Map?java
本节咱们要解决以下几个问题:git
Spring Boot默认的配置文件有两种格式: application.properties
和 application.yml
。github
查找顺序是首先从application.properties
查找,若是找不到,再查找 application.yml
。
spring
优先级: application.properties > application.yml
。数据库
首先,添加依赖。json
Maven:api
<!-- spring boot config --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
Gradle:服务器
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
配置以下:数据结构
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'}
代码以下:app
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("#{'${erwin.visitedCities}'.split(',')}") private List<String> visitedCities; @Value("#{${erwin.moreOther}}") private Map<String, String> moreOther; }
配置以下(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
代码以下:
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; }
读取出来的数据展现:
{ "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" } }
@Autowired private Environment environment;
格式:@Value("${name:defaultValue}")
当配置文件找不到这个配置时,就会返回默认值,若是没有默认值,就会报错。
如:@Value("${java.home}")
D:JavaJava8jdk1.8.0_251jre
// 单元测试-读取配置文件的值 @Value("${erwin.like}") public void testReadLike(String like, @Value("${erwin.sex}") String sex) { System.out.println("1===>" + like); System.out.println("1===>" + sex); }
参数 like 会取 @Value("${erwin.like}") 的值
参数 sex 会取 @Value("${erwin.sex}") 的值
通过测试,多个方法,执行顺序是随机。
特别注意:这个只是在启动的时候执行,可是实际调用的时候,仍是传入的值。
方法一:
配置文件:
erwin.like=movie,game,music,tea,travel
Java代码:
@Value("${erwin.like}")
private List<String> likes;
方法二:
erwin.visitedCities=巴中,揭阳,广州,从化,成都,三亚,上海,杭州,北京
Java代码:
@Value("#{'${erwin.visitedCities}'.split(',')}")
private List<String> visitedCities;
配置文件:
erwin.moreOther={myWeb:' https://fengwenyi.com',github:' https://github.com/fengwenyi'}
Java代码:
@Value("#{${erwin.moreOther}}")
private Map<String, String> moreOther;
@Value("#{systemProperties}")
private Map<String, String> systemPropertiesMap;
@Component @PropertySource("classpath:values.properties") public class PriorityProvider { private String priority; @Autowired public PriorityProvider(@Value("${priority:normal}") String priority) { this.priority = priority; } // standard getter }
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
说明:
一、@Configuration 代表这是一个Spring配置,会注入到Spring容器中。
二、@ConfigurationProperties(prefix = "db") 表示这个类与配置文件关联,其中prefix指明前缀。
三、@PropertySource 这个指明配置文件的位置,若是没有这个配置,则会从默认的配置文件读取。默认的配置文件:application.properties > application.yml。另外,这个默认只支持properties类型的文件,因此,须要配置factory。
四、@PropertySource也能够与@Value结合使用。
YamlPropertySourceFactory
package com.fengwenyi.spring_boot_config_sample.support; import org.springframework.boot.env.YamlPropertySourceLoader; import org.springframework.core.env.PropertySource; import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.PropertySourceFactory; import java.io.IOException; /** * @author Erwin Feng * @since 2020/8/13 */ public class YamlPropertySourceFactory implements PropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { // 返回 yaml 属性资源 return new YamlPropertySourceLoader() .load(resource.getResource().getFilename(), resource.getResource()) .get(0); } }
我是冯文议(Erwin Feng),Java Developer,专一于程序设计与开发。开源项目:JavaLib、api-result。喜欢电影、游戏、音乐、茶、旅行。
个人我的网站:https://fengwenyi.com
个人Github:https://github.com/fengwenyi