简体   繁体   English

Spring在Bean中注入任务执行器

[英]Spring inject task executor in bean

I'm using Spring Boot and I'm not able to inject a task executor in a service bean. 我正在使用Spring Boot,但无法在Service Bean中注入任务执行器。 Here's some code: 这是一些代码:

@Service
public class ClassA {
 @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

 public void doSht(){
     for(int i = 0; i<this.taskExecutor.getMaxPoolSize(); i++){
         this.taskExecutor.execute(new ClassB());
     }
 }
}

Class B: B级:

public class ClassB implements Runnable {

    @Override
    public void run() {
        System.out.println("Class B running");
    }

}

Controller: 控制器:

@Controller
public class IndexController {
    @Autowired
    ClassA ca;

    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @RequestMapping("test")
    public String test(ClassA ca){
        ca.doSht();
        return "test";
    }
}

And here's the task executor configuration: 这是任务执行器配置:

@SpringBootApplication
public class App{
    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(5);
        taskExecutor.setMaxPoolSize(10);
        taskExecutor.setQueueCapacity(30);
        return taskExecutor;
    }


    public static void main(String[] args) throws Exception{
        SpringApplication app = new SpringApplication(App.class);
        app.run(args);
    }
}

I want the ClassB instances being executed when a request comes to /test, but I get a NullPointerException because the task executor being not autowired into ClassA bean. 我希望在请求到达/ test时执行ClassB实例,但是我得到了NullPointerException,因为任务执行器没有自动装配到ClassA bean中。

What am I doing wrong? 我究竟做错了什么?

To fix the error please see the following instructions: 要修复该错误,请参见以下说明:

  1. Go to IndexController class 转到IndexController

  2. Go to public String test(ClassA ca) method 转到public String test(ClassA ca)方法

  3. Remove Class ca input parameter form test method 删除Class ca输入参数形式的测试方法

  4. test method should be like this 测试方法应该是这样的

test method changed: 测试方法已更改:

   @RequestMapping("test")
    public String test(){
        ca.doSht();
        return "test";
    }

The null pointer exception is caused because test method is using ca method argument instead of ca object that comes from @Autowired annotation 导致空指针异常是因为测试方法使用的是ca方法参数而不是来自@Autowired批注的ca对象

As your using @SpringBootApplication in App class , it is registering your custom bean definition to IOC and also it is autowiring the bean correctly. 在App类中使用@SpringBootApplication时,它会将自定义bean定义注册到IOC,并且正确地自动装配了bean。 Otherwise you'll get error on autowiring class. 否则,您将在自动装配类时出错。

So its not the issue with autowiring. 因此,它不是自动装配的问题。 You can set a debug point to check that. 您可以设置调试点进行检查。 Now in your controller you messed up your instance variable with method argument by using same name. 现在,在您的控制器中,您使用相同的名称将实例变量与方法参数弄混了。 Moreover @RequestMapping not supplying your custom class objects. 此外,@ RequestMapping不提供您的自定义类对象。 So it is coming as null and you're get the exception. 因此,它将作为null出现,您将获得异常。

Hope you understand the issue. 希望你理解这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM