您的位置:首页 > 编程语言 > Java开发

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

2017-01-08 17:53 1096 查看

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

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

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


controller类中使用这些随机数

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配置文件中先前定义的值

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


三.application.properties文件的优先级



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

四.其他配置介绍

#配置tomcat的端口
server.port=8080

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

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


工程springboot_properties下载地址:点击打开链接

spring boot讨论群:611262656,一键加群:点击加群

更多技术文章请关注微信公众号“Java架构师之路”:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: