How do I autowire a Spring TaskExecutor created thread?

后端 未结 3 1084
北海茫月
北海茫月 2021-02-04 08:18

According to Spring\'s documentation the way to use the TaskExecutor is as follows:

import org.springframework.core.task.TaskExecutor;

public class TaskExecutor         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-02-04 08:53

    Try this method.

    @Service
    @Scope("prototype")
    public invokeClass {
    
        @Autowired
        private ApplicationContext applicationContext;
    
    
    
        public void methodName(TestRequest input){
            TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
    
            taskExecutor.execute(new Runnable() {
                public void run() {
    
                    applicationContext.getBean("testService", TestService.class).execute(input);
                }
            });
        }
    }
    

提交回复
热议问题