单独使用Quartz 2.1.7 时Job属性Spring无法注入
来源:互联网 发布:mac在线是什么意思 编辑:程序博客网 时间:2024/06/09 17:36
在使用Quartz的时候遇到一个问题:
原因:
当前使用的是Spring-Quartz,xml配置,但是Job太多,导致很多的xml配置
目的:
不使用spring-quartz的配置,单独使用quartz,因为Job太多,配置文件太臃肿,而且尽量不去更改现有的Job代码,因为Job太多,更改难免会出现问题。
因为现有的Job中有很多Spring的属性依赖,不打算放到JobExecutionContext中,还要再取出来
问题:
原来的Job是Spring自动扫描的,属性可以自动注入,现在换成使用单独的Quartz,属性不能注入了(具体原因没有细看)
解决方法:
方法1:
网上查找到的方法是 一个类继承 AdaptableJobFactory
当前版本的Quartz 2.1.7的Scheduler中有一个方法 setJobFactory
然后,如下
public class JobFactory extends AdaptableJobFactory { @Autowired private AutowireCapableBeanFactory capableBeanFactory; @Override protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { //调用父类的方法 Object jobInstance = super.createJobInstance(bundle); //进行注入 capableBeanFactory.autowireBean(jobInstance); return jobInstance; }}
再去看原来Job中的属性,已经被注入了。
方法2:
public class ApplicationContextHolder implements ApplicationContextAware { private static ApplicationContext applicationContext; @SuppressWarnings("all") public void setApplicationContext(ApplicationContext context) throws BeansException { if(this.applicationContext != null) { throw new IllegalStateException("ApplicationContextHolder already holded 'applicationContext'."); } this.applicationContext = context; } public static ApplicationContext getApplicationContext() { if(applicationContext == null) throw new IllegalStateException("'applicationContext' property is null,ApplicationContextHolder not yet init."); return applicationContext; } public static Object getBean(Class<?> clazz) { return null == applicationContext ? null : applicationContext.getBean(clazz); } public static Object getBean(String beanName) { return getApplicationContext().getBean(beanName); } public static void cleanHolder() { applicationContext = null; }}
使用方法
在Job中使用 ApplicationContextHolder.getBean() 赋值给原来需要注入的属性。
推荐使用方法1 ,没有侵入性,而且需要更改的代码量最少。
0 0
- 单独使用Quartz 2.1.7 时Job属性Spring无法注入
- Quartz的使用(解决quartz的job无法注入spring对象)
- quartz的job类无法保留本身通过spring注入的属性问题
- Spring+Hibernate+Struts2整合Quartz, JOB中无法使用Spring注入
- Spring使用Quartz定时调度Job无法Autowired注入Service的解决方案
- Spring使用Quartz定时调度Job无法Autowired注入Service的解决方案
- 解决quartz的job无法注入spring对象
- quartz的job中注入spring对象!
- quartz job 注入 spring 管理的 bean
- Quartz入门实例14-让Quartz的Job使用Spring注入的Bean
- Quartz入门实例14-让Quartz的Job使用Spring注入的Bean
- quartz单独使用,不整合spring实例
- Quartz实现的job接口无法注入实例bean
- Spring+Quartz无法自动注入bean问题
- 使用Spring配置shiro时,自定义Realm中属性无法使用注解注入解决办法
- 使用Spring配置shiro时,自定义Realm中属性无法使用注解注入解决办法
- quartz 与Spring的整合-Quartz中的job如何自动注入spring容器托管的对象
- Quartz与Spring的整合-Quartz中的job如何自动注入spring容器托管的对象
- java图片裁剪和java生成缩略图
- String的trim()方法无效的解决办法
- DynamicCocoa:滴滴 iOS 动态化方案的诞生与起航
- +-字符串
- sublime格式化代码快捷键+修改tab为4个空格
- 单独使用Quartz 2.1.7 时Job属性Spring无法注入
- 数组中的逆序对
- CSS3动画——Keyframes
- AES DES RSA SHA MD5优缺点
- u-boot之u-boot.bin的生成
- 机器学习算法比较
- 剑指offer--跳台阶(递归问题)
- [python3教程]第三章.3.2迈出第一步
- COM学习笔记(二)