没有可用的'ThreadPoolTask​​Executor'类型的合格bean

时间:2020-02-17 16:21:06

标签: spring spring-boot

我正在使用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: 创建名称为'detailsS​​craper'的bean时出错:不满意的依赖关系 通过字段“ detailsS​​craperExecutor”表示;嵌套异常为 org.springframework.beans.factory.NoSuchBeanDefinitionException:否 类型的合格豆 'org.springframework.scheduling.concurrent.ThreadPoolTask​​Executor' 可用:至少有1个符合自动装配条件的bean 候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(required = true), @ org.springframework.beans.factory.annotation.Qualifier(value =“ detailsS​​craperExecutor”)}

我的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也找不到它?为什么呢?

1 个答案:

答案 0 :(得分:1)

您需要注入与配置中声明的类型相同的类,但不需要注入更高级别的类。但是您可以使用较低级别的一个。

 @Autowired
 private Executor detailsScraperExecutor;