我正在使用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' 可用:至少有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也找不到它?为什么呢?
答案 0 :(得分:1)
您需要注入与配置中声明的类型相同的类,但不需要注入更高级别的类。但是您可以使用较低级别的一个。
@Autowired
private Executor detailsScraperExecutor;