SpringBoot配置文件占位符

一.随机数

实体类代码:

复制代码
package com.qcby.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "random")
public class RandomConfig {

    private String value;
    private int intValue;
    private long longValue;
    private int intInRange;
    private int intWithLimit;

    // Getters 和 Setters

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public int getIntValue() {
        return intValue;
    }

    public void setIntValue(int intValue) {
        this.intValue = intValue;
    }

    public long getLongValue() {
        return longValue;
    }

    public void setLongValue(long longValue) {
        this.longValue = longValue;
    }

    public int getIntInRange() {
        return intInRange;
    }

    public void setIntInRange(int intInRange) {
        this.intInRange = intInRange;
    }

    public int getIntWithLimit() {
        return intWithLimit;
    }

    public void setIntWithLimit(int intWithLimit) {
        this.intWithLimit = intWithLimit;
    }

    @Override
    public String toString() {
        return "RandomConfig{" +
                "value='" + value + '\'' +
                ", intValue=" + intValue +
                ", longValue=" + longValue +
                ", intInRange=" + intInRange +
                ", intWithLimit=" + intWithLimit +
                '}';
    }
}

yml里面加

复制代码
random:
  value: ${random.value}  # 随机的 UUID 值
  intvalue: ${random.int}  # 随机的整数
  longvalue: ${random.long}  # 随机的长整型数值
  intInRange: ${random.int[1024,65536]}  # 随机的整数,范围从 1024 到 65536
  intWithLimit: ${random.int(10)}  # 随机生成一个 0 到 10 之间的整数

启动类

复制代码
@SpringBootApplication
@EnableConfigurationProperties(person.class)
@ImportResource("classpath:beans.xml")//导入xml注解
public class mavenSpringBootDemo {

    @Autowired
    private RandomConfig randomConfig;
    public static void main(String[] args) {
    SpringApplication.run(mavenSpringBootDemo.class,args);
   }


   @Bean
   public CommandLineRunner run() {
     return args -> {
            // 打印随机配置的值
            System.out.println("Random Config: " + randomConfig);
        };
    }
}

控制台打印

二.占位符获取之前配置的值,如果没有可以是用:指定默认值

yml里改

复制代码
age: ${random.int:70}

启动上面的配置类

打印的值随机生成为了4

注意:如果年龄字段有数据校验,要注释掉

相关推荐
星栈6 小时前
MCP 从 stdio 迁到 SSE,踩了 5 个传输层坑
人工智能·后端·架构
Conan在掘金6 小时前
鸿蒙报错速查:struct 里嵌 enum 声明就炸,根因 + 真解法
后端
captain3767 小时前
多线程线程安全问题
java·java-ee
CoderYanger7 小时前
A.每日一题:1979. 找出数组的最大公约数
java·程序人生·算法·leetcode·面试·职场和发展·学习方法
paopaokaka_luck7 小时前
基于Springboot3+Vue3的高校选课系统(AI选课助手、协同过滤算法、分享到微博、扣扣、Echarts图形化分析)
网络·spring boot·网络协议·echarts
灵极海7 小时前
LangChain4j RAG 实战完整指南:从入门到踩坑
java·langchain
yaoxin5211237 小时前
476. Java 反射 - 调用方法
java·开发语言
回家路上绕了弯7 小时前
Java双数据源实战全指南:Spring Boot多数据源配置、原理与踩坑避坑
后端
神奇小汤圆7 小时前
Redis主从切换后,旧主恢复竟触发全量同步?——我猜错了,原因比想象中更严谨
后端
JoyT7 小时前
滚动MAD与孤立森林在时序异常检测中的协同应用
后端