spring mvc 定時任務

配置文件java

    <bean id="TestController" class="com.whty.mytest.controller.TestController"></bean>
    <bean id="printhahaha" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="TestController" />
        <property name="targetMethod" value="printhahaha"/>
        <property name="concurrent" value="false" />
    </bean>
    <bean id="printhahahaTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
        <property name="jobDetail" ref="printhahaha" /> 
        <!-- 每30秒執行 -->
        <property name="cronExpression" value="0/30 * * * * ?" />
    </bean>
    
    <!-- 觸發器工廠,將全部的定時任務都注入工廠-->  
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <!--  添加觸發器   -->
        <property name="triggers">
            <list>
            <ref local="printhahahaTrigger" />
            </list>
        </property>
        <!-- 鏈接線程池配置文件 -->
        <property name="configLocation" value="classpath:properties/quartz.properties" />
    </bean>

java類spring

package com.whty.mytest.controller;

@Controller
@RequestMapping("/myTest")
public class TestController {
    public void printhahaha(){
        System.out.println("哈哈哈哈哈哈哈哈");
    }
}
相關文章
相關標籤/搜索