david
david

Reputation: 1077

No qualifying bean of type 'ThreadPoolTaskExecutor' available

I'm using Spring Boot 2.2.4 and I'm trying to a custom Executor

Below are the relevant classes

@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;
    }
}

and the following class which tries to use it.

@Component
@Profile("!test")
public class DetailsScraper {
    private static final Logger logger = LoggerFactory.getLogger(DetailsScraper.class);

    @Autowired
    @Qualifier("detailsScraperExecutor")
    private ThreadPoolTaskExecutor detailsScraperExecutor;
}

When I run the application I get the following error

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'detailsScraper': Unsatisfied dependency expressed through field 'detailsScraperExecutor'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value="detailsScraperExecutor")}

my 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

Even though I have named it detailsScraperExecutor Spring can't find it? Why is that?

Upvotes: 6

Views: 8379

Answers (1)

Mykhailo Moskura
Mykhailo Moskura

Reputation: 2211

You need to inject the same type of class as declared in configuration but not a higher-level one. But you can use the lower-level one.

 @Autowired
 private Executor detailsScraperExecutor;

Upvotes: 3

Related Questions