场景:在ContextLoaderListener子类中加载job,为JobFactory的实现类声明@Component后,在ContextLoaderListener子类中为scheduler设置JobFactory。(主要解决的问题:在spring与quartz调用job时,job中无法读取注解类,实现注入)

步骤一:

ContextLoaderListener子类中contextInitialized方法中代码如下:

super.contextInitialized(event);

applicationContext = super.getCurrentWebApplicationContext();

        scheduler = applicationContext.getBean(Scheduler.class);

try {

  scheduler.setJobFactory(applicationContext.getBean(JobFactory.class));

 } catch (BeansException e1) {

  logger.error(e1.getMessage(),e1);

 } catch (SchedulerException e1) {

  logger.error(e1.getMessage(),e1);

 }

步骤二: 声明JobFactory子类,和job中的服务类

package com.river.job.listener;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component; @Component
public class MyJobFactory extends AdaptableJobFactory {
// 这个对象Spring会帮我们自动注入进来,也属于Spring技术范畴.
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
protected Object createJobInstance(TriggerFiredBundle bundle)
throws Exception {
// 调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
// 进行注入,这属于Spring的技术,不清楚的可以查看Spring的API.
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
package com.river.service1;

import org.springframework.stereotype.Service;

@Service
public class TestBean { }

步骤三:在spring-job.xml中加入要注入的包扫描

<context:component-scan base-package="com.river.job.listener" />
<context:component-scan base-package="com.river.service1" />

现在就可以测试一下,在job中测试结果如下:

river_Worker-2===============com.river.job.HiJobImp@596b2557  2015-09-07 18:41:30
com.river.service1.TestBean@7efd6242

可见这里拿到TestBean的对象了。

在ContextLoaderListener中使用注解注入的类和job中使用注解注入的类的更多相关文章

  1. Spring中 如果该Service有多个实现类,它怎么知道该注入哪个ServiceImpl类?

    方法一:Controller中注入service的时候使用@Autowired自动注入,@Qualifier("beanId") 来指定注入哪一个. 方法二:Controller中 ...

  2. util类中非静态方法中注入serivce,在controller层是使用util。

    今天碰到如题的问题,刚一开始在util中注入service总是注入失败,起初我以为是util中没有注入成功,debug看了一下果然注入不进来. 然后各种纠结,最终坑爹的问题是在controller直接 ...

  3. ASP.NET Core 中文文档 第三章 原理(10)依赖注入

    原文:Dependency Injection 作者:Steve Smith 翻译:刘浩杨 校对:许登洋(Seay).高嵩 ASP.NET Core 的底层设计支持和使用依赖注入.ASP.NET Co ...

  4. Spring bean依赖注入、bean的装配及相关注解

    依赖注入 Spring主要提供以下两种方法用于依赖注入 基于属性Setter方法注入 基于构造方法注入 Setter方法注入 例子: public class Communication { priv ...

  5. Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用

    Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...

  6. 非Contorller类使用@Service中的方法

    组件扫描这种的是指bean,跟service没关系 service只能在Controller类中使用,如果别的类想使用,必须使用下面这种方法 内容来源:https://blog.csdn.net/u0 ...

  7. Spring5源码分析之启动类的相关接口和注解

    一些基础但是核心的知识总结: Spring Boot项目启动的时候需要加@Configuration. @ComponentScan @Configuration + @Bean 把第三方jar包注入 ...

  8. 配置类为什么要添加@Configuration注解呢?

    配置类为什么要添加@Configuration注解呢? 本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 推荐阅读: Spring官网阅读 | 总结篇 Spring ...

  9. Spring注解驱动开发04(给容器中注册组件的方式)

    给容器中注册组件的方式 1. 组件注解标注 + 包扫描(适用于自己写的类) //控制层组件 @Controller public class PersonController { } //业务逻辑层组 ...

  10. 基于ImportBeanDefinitionRegistrar和FactoryBean动态注入Bean到Spring容器中

    基于ImportBeanDefinitionRegistrar和FactoryBean动态注入Bean到Spring容器中 一.背景 二.实现方案 1.基于@ComponentScan注解实现 2.基 ...

随机推荐

  1. dubbo hessian+dubbo协议

    Dubbo缺省协议采用单一长连接和NIO异步通讯,适合于小数据量大并发的服务调用,以及服务消费者机器数远大于服务提供者机器数的情况 Hessian协议用于集成Hessian的服务,Hessian底层采 ...

  2. thymeleaf 拼接字符串与变量

    参考https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html <span th:text="'The name o ...

  3. 52. N-Queens II (Array; Back-Track)

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  4. ArrayList LinkList比较

    1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构.      2.对于随机访问get和set,ArrayList优于LinkedList,因为ArrayLi ...

  5. 分割回文串 II · Palindrome Partitioning II

    [抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文. 返回s符合要求的的最少分割次数. [思维问题]: 不知道要用预处理字符串降低复杂度 [一句话思路]: 先把预处理获得s中回文串的 ...

  6. 【JDK1.8】JUC——ReentrantLock

    一.前言 在之前的几篇中,我们回顾了锁框架中比较重要的几个类,他们为实现同步提供了基础支持,从现在开始到后面,就开始利用之前的几个类来进行各种锁的具体实现.今天来一起看下ReentrantLock,首 ...

  7. ubuntu14.04安装opengl

    OpenGL 是一套由SGI公司发展出来的绘图函式库,它是一组 C 语言的函式,用于 2D 与 3D 图形应用程式的开发上. 不可或缺的就是编译器与基本的函式库,如果系统没有安装的话,依照下面的方式安 ...

  8. PHP-GTK的demo在windows下运行出现的问题

    I am trying to use Firebird 2.5.2.26539 with wamp,When i enable the extensions of firebird in php: - ...

  9. db2 sql调优

    当我们发现某个SQL语句执行很慢时,可以通过查看它的访问计划来定位原因,如是否执行了合适的索引.是否采用了正确的连接方法等.但是我们发现很多用户对访问计划的生成和解释工具的使用存在很多疑惑,本文通过一 ...

  10. Codeforces 709B 模拟

    B. Checkpoints time limit per test:1 second memory limit per test:256 megabytes input:standard input ...