解决Spring+Quartz无法自动注入bean问题
问题
我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中。无奈此方式有个问题:Spring Bean无法自动注入。
环境:Spring3.2.2 + Quartz1.6.1
Quartz配置:
<bean id="traderRiskReportJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="traderNoServerResource" /> <property name="targetMethod" value="queryTraderNo" /> <property name="concurrent" value="true" /> </bean>
service配置:
<bean name="traderNoServerResource" class="com.test.TraderNoServerResource" ><property name="threadPool" ref="threadPool"/> </bean>
ThreadPool配置:
<bean name="threadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" ><property name="corePoolSize" value="25"></property><property name="maxPoolSize" value="100"></property></bean>
出现的问题是:traderNoServerResource中的threadPool为null。
解决方法
成员变量添加注解@Autowired
然后在方法中(如例子中的queryTraderNo方法)添加以下代码,自动注入成员变量实现类
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
关于引发这个问题的原因,有待深入验证。说的比较多的是Quartz与SpringMVC的context不同,父context无法访问子context中的bean。
参考资料
http://stackoverflow.com/questions/6990767/inject-bean-reference-into-a-quartz-job-in-spring
解决Spring+Quartz无法自动注入bean问题相关推荐
- Spring系列之自动注入(autowire)详解
本文内容 手动注入的不足 Class.isAssignableFrom方法介绍 3种自动注入方式详解及案例 按名称自动注入 按类型自动注入 按构造器进行自动注入 按类型自动注入某种类型的所有bean给 ...
- java手工注入bean_java相关:Spring中如何动态注入Bean实例教程
java相关:Spring中如何动态注入Bean实例教程 发布于 2020-3-8| 复制链接 摘记: 前言在Spring中提供了非常多的方式注入实例,但是由于在初始化顺序的不同,基于标注的注入方式, ...
- 跟杨春娟学Spring笔记:自动装备Bean
跟杨春娟学Spring笔记:自动装配Bean 完成:第一遍 1.如何自动装配Bean? 要使用自动装配,就需要配置< bean >元素的autowire属性 名称:byName 说明:根据 ...
- springMVC通过spring.xml对属性注入bean值(工厂模式)
springMVC通过spring.xml对属性注入bean值,该bean是一个map容器: <bean id="configXMLCreatorFactory" class ...
- 解决Spring Boot 拦截器注入service为空的问题
解决Spring Boot 拦截器注入service为空的问题 参考文章: (1)解决Spring Boot 拦截器注入service为空的问题 (2)https://www.cnblogs.com/ ...
- 解决spring coud打包报Singleton bean creation not allowed while singletons of this factory are in destruct
解决spring coud打包报Singleton bean creation not allowed while singletons of this factory are in destruct ...
- 五)Spring + Quartz 复杂业务的两个问题:获取Spring上下文 和 自动注入服务类
配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...
- 【Spring学习】spring注解自动注入bean
Spring mvc注解用到的配置: <!-- 启用spring mvc 注解 --> <context:annotation-config /> <context:co ...
- 网店版重生系列:都是Spring配置中自动注入惹的祸
在对Spring Container管理的bean进行配置时,有一个很好用的功能就是自动注入,可以根据不同规则对bean所依赖的bean进行自动set,相信最常用的就是设置default-autowi ...
最新文章
- Android数据持久化:文件存储
- Ubuntu 14.04 64bit上升级Intel官方集显更新驱动程序
- 使用python函数计算3.5四舍五入的结果_python 数字的四舍五入-Go语言中文社区
- tensorflow基础-placeholder
- python语句print(tuple(range(2)))_Python学习(四)数据结构 —— list tuple range
- SQL Server DATEDIFF() 函数计算时间差
- [z]Qt 内存管理机制
- EL表达式(web作业)
- HDU 6113 度度熊的01世界【DFS/Flood Fill】
- 51Nod 1509加长棒
- xcap发包工具的简单使用1(构造报文)
- ActiveMQ集群安装与配置
- 用计算机做图画ppt,用计算机画图课件.ppt
- java: 未报告的异常错误org.json.JSONException; 必须对其进行捕获或声明以便抛出
- 表情识别(二)——使用Dlib、opencv和Python标记和提取脸部的特定区域
- 【LeetCode 876】链表的中间结点
- 2D转换分页按钮的制作流程(12)
- mmrotate学习记录
- HDU--1871--细节处理--记忆化暴力查找
- python 练习题目