【Quartz】基于Spring注解方式配置Quartz
2016-11-18 16:20
381 查看
林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka
在上讲【Quartz】Spring3.2.9+Quqrtz2.2.1实现定时实例中,我们使用了XML的方式来配置Quartz定时任务,虽然比用API的方式简便多了,但是Spring还支持基本注解的方式来配置。这样做不仅更加简单,而且代码量也更加少了。
新建一个Java工程,导入要用到的包,Spring3.2、Quartz2.2.1、aopalliance-1.0.jar、commons-logging-1.2.jar。整个工程目录如下:
本文工程免费下载
1、配置需要调度的类,并添加注解
[java]
view plain
copy
package com.mucfc;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
*事件类,基于Spring注解方式
*作者 林炳文(ling20081005@126.com 博客:http://blog.csdn.net/evankaka)
*时间 2015.4.29
*/
@Component
public class MyJob {
public MyJob(){
System.out.println("MyJob创建成功");
}
@Scheduled(cron = "0/1 * * * * ? ")//每隔1秒隔行一次
public void run(){
System.out.println("Hello MyJob "+
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));
}
}
2、首先要配置我们的beans.xml,在xmlns 多加下面的内容
[html]
view plain
copy
xmlns:task="http://www.springframework.org/schema/task"
3、然后xsi:schemaLocation多加下面的内容
[java]
view plain
copy
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
4、最后是我们的task任务扫描注解
[java]
view plain
copy
<!--开启这个配置,spring才能识别@Scheduled注解-->
<task:annotation-driven/>
5、自动配置扫描位置:
[java]
view plain
copy
<!-- 自动扫描注解的bean -->
<context:component-scan base-package="com.mucfc"/>
6、整个文档如下
[html]
view plain
copy
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!--开启这个配置,spring才能识别@Scheduled注解-->
<task:annotation-driven/>
<!-- 自动扫描注解的bean -->
<context:component-scan base-package="com.mucfc"/>
</beans>
7、使用
[java]
view plain
copy
package com.mucfc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
}
}
输出结果:
一旦这个xml被加载进来来,就会自动创建bean的实例,并且开始定时任务了
需要注意的几点:
1、spring的@Scheduled注解 需要写在实现上
2、 定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true、具体就去百度google吧)
3、实现类上要有组件的注解@Component
林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka
本文工程免费下载
在上讲【Quartz】Spring3.2.9+Quqrtz2.2.1实现定时实例中,我们使用了XML的方式来配置Quartz定时任务,虽然比用API的方式简便多了,但是Spring还支持基本注解的方式来配置。这样做不仅更加简单,而且代码量也更加少了。
新建一个Java工程,导入要用到的包,Spring3.2、Quartz2.2.1、aopalliance-1.0.jar、commons-logging-1.2.jar。整个工程目录如下:
本文工程免费下载
1、配置需要调度的类,并添加注解
[java]
view plain
copy
package com.mucfc;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
*事件类,基于Spring注解方式
*作者 林炳文(ling20081005@126.com 博客:http://blog.csdn.net/evankaka)
*时间 2015.4.29
*/
@Component
public class MyJob {
public MyJob(){
System.out.println("MyJob创建成功");
}
@Scheduled(cron = "0/1 * * * * ? ")//每隔1秒隔行一次
public void run(){
System.out.println("Hello MyJob "+
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));
}
}
2、首先要配置我们的beans.xml,在xmlns 多加下面的内容
[html]
view plain
copy
xmlns:task="http://www.springframework.org/schema/task"
3、然后xsi:schemaLocation多加下面的内容
[java]
view plain
copy
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
4、最后是我们的task任务扫描注解
[java]
view plain
copy
<!--开启这个配置,spring才能识别@Scheduled注解-->
<task:annotation-driven/>
5、自动配置扫描位置:
[java]
view plain
copy
<!-- 自动扫描注解的bean -->
<context:component-scan base-package="com.mucfc"/>
6、整个文档如下
[html]
view plain
copy
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!--开启这个配置,spring才能识别@Scheduled注解-->
<task:annotation-driven/>
<!-- 自动扫描注解的bean -->
<context:component-scan base-package="com.mucfc"/>
</beans>
7、使用
[java]
view plain
copy
package com.mucfc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
}
}
输出结果:
一旦这个xml被加载进来来,就会自动创建bean的实例,并且开始定时任务了
需要注意的几点:
1、spring的@Scheduled注解 需要写在实现上
2、 定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true、具体就去百度google吧)
3、实现类上要有组件的注解@Component
林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka
本文工程免费下载
相关文章推荐
- 【Quartz】基于Spring注解方式配置Quartz
- 基于Spring注解方式配置Quartz
- spring 基于XML和注解的两种事务配置方式
- Spring简单配置二-基于注解方式
- Spring_基于注解的方式配置bean
- Java 基于quartz实现定时 之一(注解方式配置)
- 基于spring注解方式配置和使用spring AOP
- Spring 事务基于注解方式的配置
- 基于springboot搭建dubbo框架(注解和xml配置两种方式)
- spring Quartz基于配置文件和注解的实现
- 8 -- 深入使用Spring -- 4...5 AOP代理:基于注解的“零配置”方式
- Spring AOP基于注解的“零配置”方式实现以及一些其他知识点
- Spring中Bean的配置:基于注解的方式
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
- spring与hibernate整合配置基于Annotation注解方式管理实务
- Spring基于注解的零配置方式的面向切面编程
- spring aop的使用(注解方式以及基于xml配置方式)
- Spring基于注解的方式配置bean的实例
- Spring 3.0.5 MVC 基于注解ehcache.xml 配置方式
- Spring 3.0.5 MVC 基于注解ehcache.xml 配置方式