spring Quartz基于配置文件和注解的实现

一、基于配置文件的实现

①编写需要调度的类

package com.study;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
//@Component
public class QuartzJob {
    public QuartzJob(){
        System.out.println("Quartzjob创建成功");
    }
    //@Scheduled(cron = "0/1 * *  * * ? ")
    public void run(){
        System.out.println("Quartz执行的任务调度");
    }
}

②设置配置文件spring-quartz.xml




 


    
    
    
    
    
    



    
    
    


 
 


    
        
            
        
    

③启动spring容器

package com.study;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    public static void main(String[] args) {
        System.out.println("启动spring容器");
        ApplicationContext ac = newClassPathXmlApplicationContext("classpath:spring-quartz.xml");
    }
}

二、基于注解的实现

①配置需要调度的类,并添加注解

package com.study;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class QuartzJob {
    public QuartzJob(){
        System.out.println("Quartzjob创建成功");
    }
    @Scheduled(cron = "0/1 * *  * * ? ")
    public voidrun(){
        System.out.println("Quartz执行的任务调度");
    }
}

②添加配置文件


    
    
    
    

③启动容器,这里通过配置web.xml启动


    
        contextConfigLocation
        classpath:spring-quartz2.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

你可能感兴趣的:(spring Quartz基于配置文件和注解的实现)