spring scheduled annotation 实例一

来源:互联网 发布:乐视视频无法连接网络 编辑:程序博客网 时间:2024/06/05 18:51

创建一个maven工程,并转化成web工程。不懂怎么转化成web。请看这里实例。http://lzj0470.iteye.com/admin/blogs/1926744

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>quartz_annotation</groupId>  <artifactId>quartz_annotation</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>war</packaging>  <dependencies>  <dependency>    <groupId>org.quartz-scheduler</groupId>    <artifactId>quartz</artifactId>    <version>2.1.7</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-core</artifactId>    <version>3.2.4.RELEASE</version></dependency> <dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-web</artifactId>    <version>3.2.2.RELEASE</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-orm</artifactId>    <version>3.2.2.RELEASE</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-tx</artifactId>    <version>3.2.2.RELEASE</version> </dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-context-support</artifactId>    <version>3.2.2.RELEASE</version></dependency><dependency><groupId>ojdbc</groupId><artifactId>ojdbc</artifactId><version>14</version></dependency> <dependency>    <groupId>org.quartz-scheduler</groupId>    <artifactId>quartz-oracle</artifactId>    <version>2.1.7</version></dependency>  </dependencies></project>

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 加载Spring Framework容器 -->  <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param><param-name>contextConfigLocation</param-name><param-value>WEB-INF/classes/applicationContext.xml</param-value>  </context-param>   <display-name>quartz_annotation</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.2.xsd"> <!-- 加载应用组件 --><context:component-scan base-package="com.newyulong.spring.scheduling" /><task:annotation-driven/></beans>
注意点:

< !-- 定时器开关 开始--><task:annotation-driven/>< !-- 定时器开关 结束-->

a)、需要在xmlns里面加入:xmlns:task="http://www.springframework.org/schema/task"b)、在xsi:schemaLocation中加入http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.0.xsd

<!-- 加载应用组件 --><context:component-scan base-package="com.newyulong.spring.scheduling" />

import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class AnnotationQuartz {@Scheduled(cron="0 0/2 * * * ?")public void test() {System.out.println("AnnotationQuartz Testing...");}}





0 0