Spring Quartz动态设置触发时间问题

Spring使用Quartz框架来完成任务调度,有两种方法:
1. 利用JobDetailBean包装QuartzJobBean子类(即Job类)的实例。
2. 利用MethodInvokingJobDetailFactoryBean工厂Bean包装普通的Java对象(即Job类)。

作为开发使用第二种方法是比较方便的,一般在xml文件中配置好。但是项目有时需要动态改变cronExpression的值,这里有 QuartzUtils demo 实现代码 。过程中遇到了下面的几个问题。

1.自动注入SchedulerFactoryBean失败

提示: Failed to convert property value of type [org.quartz.impl.StdScheduler] to required type [org.springframework.scheduling.quartz.SchedulerFactoryBean]

原代码实现:

@Autowired
private SchedulerFactoryBean schedulerFactoryBean;

提示类型不匹配,问题参考网址:http://blog.csdn.net/neutrojan/article/details/8747275 只需修改为下面的代码,便可以成功注入:

@Autowired
private Scheduler scheduler;

2.如何获取xml中配置的bean

除了注入的方式获取schedule,也可以直接获取xml 对应schedule的bean。参考网址:http://blog.csdn.net/zsw12013/article/details/51701671 下面是通过方法5实现代码:

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

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

    }

    /**
     * 取得存储在静态变量中的ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        checkApplicationContext();
        return applicationContext;
    }

    /**
     * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        checkApplicationContext();
        return (T) applicationContext.getBean(name);
    }

    /**
     * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        checkApplicationContext();
        return (T) applicationContext.getBeansOfType(clazz);
    }

    /**
     * 清除applicationContext静态变量.
     */
    public static void cleanApplicationContext() {
        applicationContext = null;
    }

    private static void checkApplicationContext() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
        }
    }

}

<bean id="myApplication" class="app.util.SpringContextUtil" />

//获取
Scheduler  scheduler = SpringContextUtil.getBean("mySheduler");

3.spring注解 @autowired @resource区别

参考网址:http://www.educity.cn/wenda/361917.html

  1. @Autowired默认是按类型装配对象的,默认情况下它要求依赖对象必须存在
  2. @Resource默认按名称装配,名称可以通过name属性指定。当找不到与名称匹配的bean时,才会按类型装配
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值