• 关于Spring mvc注解中的定时任务的配置


    关于spring mvc注解定时任务配置

    简单的记载:避免自己忘记,不是很确定我理解的是否正确。有错误地方望请大家指出。

    1,定时方法执行配置:

      (1)在applicationContext.xml中加入以下配置

    xmlns:task="http://www.springframework.org/schema/task" 

    在xsi:schemaLocation地址下引入

    http://www.springframework.org/schema/task  
            http://www.springframework.org/schema/task/spring-task-3.2.xsd  

    加入task引入

    <task:annotation-driven/>  

    applicationContext.xml配置完成。

    (2)在你需要定时的方法上面加入注解@Scheduled(fixedRate = 10 * 1000)

     /**
         * 每隔十分钟执行一次
         * @Scheduled(fixedRate = 60 * 10 * 1000) 
         */
    @Scheduled(fixedRate =  60 * 10 * 1000)
        public void handle() {
            //写入自己的逻辑代码
     }

    到此我理解的第一种结束

    2,在配置用xml配置定时任务

    (1)新建一个xml文件:配置定时任务

    <?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:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    
        <!-- 自己定义的定时类  -->
        <bean id="smsAutoSendInfoQuartzJob" class="com.minxinloan.sms.auto.quartz.SmsAutoSendInfoQuartzJob" /><!-- 自己项目中需要定时的处理逻辑类 -->
        
        
        <bean id="smsAutoSendInfoDetail"
            class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject">
                <ref bean="smsAutoSendInfoQuartzJob" /><!-- 注入需要定时执行的类  -->
            </property>
            <property name="targetMethod">
                <value>atuoRemitInfoTask</value> <!-- 类中的方法名称配置  -->
            </property>
        </bean>
        <!-- 设置定时任务的时间  -->
        <bean id="smsAutoSendInfoTrigger"
            class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail">
                <ref bean="smsAutoSendInfoDetail" />
            </property>
            <property name="cronExpression">
                <value>0 0 * * * ?</value> <!-- 这个可能是每天早上凌晨触发,这个参数我记不太清楚,自己可以根据需要查询 -->
            </property>
        </bean>
        <!-- end -->
        <!-- 启动触发器的配置开始 -->
        <bean name="startCrmQuertz" lazy-init="true" autowire="no"
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="smsAutoSendInfoTrigger" />
                </list>
            </property>
        </bean>
        <!-- 启动触发器的配置结束 -->
    </beans>

    (2)需要的jar包

    Spring-context-support-3.2.2.jar包

    差不多我理解的到此结束。

  • 相关阅读:
    HDFS Namenode 高可用
    旁路模式,numa、mmu、调整内存分页大小
    k8s 调度 GPU
    破解 CSDN 登录后才能复制
    微信小程序开发video遮罩功能(禁止拖动进度条)
    微信小程序之下拉刷新
    微信小程序JS中文排序
    vue2 和 vue3 路由使用对比
    Golang 操作mongo
    win10安装CUDA和cuDNN的正确姿势
  • 原文地址:https://www.cnblogs.com/puqiuxiaomao/p/3707491.html
Copyright © 2020-2023  润新知