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

た 入场券 2022-05-29 09:50 316阅读 0赞

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

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

[html] view plain copy

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

controller类中使用这些随机数

[html] view plain copy

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

二.属性占位符

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

[html] view plain copy

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

三.application.properties文件的优先级

Center

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

四.其他配置介绍

[html] view plain copy

  1. #配置tomcat的端口
  2. server.port=8080
  3. #时间格式化
  4. spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
  5. #时区设置
  6. spring.jackson.time-zone=Asia/Chongqing

发表评论

表情:
评论列表 (有 0 条评论,316人围观)

还没有评论,来说两句吧...

相关阅读