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

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

相关推荐
寻星探路4 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
想用offer打牌5 小时前
MCP (Model Context Protocol) 技术理解 - 第二篇
后端·aigc·mcp
曹牧6 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
KYGALYX6 小时前
服务异步通信
开发语言·后端·微服务·ruby
掘了6 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
爬山算法7 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
kfyty7257 小时前
集成 spring-ai 2.x 实践中遇到的一些问题及解决方案
java·人工智能·spring-ai
猫头虎7 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
李少兄7 小时前
在 IntelliJ IDEA 中修改 Git 远程仓库地址
java·git·intellij-idea