Pavlo Pavlo - 1 year ago 1122
Java Question

Spring boot. How to Create TaskExecutor with Annotation?

I did a

class in Spring Boot application with one of the methods that should run asynchronously. As I read method should be
annotated and also I have to run a
bean. But in Spring manual I not find any info or example how to run
with annotation, without XML config. Is it possible to create
bean in Spring Boot without XML, with annotations only? Here my Service class:

public class CatalogPageServiceImpl implements CatalogPageService {

public void processPagesList(List<CatalogPage> catalogPageList) {
for (CatalogPage catalogPage:catalogPageList){

public void processPage(CatalogPage catalogPage) {
System.out.println("print from Async method "+catalogPage.getUrl());

Answer Source

Add a @Bean method to your Spring Boot application class:

public class MySpringBootApp {

    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        return executor;

    public static void main(String[] args) {
        // ...

See Java-based container configuration in the Spring Framework reference documentation on how to configure Spring using Java config instead of XML.

(Note: You don't need to add @Configuration to the class because @SpringBootApplication already includes @Configuration).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download