配置文件说明
springboot的默认配置都封装 spring-boot-autoconfigure-xx版本.jar/META-INF/additional-spring-configuration-metadata.json
文件中
打开这个文件,比如搜索端口配置 server.port
,看到默认为8080
springboot常用的配置文件有 yaml和properties格式
在springboot项目src/main/resources
目录新建application.yml
文件,添加内容:
server:
port: 8081
启动项目后,端口变为8081,打印日志:
Tomcat started on port(s): 8081 (http) with context path ''
读取配置的3种方式
在配置中添加自定义配置:
myconfig:
user:
name: 哈哈哈
sex: 1
age: 20
下面通过3中方式来读取配置的值
@Value
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class ConfigApp {
public static void main(String[] args) throws Exception {
SpringApplication.run(ConfigApp.class, args);
}
//@Value("myconfig.user.name")//结果:name=myconfig.user.name 不会从配置取值,相当于直接赋值
@Value("${myconfig.user.name}") //结果 配置中的姓名
private String name;
@Value("${myconfig.user.sex}")
private int sex;
@Value("${myconfig.user.age}")
private int age;
// 测试接口
@GetMapping("/user")
String user() {
StringBuilder sb = new StringBuilder();
sb.append("@Value形式获取配置参数:\n");
sb.append(String.format("name=%s, sex=%s, age=%s \n", name, sex, age));
return sb.toString();
}
}
测试结果:
@Value形式获取配置参数:
name=哈哈哈, sex=1, age=20
配置类
也可以自己新建一个配置类,只需要说明配置的前缀@ConfigurationProperties(prefix = "前缀名")
,会自动匹配类字段。 如前面的user配置:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "myconfig.user")//前缀
@Data
public class User {
private String name;//自动匹配name
private int sex;//自动匹配sex
private int age;//自动匹配age
}
测试:
@Autowired
User user;
@GetMapping("/user")
String user() {
StringBuilder sb = new StringBuilder();
sb.append("\n\n---------------------------------------\n\n");
sb.append("从配置类中获取配置:\n");
sb.append(user);
return sb.toString();
}
结果:
---------------------------------------
从配置类中获取配置:
User(name=哈哈哈, sex=1, age=20)
Environment
从环境信息中读取配置
@Autowired
Environment env;
@GetMapping("/user")
String user() {
StringBuilder sb = new StringBuilder();
sb.append("从Environment中获取配置:\n");
sb.append(env.getProperty("myconfig.user.name"));//注意 这里不加${}
return sb.toString();
}
结果:
从Environment中获取配置:
哈哈哈
配置集合 array、list、map
配置数组
yml格式:
myconfig:
arr: arr, arrarr #数组
list: list11,listlist #集合
properties格式
myconfig.arr[0]=arr
myconfig.arr[1]=arrarr
获取数组
@Value("${myconfig.arr}")
private String[] arr;
@Value("${myconfig.list}")
private List<String> list;
@GetMapping("/coll")
public String getArr(){
return "arr:=" + arr[0] + ","+arr[1] + "\nlist="+ list;
}
测试结果:
arr=arr,arrarr
list=[list11, listlist]
配置map
使用@Value获取map的坑
如果直接使用@Value,启动会报错
yml:
myconfig:
map:
k1: v1
k2: v2
java代码:
@Value("${myconfig.map}")
private Map map;
报错信息:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'myconfig.map' in value "${myconfig.map}"
最后找了半天资料,发现需要听过配置类来解决
使用配置类获取map的值
@Configuration
@ConfigurationProperties(prefix = "myconfig")
public class MyConfig {
@Getter @Setter//必须添加getter、setter
private Map map;
}
----
//读取配置
@Autowired private MyConfig myConfig;
@GetMapping("/coll")
public Map getArr(){
return myConfig.getMap();
}
结果:
{k1=v1, k2=v2}
随机数配置
myconfig:
rand:
str: ${random.value}
num: ${random.int}
num1: ${random.long}
num2: ${random.int(10,99)}
uuid: ${random.uuid}
演示结果:
随机数:
random.value:b02d121044d9130ea9024bc6853cf0aa
random.int:98141319
random.long:8353065529278943330
random.int(10,99):27
random.uuid:2cd9d432-3210-48ea-bedc-cbe6fac3ecb4
源码
https://gitee.com/yimingkeji/springboot.git