gpt4 book ai didi

java - Spring配置属性问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:04:44 25 4
gpt4 key购买 nike

我有一个 Spring Boot 应用程序&这段代码如下

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

在属性文件中我有

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

虽然 maxPoolSize 有效,但 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

知道为什么会发生这种情况以及如何解决它吗?

最佳答案

这有效。创建了一个配置 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"/>

关于java - Spring配置属性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60349311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com