1、配置属性的提示工具,导入相对应的依赖,

        <!--配置属性的提示工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

 

2、创建一个类用于属性映射

@Data
@Component //放入bean中,该服务下任何位置都可以使用
@ConfigurationProperties(prefix = "myproject.thread") //自定义标识前缀
public class ThreadPoolConfigProperties {
    private Integer coreSize;
    private Integer maxSize;
    private Integer keepAliveTime;
}

 

3、编写线程池配置类

//@EnableConfigurationProperties(ThreadPoolConfigProperties.class) 
//开启ThreadPoolConfigProperties类的属性配置,注入后可以获取到该类了。如果该类已经在容器中了,就不需要再写该注解,直接使用即可 @Configuration public class MyThreadConfig { @Bean public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties threadPoolConfigProperties) { return new ThreadPoolExecutor( threadPoolConfigProperties.getCoreSize(), threadPoolConfigProperties.getMaxSize(), threadPoolConfigProperties.getKeepAliveTime(), TimeUnit.SECONDS, new LinkedBlockingQueue(100), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); } }

 

4、application.properties文件中定义

# 自定义的线程池的配置
myproject.thread.core-size=20
myproject.thread.max-size=100
myproject.thread.keep-alive-time=10

 

 

几个注解:

@Component //将该类放入bean中,该微服务下任何位置都可以使用
@ConfigurationProperties(prefix = "gulimall.thread") //配合属性,自定义标识前缀

@EnableConfigurationProperties(ThreadPoolConfigProperties.class) //开启配置属性,并指定属性类
posted on 2020-10-27 19:52  幂次方  阅读(728)  评论(0编辑  收藏  举报