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

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

相关推荐
皮皮林5512 分钟前
SpringBoot 4 最被低估的新特性:Spring Data AOT
spring boot
冰河团队4 分钟前
一个拉胯的分库分表方案有多绝望?整个部门都在救火!
java·高并发·分布式数据库·分库分表·高性能
洛_尘8 分钟前
Java EE进阶:Linux的基本使用
java·java-ee
宸津-代码粉碎机10 分钟前
Spring Boot 4.0虚拟线程实战调优技巧,最大化发挥并发优势
java·人工智能·spring boot·后端·python
MaCa .BaKa12 分钟前
47-心里健康咨询平台/心理咨询系统
java·spring boot·mysql·tomcat·maven·intellij-idea·个人开发
木子欢儿31 分钟前
Docker Hub 镜像发布指南
java·spring cloud·docker·容器·eureka
Devin~Y41 分钟前
高并发电商与AI智能客服场景下的Java面试实战:从Spring Boot到RAG与向量数据库落地
java·spring boot·redis·elasticsearch·spring cloud·kafka·rag
蜡台1 小时前
IDEA 一些 使用配置和插件
java·ide·intellij-idea
小码哥_常1 小时前
一个Starter搞定六种防护,Spring Boot API的超强护盾来了
后端
磊 子1 小时前
redis详解2
java·spring boot·redis