开发者社区 问答 正文

Spring+quartz整合,在JOB中获取不都Spring的上下文

Spring+quartz整合,在JOB中获取不都Spring的上下文。尝试了很多方式。 JOB类不是在配置文件中定义的。 是通过JobDetail添加的。

展开
收起
a123456678 2016-03-17 14:17:36 3372 分享 版权
1 条回答
写回答
取消 提交回答
  • 使用 Spring 提供的 Schedule 工厂创建 Schedule

    注意org.springframework.scheduling.quartz.SchedulerFactoryBean类中 applicationContextSchedulerContextKey 属性,用于设置 Spring上下文在 Schedule 上下文的 Key 值。

    例如将 Key 设置为 "applicationContext":

    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
    p:applicationContextSchedulerContextKey="applicationContext"/>
    
    
    在 Job 通过 Schedule上下文获取 Spring 上下文,例如:
    
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
     
        ApplicationContext applicationContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext");
     
    }
    2019-07-17 19:05:20
    赞同 展开评论