spring boot通过文件配置yaml里面的属性

yaml文件

yaml 复制代码
fsg:
batch-approval:
  # 批量审批
  batch-approval:
    pool:
      core-size: 2
      max-size: 10
      queue-capacity: 100
      keep-alive: 60
      name-prefix: ApprovalThread-
    shutdown:
      await-termination: true
      await-termination-period: 60

ConfigurationProperties配置

java 复制代码
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.time.Duration;

@Data
@ConfigurationProperties("fsg.batch-approval")
public class BatchApprovalProperties {
    private Pool pool = new Pool();

    private Shutdown shutdown = new Shutdown();

    @Data
    public static class Pool {

        /**
         * Queue capacity. An unbounded capacity does not increase the pool and therefore
         * ignores the "max-size" property.
         */
        private int queueCapacity = 200;

        /**
         * Core number of threads.
         */
        private int coreSize = 2;

        /**
         * Maximum allowed number of threads. If tasks are filling up the queue, the pool
         * can expand up to that size to accommodate the load. Ignored if the queue is
         * unbounded.
         */
        private int maxSize = 10;

        /**
         * Whether core threads are allowed to time out. This enables dynamic growing and
         * shrinking of the pool.
         */
        private boolean allowCoreThreadTimeout = true;

        /**
         * Time limit for which threads may remain idle before being terminated.
         */
        private Integer keepAlive = 60;

        private String namePrefix = "ApprovalThread-";
    }

    @Data
    public static class Shutdown {

        /**
         * Whether the executor should wait for scheduled tasks to complete on shutdown.
         */
        private boolean awaitTermination;

        /**
         * Maximum time the executor should wait for remaining tasks to complete.
         */
        private Duration awaitTerminationPeriod;
    }

}

使用配置文件的属性

java 复制代码
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
@EnableAsync
@Configuration
@EnableConfigurationProperties(BatchApprovalProperties.class)
public class BatchApprovalAsyncConfig {

    @Bean(name = "batchApprovalTaskExecutor")
    public Executor batchApprovalTaskExecutor(BatchApprovalProperties props) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(props.getPool().getCoreSize()); // 核心线程数
        executor.setMaxPoolSize(props.getPool().getMaxSize()); // 最大线程数
        executor.setQueueCapacity(props.getPool().getQueueCapacity()); // 队列大小
        executor.setKeepAliveSeconds(props.getPool().getKeepAlive()); // 线程空闲时的存活时间
        executor.setThreadNamePrefix(props.getPool().getNamePrefix()); // 线程名称前缀
        executor.initialize(); // 初始化线程池
        return executor;
    }

}
相关推荐
我真会写代码几秒前
深入理解JVM GC:触发机制、OOM关联及核心垃圾回收算法
java·jvm·架构
本喵是FW7 分钟前
C语言手记1
java·c语言·算法
程序员Terry12 分钟前
RocketMQ 使用指南
后端·rocketmq
洛阳泰山15 分钟前
MaxKB4j Docker Compose 部署指南
java·docker·llm·springboot·rag·maxkb4j
AI茶水间管理员17 分钟前
OpenClaw 的 Token 消耗怎么计算?(附实操优化方案)
后端
星浩AI18 分钟前
现在最需要被 PUA 的,其实是 AI
人工智能·后端·github
程序员老赵24 分钟前
超全 Docker 镜像源配置指南|Windows/Mac/Linux一键搞定,拉镜像再也不卡顿
linux·后端·容器
森林里的程序猿猿32 分钟前
垃圾收集器G1和ZGC
java·jvm·算法
弹简特34 分钟前
【JavaEE18-后端部分】 MyBatis 入门第二篇:使用注解完成增删改查(含有参数传递底层原理)
spring boot·mybatis
weixin_4041576834 分钟前
Java高级面试与工程实践问题集(五)
java·开发语言·面试