Spring Boot入门四:配置文件详解properties

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

在application.properties文件添加配置信息

#32位随机数
woniu.secret=${random.value}
#随机整数
woniu.number=${random.int}
#指定范围随机数
woniu.limitnumber=${random.int[0,9]}

controller类中使用这些随机数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.woniu.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=("/web"))
public class WebController {

@Value(value="${woniu.secret}")
private String uuid;

@Value(value="${woniu.number}")
private int randomID;

@Value(value="${woniu.limitnumber}")
private int limitnumber;


@RequestMapping(value="/index")
public Map<String, Object> Index(){
Map<String, Object> map = new HashMap<String, Object>();
map.put("uuid", uuid);
map.put("randomID", randomID);
map.put("limitnumber", limitnumber);
return map;
}
}

二.属性占位符

使用application.properties配置文件中先前定义的值

1
2
woniu.name="woniu"
woniu.desc=${woniu.name} is a domain name

三.application.properties文件的优先级

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

四.其他配置介绍

1
2
3
4
5
6
7
8
#配置tomcat的端口
server.port=8080

#时间格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

#时区设置
spring.jackson.time-zone=Asia/Chongqing

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×