Spring Boot系列教程四:配置文件详解properties

一.配置随机数,使用随机数

在application.properties文件添加配置信息
1 #32位随机数
2 woniu.secret=${random.value}
3 #随机整数
4 woniu.number=${random.int}
5 #指定范围随机数
6 woniu.limitnumber=${random.int[0,9]}

controller类中使用这些随机数

 1 package com.woniu.controller;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 import org.springframework.beans.factory.annotation.Value;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RestController;
 9 
10 @RestController
11 @RequestMapping(value=("/web"))
12 public class WebController {
13 
14     @Value(value="${woniu.secret}")
15     private String uuid;
16     
17     @Value(value="${woniu.number}")
18     private int randomID;
19     
20     @Value(value="${woniu.limitnumber}")
21     private int limitnumber;
22     
23     
24     @RequestMapping(value="/index")
25     public Map<String, Object> Index(){
26         Map<String, Object> map = new HashMap<String, Object>();
27         map.put("uuid", uuid);
28         map.put("randomID", randomID);
29         map.put("limitnumber", limitnumber);
30         return map;
31     }
32 }

二.属性占位符

使用application.properties配置文件中先前定义的值
1 woniu.name="woniu"
2 woniu.desc=${woniu.name} is a domain name

三.application.properties文件的优先级

相同的配置信息在配置在application.properties中,优先级高的生效
 

四.其他配置介绍

1 #配置tomcat的端口
2 server.port=8080
3 
4 #时间格式化
5 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
6 
7 #时区设置
8 spring.jackson.time-zone=Asia/Chongqing
 
 
 
 
 
原文地址:https://www.cnblogs.com/wdpnodecodes/p/7406829.html