五)Spring + Quartz 复杂业务的两个问题:获取Spring上下文 和 自动注入服务类

配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:task="http://www.springframework.org/schema/task"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    <context:component-scan base-package="cn.zno" />
    <task:executor id="threadPoolTaskExecutor" pool-size="1" />

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobFactory">
            <bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory" />
        </property>
        <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
        <property name="startupDelay" value="0" />
        <property name="overwriteExistingJobs" value="true" />
        <property name="exposeSchedulerInRepository" value="true" />
        <property name="taskExecutor" ref="threadPoolTaskExecutor" />
        <property name="triggers">
            <list>
                <ref bean="cronTrigger_1" />
            </list>
        </property>
    </bean>

    <bean id="cronTrigger_1"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="jobDetail_1" />
        <property name="cronExpression" value="* * * * * ?" />
    </bean>
    <bean id="jobDetail_1"
        class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="cn.zno.job.Breathe" />
    </bean>
    <bean id="breath" class="cn.zno.job.Breathe" />
</beans>

为什么要配置这个

<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />

因为可以通过这个key取到Spring上下文。

配置1存在的问题:不能自动注入。

package cn.zno.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;

import cn.zno.service.AService;

public class Breathe implements Job {

//    @Autowired
//    private AService aService;

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        try {
//            aService.Show();
            ApplicationContext ctx = (ApplicationContext)context.getScheduler().getContext().get("applicationContextKey");
            AService aService = ctx.getBean(AService.class);
            aService.Show();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

配置一改:解决自动注入问题

change

        <property name="jobFactory">
            <bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory" />
        </property>

to

        <property name="jobFactory">
            <bean class="cn.zno.common.SpringBeanJobFactory" />
        </property>

cn.zno.common.SpringBeanJobFactory

package cn.zno.common;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringBeanJobFactory extends
        org.springframework.scheduling.quartz.SpringBeanJobFactory implements
        ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        this.applicationContext = applicationContext;

    }

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle)throws Exception {
        Object jobInstance = super.createJobInstance(bundle);
        applicationContext.getAutowireCapableBeanFactory().autowireBean(jobInstance);
        return jobInstance;
    }
}

另一种实现方式为:

package cn.zno.common;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;

public class SpringBeanJobFactory extends
        org.springframework.scheduling.quartz.SpringBeanJobFactory {

    @Autowired
    private AutowireCapableBeanFactory autowireCapableBeanFactory;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle)throws Exception {
        Object jobInstance = super.createJobInstance(bundle);
        autowireCapableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}
时间: 2024-08-09 01:22:31

五)Spring + Quartz 复杂业务的两个问题:获取Spring上下文 和 自动注入服务类的相关文章

Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea

文章大纲 在xml中声明bean和注入bean 在xml中声明bean和自动注入bean 自动扫描bean和自动注入bean 对自动扫描bean增加约束条件 首次接触spring请参考 Spring 3.0 学习-环境搭建和三种形式访问 1.典型的Spring XML 配置文件表头 <?xml version="1.0" encoding="UTF-8"?><!-- 一般化的Spring XML 配置 --> <beans xmlns=

spring 需要自动注入的类 可以申明为static 吗?

 @Autowired   private UserInfoService service;   private static UserInfoService service;

spring quartz 分布式任务计划

通过maven管理的spring mvc工程,且已经成功连接数据库. 数据库表结构 /*Table structure for table `qrtz_calendars` */ DROP TABLE IF EXISTS `qrtz_calendars`; CREATE TABLE `qrtz_calendars` ( `SCHED_NAME` varchar(120) NOT NULL, `CALENDAR_NAME` varchar(200) NOT NULL, `CALENDAR` blo

获取spring的ApplicationContext几种方式【转】

转自:http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html Java类获取spring 容器的bean 常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象 代码: 1 ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); 2 ac.getBean(&qu

Spring集成Struts、Hibernate----三大框架SSH(Spring、Struts和hibernate)

Spring 框架可以完成业务层的设计任务,Struts框架可以将表示层和业务层分离,而Hibernate框架可以提供灵活的持久层支持.下面介绍三大框架的集成环境: 1.配置Struts2. I.导入相关的JAR包. II.修改web.xml文件.Struts2的核心控制是通过过滤器(Filter)来实习的,所以在web.xml文件中需要进行过滤器的配置一边加载strust2框架.web.xml文件的代码如下: 1 <!-- 配置Struts2框架的核心Filter --> 2 <fil

Spring第八章:Spring自动注入

一.自动注入 1.在 Spring 配置文件中对象名和 ref=”id”id 名相同使用自动注入,可以不配置<property/> 2.两种配置办法 2.1 在<bean>中通过 autowire=”” 配置,只对这个<bean>生效 2.2 在<beans>中通过 default-autowire=””配置,表当当前文件中所有<bean>都是全局配置内容 2.3 自动注入代码 2.3.1测试自动注入的实体类,老师和学生类 package com

Spring 注解Autowired自动注入bean异常解决

错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined 错误的一般解决办法: 1.看xxbean是否已经注入,或者得到的bean名字错误. 2.看spring的配置文件<context:component-scan base-package="com.xx"></context:component-scan>是否扫描了 c

分析解决 spring quartz 中出现的执行两次问题

1. 问题描述 在开发询盘功能时,遇到一个需求,就是后台定时任务执行用电施工业务的工单下发. 使用的技术是 spring quartz,因为其他应用有先例,配置quartz 完成后,先写了一个 helloworld 测试下. 然而却发现,每次到定时时间后,程序都会执行两次. 2. 分析过程 先使用 bing 搜索了下看别人是否也遇到过类似问题,果然有. http://blog.csdn.net/jiang117/article/details/43077275 上面文档的作者,查找的原因是 Co

spring quartz执行两次问题

解决quartz定时任务被触发两次的问题: 其中<Host/>告诉tomcat,在启动的时候加载webapps下的所有项目工程文件,<Context/>又让tomcat再加载了一遍(一般情况下配置<Context/>,主要是由于想域名访问时将工程名去掉的原因配置),这种情况下会导致工程中的quartz定时被两次触发,执行两次. <Host/>里面的改成 autoDeploy="false" deployOnStartup="fa