Spring ConfigurationProperties问题

问题描述 投票:1回答:1

我有一个spring boot应用程序,下面是这段代码

@ConfigurationProperties(prefix = “asynchronous-helper”)
public class AsynchronousHelper {
   private transient ExecutorService executor;
}

在我拥有的属性文件中

asynchronous-helper.executor.maximumPoolSize=10
asynchronous-helper.executor.corePoolSize=10

同时maximumPoolSize起作用corePoolSize失败并显示以下错误

Failed to bind properties under ‘asynchronous-helper.executor’ to java.util.concurrent.ExecutorService:

Property: asynchronous-helper.executor.corepoolsize
Value: 10
Origin: “asynchronous-helper.executor.corePoolSize” from property source “class path resource [backend-product.properties]”
Reason: Failed to bind properties under ‘asynchronous-helper.executor’ to java.util.concurrent.ExecutorService

Action:
Update your application’s configuration

执行程序的具体类是java.util.concurrent.ThreadPoolExecutor

任何想法为什么会发生以及如何解决?

java spring spring-boot configuration
1个回答
0
投票

这有效。创建了一个配置bean

@Configuration
public class AsyncHelperConfig {
  @Value("${asynchronous-helper.executor.core-pool-size:10}")
  private Integer corePoolSize;

  @Value("${asynchronous-helper.executor.maximum-pool-size:10}")
  private Integer maximumPoolSize;

  @Value("${asynchronous-helper.executor.keep-alive-time:10}")
  private Integer keepAliveTime;

  private transient ExecutorService executor;

@Bean
public AsynchronousHelper asynchronousHelper(){
    AsynchronousHelper asynchronousHelper = new AsynchronousHelper();
    executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 
   keepAliveTime, TimeUnit.MINUTES,
            new LinkedBlockingQueue<Runnable>());
    asynchronousHelper.setExecutor(executor);
    return asynchronousHelper;
}

并且在beans.xml中添加了以下配置

<bean id="AsyncHelperConfig" 
    class="asynchonous.AsyncHelperConfig"/>
© www.soinside.com 2019 - 2024. All rights reserved.