SpringBoot集成Quartz框架时Bean对象无法注入问题解决

最近在使用springboot集成quartz框架时,遇到了无法利用@Autowire注入Bean对象,最终解决方法如下:

1.新增SpringContextUtils.java工具类

/**
 * @author Toryci
 * @version v1.0
 * @description Spring Bean 对象获取工具类
 * @date 2020/7/15 22:16
 */
@Component
public class SpringContextUtils implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;
    }

    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    public static  T getBean(String name, Class requiredType) {
        return applicationContext.getBean(name, requiredType);
    }

    public static boolean containsBean(String name) {
        return applicationContext.containsBean(name);
    }

    public static boolean isSingleton(String name) {
        return applicationContext.isSingleton(name);
    }

    public static Class getType(String name) {
        return applicationContext.getType(name);
    }
}

2.在quartz job作业类中通过类似以下方式注入Bean对象即可:

// 获取Bean对象
JobExecuteService jobExecuteService = (JobExecuteService) SpringContextUtils.getBean("jobExecuteServiceImpl");
// 调用方法
jobExecuteService.print();

 

你可能感兴趣的:(java)