springboot支持两种配置文件
- application.properties
- application.yml(推荐yml)
在创建SpringBoot项目时默认使用的是application.properties
下面分别介绍一下这两种配置方式:
一、application.properties这种方式
这种方式就是一般的properties文件的配置方式(key=value方式)
比如配置端口号和数据库
#端口号,默认8080
server.port=80
#数据库
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test_db?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
那么问题来了
如果配置项很多,我要找某一个配置项,那么开发或者运维人员岂不是很麻烦
那么接下来介绍一下application.yml配置方式
二、application.yml配置方式
1.先介绍下YAML:
YAML是一个可读性高,用来表达数据序列化的格式。YAML 的配置文件后缀为 .yml,这种格式和json很像,都是key: value格式,但是:后有空格
例如:
debug: true
2.YAML基本语法:
- 大小写敏感
- 使用缩进表示层级关系
- 缩进不允许使用tab,只允许空格
- 缩进的空格数不重要,只要相同层级的元素左对齐即可
- '#'表示注释
3.在SpringBoot中使用application.yml配置
在resources文件夹下创建application.yml文件,删除application.properties文件
上面的配置项就变成了这样
#端口号,默认8080
server:
port: 80
#数据库
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test_db?useUnicode=true&characterEncoding=utf8
driver-class-name: com.mysql.jdbc.Driver
如果我要在代码中获取配置文件中自定义的数据的值该怎么办?
三、配置自定义数据
3.1简单的数据类型读取-@Value
首先在配置文件中配置自定义数据
myconfig:
name: 张三
description: 欢迎
age: 24
show-advert: true
在代码中使用@Configuration和@Value注解方式获取
package com.shunli.video_management.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
/**
* @author shunli
* @描述:
* @create 2020/3/1
* @since 1.0.0
*/
@Configuration
public class Persion {
@Value("${myconfig.name}")
private String name;
@Value("${myconfig.age}")
private int age;
@Value("${myconfig.description}")
private String description;
@Value("${myconfig.show-advert}")
private boolean showAdvert;
@Override
public String toString() {
return "Persion{" +
"name='" + name + '\'' +
", age=" + age +
", description='" + description + '\'' +
", showAdvert=" + showAdvert +
'}';
}
}
3.2复杂的配置文件读取- @ConfigurationProperties
application.yml
person:
username: zhangsan
age: 25
salary: 22000
sex: male
pets: cat,dog
friend:
username: wangwu
age: 27
list:
- value1
- value2
children:
- name: ting
weight: 2
- name: hao
weight: 5
employee:
name: lisi
age: 21
application.properties
#基本类型
person.userName=zhangsan
person.age=29
person.salary=22000
person.sex=male
#array
person.pets=dog,cat
#map
person.friend.userName=wangwu
person.friend.age=28
#list
person.list[0]=value1
person.list[1]=value2
#list嵌套Map/对象
person.children[0].name=ting
person.children[0].weight=2
person.children[1].name=hao
person.children[1].weight=5
#对象嵌套对象
person.employee.name=lisi
person.employee.age=21
@ConfigurationProperties 注解向Spring Boot声明该类中的所有属性和配置文件中相关的配置进行绑定。
prefix = “person”:声明配置前戳,将该前戳下的所有属性进行映射。
@Component 将该组件加入Spring Boot容器,只有这个组件是容器中的组件,配置才生效。
package com.shunli.video_management.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @author shunli
* @描述:
* @create 2020/3/1
* @since 1.0.0
*/
@ConfigurationProperties(prefix = "person")
@Component
public class Persion {
private String Username;
private Integer age;
private double salary;
private String sex;
//array
private String[] pets;
//map
private Map<String,String> friend;
//list
private List<String> list;
//list嵌套对象或者map
private List<Map<String, String>> children;
//对象嵌套对象
private Employee employee;
public String getUsername() {
return Username;
}
public void setUsername(String username) {
Username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String[] getPets() {
return pets;
}
public void setPets(String[] pets) {
this.pets = pets;
}
public Map<String, String> getFriend() {
return friend;
}
public void setFriend(Map<String, String> friend) {
this.friend = friend;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public List<Map<String, String>> getChildren() {
return children;
}
public void setChildren(List<Map<String, String>> children) {
this.children = children;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
@Override
public String toString() {
return "Persion{" +
"Username='" + Username + '\'' +
", age=" + age +
", salary=" + salary +
", sex='" + sex + '\'' +
", pets=" + Arrays.toString(pets) +
", friend=" + friend +
", list=" + list +
", children=" + children +
", employee=" + employee +
'}';
}
}
3.3 静态属性获取
1.application.yml
#JWT 密钥
jwt:
secretKey: xxxxxfdsfxxx
issuer: abc1234
2.新增 一个配置读取类 TokenSettings
package com.shunli.video_management.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* @author shunli
* @描述:
* @create 2020/3/1
* @since 1.0.0
*/
@Configuration
@ConfigurationProperties(prefix = "jwt")
public class TokenSettings {
private String secretKey;
private String issuer;
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public String getIssuer() {
return issuer;
}
public void setIssuer(String issuer) {
this.issuer = issuer;
}
@Override
public String toString() {
return "TokenSettings{" +
"secretKey='" + secretKey + '\'' +
", issuer='" + issuer + '\'' +
'}';
}
}
3.创建一个 JwtTokenUtil
package com.shunli.video_management.utils;
import com.shunli.video_management.entity.TokenSettings;
/**
* @author shunli
* @描述:
* @create 2020/3/1
* @since 1.0.0
*/
public class JwtTokenUtil {
private static String secretKey;
private static String issuer;
public static void setTokenSettings(TokenSettings tokenSettings) {
secretKey = tokenSettings.getSecretKey();
issuer = tokenSettings.getIssuer();
}
public static String getSecretKey() {
return secretKey;
}
public static String getIssuer() {
return issuer;
}
}
4.创建一个代理类 StaticInitializerUtil
package com.shunli.video_management.utils;
import com.shunli.video_management.entity.TokenSettings;
import org.springframework.stereotype.Component;
/**
* @author shunli
* @描述:
* @create 2020/3/1
* @since 1.0.0
*/
@Component
public class StaticInitializerUtil {
private TokenSettings tokenSettings;
public StaticInitializerUtil(TokenSettings tokenSettings) {
JwtTokenUtil.setTokenSettings(tokenSettings);
}
}
四、环境配置文件
在开发过程中,开发、测试、生产环境上配置文件不相同
- SpringBot可针对不同环境提供不同的Profile文件
- Profile文件的默认命名格式为application-{env}.yml
- 使用spring.profiles.active选项来指定不同的profile
比如开发环境和测试环境的端口号,日志配置等不相同。
新建两个文件为application-dev.yml和application-prd.yml
application-dev.yml文件内容
#端口号,默认8080
server:
port: 8080
debug: false
logging:
file:
path: /local/soft
name: myspringbootdemo.txt
application-prd.yml文件内容:
#端口号,默认8080
server:
port: 80
debug: false
logging:
file:
path: /local/soft
name: myspringbootdemo.txt
在application.yml文件中删除其他配置项,使用spring.profiles.active选项来指定不同的profile
spring:
profiles:
active: dev
这样在部署的时候就可以修改spring.profiles.active选项来切换配置文件