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

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

    public List getId() {
        return id;
    }

    public void setId(List id) {
        this.id = id;
    }
    
   
    //逗号分隔
    @Value("#{'${com.rumenz.uid}'.split(',')}")
    private List 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 map;

    @RequestMapping("/index3")
    @ResponseBody
    public String index3(){
        //配置文件中的随机值
        return map.toString();
    }

}

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

本小结源码地址:

介绍

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

你可能感兴趣的:(springboot)