Quarz调度通过ApplicationContext获取Service实例
2020-03-06 21:34
1176 查看
项目中需要继承Quartz框架。项目的环境是SSM框架
遇到的问题是:在quartz的Job中使用@Autowired自动注入service时候报错,报service为null。
quarz调度不受spring容器管理,不能直接使用@AutoWired注入service
问题:如何在非spring容器中(非controller中)使用service方法。
思路:可以通过ApplicationContext 实例去获取bean
解决方法:
1、编写工具类继承ApplicationContextAware类
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.ComponentScan; public class ApplicationContextSup 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> T getBean(Class<T> tClass){ return context.getBean(tClass); }
}
2、在spring.xml中配置bean如下:
<bean id="ApplicationContextSup" class="com.quarz.ApplicationContextSup"></bean>
3、在web.xml配置监听器
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
4、最后可以在quarz作业中使用ApplicationContext获取bean
AAService aaservice = (AAService) ApplicationContextSup.getBean(AAService.class);
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 通过Spring注入,获取Service对象实例
- python 通过xml获取测试节点和属性的实例
- Java反射代码--01.通过类名称加载一个类并且获取当前类实例
- MyBatis+Spring在注解@Autowried后通过反射的方式调用方法获取注入的Service或DAO对象为空
- spring mvc DispatcherServlet详解之三---request通过ModelAndView中获取View实例的过程
- Flex通过JS获取客户端IP和计算机名的实例代码
- C#反射 获取程序集信息和通过类名创建类实例
- JS和jQuery通过this获取html标签中的属性值(实例代码)
- WF实例学习笔记:(2)通过Workflow 调用 WCF Data Services 获取数据
- Android通过百度地图API用Service和Alarm在后台定时获取地理位置信息
- 通过反序列化获取多个实例会破坏单例类的规则
- Angular 通过注入 $location 获取与修改当前页面URL的实例
- cocos2d-x 2.0通过CCAnimation实例获取CCSpriteFrame
- Flex通过JS获取客户端IP和计算机名的实例代码
- XML Web Service开发实例——通过Windows Forms调用Web Service
- C#反射 获取程序集信息和通过类名创建类实例
- JavaWeb将数据包装成JSON以及JSP通过ajax对JSON的获取与解析(代码实例)
- PHP通过文件路径获取文件名的实例代码
- 如何在自定义标签中获取spring通过@Service注解来的service
- C#反射 获取程序集信息和通过类名创建类实例