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

}
相关推荐
老大白菜3 小时前
DeepSeek API 调用 - Spring Boot 实现
java·spring boot·后端
道友老李4 小时前
【Java】多线程和高并发编程(四):阻塞队列(上)基础概念、ArrayBlockingQueue
java·系统架构
API快乐传递者4 小时前
Java爬虫:打造高效的数据抓取利器——详解详情接口设计与实现
java·开发语言·爬虫
Yoyo25年秋招冲冲冲5 小时前
Day60_补20250208_图论part5_并查集理论基础|寻找存在的路径
java·开发语言·数据结构·算法·leetcode·动态规划·图论
_平凡之路_5 小时前
实现限制同一个账号最多只能在3个客户端(有电脑、手机等)登录(附关键源码)
android·java·vue.js·spring·servlet
magic 2455 小时前
JUnit5 单元测试详解
java·开发语言·单元测试·junit 5
rain雨雨编程5 小时前
黑马Redis详细笔记(实战篇---短信登录)
java·redis·缓存·框架·短信登录
众乐乐_20086 小时前
Spring实现AOP功能的原理:代理模式(JDK动态代理与CGLIB字节码生成技术代理)的理解
java·spring·代理模式
.Net Core 爱好者6 小时前
基于Flask搭建AI应用,本地私有化部署开源大语言模型
人工智能·后端·python·语言模型·自然语言处理·flask