Automatic injection cannot be used in spring boot scheduled tasks

in the method modified by @ schedule annotation, using automatic injection will report a null pointer
my solution is to get the spring container statically and then use the getBean () method to get it, which is successful. I would like to ask why automatic injection can not be used

Jul.15,2022

The Schedule of

Spring is implemented through Quartz, but ApplicationContext is not directly supported by default. You can configure it in the following ways

@Bean
public SpringBeanJobFactory springBeanJobFactory() {
    AutoWiringSpringBeanJobFactory jobFactory = new AutoWiringSpringBeanJobFactory();
    jobFactory.setApplicationContext(applicationContext);
    return jobFactory;
}

use @ Autowired static injection of the required dependencies in the class where the method is located, and then add @ Component annotations to the class in a format similar to the following:

@Component
@EnableScheduling
//@EnableScheduling
public class ScheduleTasks{

    @Autowired
    private SomeOtherClass someOtherClass;
    
    @Scheduled(...)
    public void go(){
      someOtherClass.do();
    }
}
After

has written @ Schedule , have you written @ Component ?

Menu