[Solved] SpringBoot Task Error: Unexpected error occurred in scheduled task

Reason:

1. @Scheduled annotation mode level is higher than resource injection level, which leads to resource injection failure

2. There is an error in the code of the scheduled task execution (just check your own code carefully)

Solution:

@Component
public class ScheduleConfig implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static <T> T getBean(Class<T> clazz) {
        return context != null ?context.getBean(clazz) : null;
    }

}

Write a Bean to implement the ApplicationContextAware interface and rewrite the setApplicationContextAware method

It can be called in the scheduled task

Read More: