zoukankan      html  css  js  c++  java
  • quartzy的spring注入问题

    <!-- quartz定时器,定时器的类--自己用的类 -->
        <bean id="jobDetailFactoryBean" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <property name="jobClass" value="com.myk.oe.utils.QuartzTask"></property>
        </bean>
        <!-- 配置执行时间和规则 -->
        <bean id="quartzTask" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="jobDetailFactoryBean"></property>
            <!-- 5秒一次 -->       
            <property name="cronExpression" value="0/5 * * * * ?"></property>
        </bean>
        <!-- 将bean注入到定时器的类 -->
        <bean id="jobFactory" class="com.myk.oe.quartz.util.JobFactory"></bean>
    
        <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --> 
        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
            <property name="triggers">
                <list>
                    <ref bean="quartzTask" /><!-- 任务 -->
                </list>
            </property>
            <property name="jobFactory" ref="jobFactory"></property>
            <property name="autoStartup" value="true"/><!-- 自动开始 -->
        </bean>
    

      定义好定时任务的相关类,在类中调用dao或者service的时候会报空指针异常,

    在定时任务的类上加注解@DisallowConcurrentExecution就可以注入service和dao了

    @DisallowConcurrentExecution
    public class QuartzTask extends QuartzJobBean{
    
        @Resource(name="appointmentServiceImpl")
        private AppointmentService appointmentService;//这是服务类
    
        @Override
        protected void executeInternal(JobExecutionContext context)
                throws JobExecutionException {
    
        List<Appointment> appointments = appointmentService.getAppointments();//获取数据
            System.err.println(appointments);
        }
    }
  • 相关阅读:
    【书目20200917】影响力思维
    Spring AOP +自定义注解 + Spel表达式 实现审计日志
    SPRING BOOT 注解之OBJECTPROVIDER源码追踪
    Activiti~相关概念
    ELK~fluentd多行日志的收集
    k8s~向etc/hosts里添加内容
    maven~本地仓库的指定
    ssh~ Access denied问题解决
    MySQL如何绕过授予information_schema中对象时报ERROR 1044(4200)错误
    Zabbix如何监控SQL Server服务状态
  • 原文地址:https://www.cnblogs.com/chenKnowledgeConllection/p/8417029.html
Copyright © 2011-2022 走看看