我正在使用Spring Boot 2.2.4,并且正在尝试使用自定义执行器
以下是相关的课程
@Configuration
@ManagedResource
public class ExecutorConfig {
@Bean(name = "detailsScraperExecutor")
public Executor getDetailsAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setQueueCapacity(1000000);
executor.setThreadNamePrefix("detailsScraperExecutor-");
executor.initialize();
return executor;
}
}
和以下尝试使用它的类。
@Component
@Profile("!test")
public class DetailsScraper {
private static final Logger logger = LoggerFactory.getLogger(DetailsScraper.class);
@Autowired
@Qualifier("detailsScraperExecutor")
private ThreadPoolTaskExecutor detailsScraperExecutor;
}
当我运行应用程序时,出现以下错误
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'detailsScraper'的bean时出错:通过字段'detailsScraperExecutor'表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor'的合格bean:至少应有1个有资格作为自动装配候选的bean。依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value =“ detailsScraperExecutor”)}
我的application.properties
spring.jmx.enabled=false
spring.datasource.url=jdbc:postgresql://example.com:5432/example
spring.datasource.username=example
spring.datasource.password=password
spring.jpa.open-in-view=false
logging.level.com.gargoylesoftware.htmlunit=ERROR
spring.datasource.hikari.maximumPoolSize = 30
app.properties.parseaddress.endpoint=http://example.com
即使我将其命名为detailsScraperExecutor
Spring也找不到?这是为什么?
您需要注入与配置中声明的类型相同的类,但不需要注入更高级别的类。但是您可以使用较低级别的一个。
@Autowired
private Executor detailsScraperExecutor;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句