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

}
相关推荐
亦暖筑序24 分钟前
Spring AI Alibaba 报错合集:我踩过的那些坑
java·后端
石榴树下的七彩鱼33 分钟前
OCR 识别不准确怎么办?模糊 / 倾斜 / 反光图片优化实战(附完整解决方案 + 代码示例)
图像处理·人工智能·后端·ocr·api·文字识别·图片识别
indexsunny1 小时前
互联网大厂Java面试实战:核心技术与微服务架构在电商场景中的应用
java·spring boot·redis·kafka·maven·spring security·microservices
摇滚侠1 小时前
Java 多线程基础 Java Multithreading Basics
java
今天你TLE了吗1 小时前
LLM到Agent&RAG——AI概念概述 第一章:大模型
java·人工智能·语言模型·大模型
你的牧游哥1 小时前
Java 核心概念详解
java·开发语言
深邃-1 小时前
【数据结构与算法】-顺序表链表经典算法
java·c语言·数据结构·c++·算法·链表·html5
JAVA学习通1 小时前
励志从零打造LeetCode平台之C端竞赛列表
java·vscode·leetcode·docker·状态模式
海兰1 小时前
【第3篇-续】多模型多模态项目实现示例(增加OpenAI通用适配)附源代码
java·人工智能·spring boot·alibaba·spring ai
澄澈青空~1 小时前
有一个叫R2C,也有一个叫G2C
java·数据库·人工智能·c#