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

7次阅读

共计 3585 个字符,预计需要花费 9 分钟才能阅读完成。

SpringBoot 的配置文件中, 除了后面讲的根本配置形式。还能够配置 List,Map, 随机值等高级数据类型

配置随机数

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;
    }

}

浏览器拜访 http://127.0.0.1:8080/rumenz/index 返回random b92abe98c8eae52089dd78ae24fd47f5 num 354638503 longVal -7870587366296902654 uuid b3994be3-c183-4e2b-a375-55e27c28faef range 929

List 类型

application.properties中配置

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();}
}

拜访 http://127.0.0.1:8080/rumenz/index1 返回[1, 2, 3]

拜访 http://127.0.0.1:8080/rumenz/index2 返回[1, 2, 3, 4]

Map 类型

application.properties中配置

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();}

}

拜访 http://127.0.0.1:8080/rumenz/index3 返回{name=rumenz.com, age=10}

本小结源码地址:

  • GitHub:https://github.com/mifunc/spr…
  • Gitee:https://gitee.com/rumenz/spri…
  • https://rumenz.com/rumenbiji/…

介绍

  • 我的博客 https://rumenz.com/
  • 我的工具箱 https://tooltt.com/
  • 微信公众号:【入门小站】

  • 关注【入门小站】回复【1001】获取 linux 常用命令速查手册
  • 关注【入门小站】回复【1003】获取 LeetCode 题解【java 语言实现】
  • 关注【入门小站】回复【1004】获取 Java 根底外围总结
  • 关注【入门小站】回复【1009】获取 阿里巴巴 Java 开发手册
正文完
 0