1.定义常量以及使用
(1) 在application.properties 中定义变量名
#32位的随机数
csdncq.secret = ${random.value}//定义常量
(2)在Controller中使用常量
//使用注解读取常量
@Value(value="${csdncq.secret}")
private String secret;
@RequestMapping(value="get")
public Map<String ,String> get(@RequestParam String name){
Map<String,String> map = new HashMap<String,String >();
map.put("name",name);
map.put("value","hello word");
map.put("secret",secret);//调用常量
return map;
}
(3)在浏览器中访问:http://localhost:8080/index/get?name=wang
2.定义数字常量
(1) 在application.properties 中定义变量名
#数字随机数
csdncq.number = ${random.int}
(2)在Controller中使用常量
//使用注解读取常量
@Value(value="${csdncq.number}")
private int number;
@RequestMapping(value="get")
public Map<String ,Object> get(@RequestParam String name){
Map<String,Object> map = new HashMap<String,Object >();
map.put("name",name);
map.put("value","hello word");
map.put("secret",secret);
map.put("number",number);
return map;
}
(3)在浏览器中访问:http://localhost:8080/index/get?name=wang
3.属性占位符
(1) 在application.properties 中定义变量名
#属性占位符
csdncq.name = www.baidu.com
csdncq.desc = ${csdncq.name} is a domain name
(2)在Controller中使用常量
//使用注解读取常量
@Value(value="${csdncq.desc}")
private String desc;
@RequestMapping(value="get")
public Map<Object ,Object> get(@RequestParam String name){
Map<Object,Object> map = new HashMap<Object,Object>();
map.put("name",name);
map.put("value","hello word");
map.put("secret",secret);
map.put("number",number);
map.put("desc",desc);
return map;
}
(3)在浏览器中访问:http://localhost:8080/index/get?name=wang