1. 程式人生 > >Spring Boot系列教程四:配置文件詳解properties

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

date int ava ota axu return 端口 rand work

一.配置隨機數,使用隨機數

在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

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