SpringBoot的配置文件中,除了前面讲的基本配置方式。还可以配置List,Map,随机值等高级数据类型
配置随机数
com.rumenz.random=${random.value}
com.rumenz.num=${random.int}
com.rumenz.long.val=${random.long}
com.rumenz.uuid=${random.uuid}
com.rumenz.range=${random.int[100,1000]}
测试案例
package com.rumenz.lession7.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @className: RumenzController
* @description: TODO 类描述
* @author: 入门小站 rumenz.com
* @date: 2021/11/6
**/
@Controller
@RequestMapping("/rumenz")
public class RumenzController {
@Value("${com.rumenz.random}")
private String random;
@Value("${com.rumenz.num}")
private Integer num;
@Value("${com.rumenz.long.val}")
private Long longVal;
@Value("${com.rumenz.uuid}")
private String uuid;
@Value("${com.rumenz.range}")
private Integer range;
@RequestMapping("/index")
@ResponseBody
public String index(){
//配置文件中的随机值
String res=String.format("random %s num %d longVal %d uuid %s range %d",random,num,longVal,uuid,range);
return res;
}
}
浏览器访问
http://127.0.0.1:8080/rumenz/index
返回random b92abe98c8eae52089dd78ae24fd47f5 num 354638503 longVal -7870587366296902654 uuid b3994be3-c183-4e2b-a375-55e27c28faef range 929
List类型
application.properties
中配置
com.rumenz.id[0]=1
com.rumenz.id[1]=2
com.rumenz.id[2]=3
com.rumenz.uid=1,2,3,4
测试案例
package com.rumenz.lession7.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
/**
* @className: RumenzController
* @description: TODO 类描述
* @author: 入门小站 rumenz.com
* @date: 2021/11/6
**/
@Controller
@RequestMapping("/rumenz")
@ConfigurationProperties(prefix = "com.rumenz")
public class RumenzController {
//下标配置的数组值注入
private List<Integer> id;
public List<Integer> getId() {
return id;
}
public void setId(List<Integer> id) {
this.id = id;
}
//逗号分隔
@Value("#{'${com.rumenz.uid}'.split(',')}")
private List<Integer> uidList;
@RequestMapping("/index1")
@ResponseBody
public String index1(){
//配置文件中的随机值
return getId().toString();
}
@RequestMapping("/index2")
@ResponseBody
public String index2(){
//配置文件中的随机值
return uidList.toString();
}
}
访问
http://127.0.0.1:8080/rumenz/index1
返回[1, 2, 3]
访问
http://127.0.0.1:8080/rumenz/index2
返回[1, 2, 3, 4]
Map类型
application.properties
中配置
com.rumenz.map={name:'rumenz.com',age:10}
测试案例
package com.rumenz.lession7.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
/**
* @className: RumenzController
* @description: TODO 类描述
* @author: 入门小站 rumenz.com
* @date: 2021/11/6
**/
@Controller
@RequestMapping("/rumenz")
@ConfigurationProperties(prefix = "com.rumenz")
public class RumenzController {
@Value("#{${com.rumenz.map}}")
private Map<String,String> map;
@RequestMapping("/index3")
@ResponseBody
public String index3(){
//配置文件中的随机值
return map.toString();
}
}
访问
http://127.0.0.1:8080/rumenz/index3
返回{name=rumenz.com, age=10}
本小结源码地址: