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

}
相关推荐
吴文周2 小时前
告别重复劳动:一套插件让 AI 替你写代码、修Bug、做测试、上生产
前端·后端·ai编程
Cyeam2 小时前
Roadbook CSV:一行 CSV 秒变高德地图路书
后端·开源·aigc
yaoxin5211233 小时前
390. Java IO API - WatchDir 示例
java·前端·python
懒狗小前端3 小时前
做了一个 codex 的中文文档网站,做的不好可以随便喷
前端·后端
Halo_tjn4 小时前
Java 基于字符串相关知识点
java·开发语言·算法
梦想的颜色4 小时前
java 利用redis来限制用户频繁点击
java·开发语言
Eric_见嘉5 小时前
在职前端 Agent 配置分享
前端·后端·agent
Ares-Wang5 小时前
Flask》》 Flask-OpenID 认证、 OpenID Connect (OIDC)
后端·python·flask
掘金码甲哥5 小时前
这篇优雅安装k8s集群的姿势,请务必投喂给AI智能体, 包装包活的那种!
后端
PH = 76 小时前
OverlayFS联合文件系统使用示例
java·linux·服务器