第七节:SpringBoot高级属性配置二

【第七节:SpringBoot高级属性配置二】怀抱观古今,寝食展戏谑。这篇文章主要讲述第七节:SpringBoot高级属性配置二相关的知识,希望能为你提供帮助。
配置随机数

com.rumenz.random=$random.value com.rumenz.num=$random.int com.rumenz.long.val=$random.long com.rumenz.uuid=$random.uuid com.rumenz.range=$random.int[100,1000]

package com.rumenz.lession7.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * @className: RumenzController * @description: TODO 类描述 * @author: 入门小站 rumenz.com * @date: 2021/11/6 **/@Controller @RequestMapping("/rumenz") public class RumenzController @Value("$com.rumenz.random") private String random; @Value("$com.rumenz.num") private Integer num; @Value("$com.rumenz.long.val") private Long longVal; @Value("$com.rumenz.uuid") private String uuid; @Value("$com.rumenz.range") private Integer range; @RequestMapping("/index") @ResponseBody public String index() //配置文件中的随机值 String res=String.format("random %s num %d longVal %d uuid %s range %d",random,num,longVal,uuid,range); return res;

List类型
com.rumenz.id[0]=1 com.rumenz.id[1]=2 com.rumenz.id[2]=3 com.rumenz.uid=1,2,3,4

package com.rumenz.lession7.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; import java.util.Map; /** * @className: RumenzController * @description: TODO 类描述 * @author: 入门小站 rumenz.com * @date: 2021/11/6 **/@Controller @RequestMapping("/rumenz") @ConfigurationProperties(prefix = "com.rumenz") public class RumenzController //下标配置的数组值注入 private List< Integer> id; public List< Integer> getId() return id; public void setId(List< Integer> id) this.id = id; //逗号分隔 @Value("#$com.rumenz.uid.split(,)") private List< Integer> uidList; @RequestMapping("/index1") @ResponseBody public String index1() //配置文件中的随机值 return getId().toString(); @RequestMapping("/index2") @ResponseBody public String index2() //配置文件中的随机值 return uidList.toString();

Map类型
com.rumenz.map=name:rumenz.com,age:10

package com.rumenz.lession7.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; import java.util.Map; /** * @className: RumenzController * @description: TODO 类描述 * @author: 入门小站 rumenz.com * @date: 2021/11/6 **/@Controller @RequestMapping("/rumenz") @ConfigurationProperties(prefix = "com.rumenz") public class RumenzController @Value("#$com.rumenz.map") private Map< String,String> map; @RequestMapping("/index3") @ResponseBody public String index3() //配置文件中的随机值 return map.toString();

  • GitHub:https://github.com/mifunc/springboot/tree/main/lession7
  • Gitee:https://gitee.com/rumenz/springboot/tree/master/lession7
  • https://rumenz.com/rumenbiji/springboot-advanced-config.html
  • 我的博客 https://rumenz.com/
  • 我的工具箱 https://tooltt.com/
  • 微信公众号:【入门小站】
第七节:SpringBoot高级属性配置二

文章图片

  • 关注【入门小站】回复【1001】获取 linux常用命令速查手册
  • 关注【入门小站】回复【1003】获取 LeetCode题解【java语言实现】
  • 关注【入门小站】回复【1004】获取 Java基础核心总结
  • 关注【入门小站】回复【1009】获取 阿里巴巴Java开发手册

    推荐阅读