解决quartz的Job类中使用注解Service为null的办法

项目中需要继承Quartz框架。项目的环境是SSM框架,spring4.3,Quartz1.6版本,Intellij IDEA 2017.1
遇到的问题是:在quartz的Job中使用@Autowired自动注入service时候报错,报service为null。
原先代码如下:

public class XXXJob implements Job{
	@AutoWired
	XXXService xxxService;
	@Override
	public void execute(JobExecutionContext jobContext) throws JobExecutionException{
		System.out.println(xxxService.getResult());
	}
}

原来在非spring容器里面调用service的方法需要使用工具类获取service,ApplicationContext.getBean(“xxxService”)
然后写了工具类之后,又报出no bean named ‘xxxService’ available,没脾气,还是获取不到这个service,再去网上搜索Quartz的Job类中如何使用service,搜了一大堆相同的方法,比如什么配置AdaptableJobFactory,试了都没有用,还是报错。最后根据下面两个博客的内容整合之后,可以使用service了。
如何在Java Filter 中注入 Service
ServletContextListener使用

最后代码如下:
QuartzServletContextListener 代码如下:

public class QuartzServletContextListener extends QuartzInitializerListener {  
  
    public static final String MY_CONTEXT_NAME = "servletContext";  
  
    @Override  
    public void contextDestroyed(ServletContextEvent sce) {  
        // TODO Auto-generated method stub  
        super.contextDestroyed(sce);  
    }  
  
    @Override  
    public void contextInitialized(ServletContextEvent sce) {  
        // TODO Auto-generated method stub  
        super.contextInitialized(sce);  
        ServletContext servletContext = sce.getServletContext();  
        StdSchedulerFactory factory = (StdSchedulerFactory) servletContext  
                .getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY);  
        try {  
            factory.getScheduler().getContext()  
                    .put(QuartzServletContextListener.MY_CONTEXT_NAME, servletContext);  
        } catch (SchedulerException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    } 

将QuartzServletContextListener 配置到web.xml中去

  
        x.x.x.QuartzServletContextListener  
 

XXXJob的代码如下:

public class XXXJob implements Job{
	@Override
	public void execute(JobExecutionContext jobContext) throws JobExecutionException{
	try {  
            ServletContext context = null;  
            try {  
                context = (ServletContext) jobContext.getScheduler().getContext()  
                        .get(QuartzServletContextListener.MY_CONTEXT_NAME);  
                } catch (SchedulerException e1) {  
                // TODO Auto-generated catch block  
                e1.printStackTrace();  
         }  
         XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(context);
        XXXService xxxService = (XXXService)cxt.getBean("XXXService");
		System.out.println(xxxService.getResult());
	}
}

以上是一种方法,比较复杂,有一种更简单的,问题主要根源就是如何在非spring容器中(非controller中)使用service方法。
编写工具类继承ApplicationContextAware类

public class ApplicationContextHelper implements ApplicationContextAware{
	private static ApplicationContext context;
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context=applicationContext;
	}
	public static Object getBean(String beanName){return context.getBean(beanName);}
	
	public static  T getBean(Class tClass){return context.getBean(tClass);}
}

随后在spring-mvc.xml中配置bean如下:


使用时候,如下:
xxxService service=ApplicationContextHelper.getBean(xxxService.class);

你可能感兴趣的:(框架工具类)