1、创建SringContext 工具类SpringContextHolder
package com.ygq.demo.xxljob.config;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextHolder.applicationContext = applicationContext;
}
public static <T> T getBean(Class<T> requiredType) {
return applicationContext.getBean(requiredType);
}
// ... 其他方法 ...
}
2、在对应Hander中获取bean
TaskInfoService taskInfoService = SpringContextHolder.getBean(TaskInfoService.class);