关闭

springmvc quartz maven web

标签: spring mvcquartz
743人阅读 评论(0) 收藏 举报
分类:

1、首先把目录结构列出来(这里项目是建立在springmvc demo项目上的:点击打开链接):



2、pom文件:

<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>timertask</groupId>
  <artifactId>timertask</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
  <dependencies>    
    <dependency>  
        <groupId>jstl</groupId>  
        <artifactId>jstl</artifactId>  
        <version>1.2</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-web</artifactId>  
        <version>3.1.1.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-webmvc</artifactId>  
        <version>3.1.1.RELEASE</version>  
    </dependency>  
    <dependency>  
		<groupId>org.springframework</groupId>  
		<artifactId>spring-context-support</artifactId>  
		<version>3.1.1.RELEASE</version>  
    </dependency>
    <dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-tx</artifactId>
		<version>3.1.1.RELEASE</version>
	</dependency>
    <dependency>  
		<groupId>org.quartz-scheduler</groupId>  
		<artifactId>quartz</artifactId>  
		<version>1.8.4</version>  
    </dependency>  
      
  </dependencies>
</project>


3、web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app 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">  
  
    <display-name>Spring3MVC</display-name>  
    <servlet>  
        <servlet-name>spring</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
  
    <servlet-mapping>  
        <servlet-name>spring</servlet-name>  
        <url-pattern>*.html</url-pattern>  
     </servlet-mapping>  
  
     <welcome-file-list>  
        <welcome-file>index.jsp</welcome-file>  
    </welcome-file-list>   
</web-app>


4、spring-servlet.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"
	xmlns:context="http://www.springframework.org/schema/context"
	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">

	<context:component-scan base-package="com.busymonkey" />  
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<!-- 定义目标bean和bean中的方法 -->
	<bean id="timerTaskJob" class="com.quartz.timerTask" />
	<bean id="SpringQtzJobMethod"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref bean="timerTaskJob" />
		</property>
		<property name="targetMethod">  <!-- 要执行的方法名称 -->
			<value>printMethod</value>
		</property>
	</bean>

	<!-- ======================== 调度触发器 ======================== -->
	<bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="SpringQtzJobMethod"></property>
		<property name="cronExpression" value="0/5 * * * * ?"></property>
	</bean>

	<!-- ======================== 调度工厂 ======================== -->
	<bean id="SpringJobSchedulerFactoryBean"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="CronTriggerBean" />
			</list>
		</property>
	</bean>

</beans>


5、index.jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
 <title>Spring 3.0 MVC demo</title>  
</head>  
<body>  
 <a href="hello.html">Say Hello</a>  
</body>  
</html>


6、hello1.jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
    pageEncoding="ISO-8859-1"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
<title>Insert title here</title>  
</head>  
<body>  
<p>hello1</p>  
</body>  
</html>


7、timerController.java :

package com.busymonkey;

import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.servlet.ModelAndView;  
  
@Controller  
public class timerController {   
    @RequestMapping("/hello")  
    public ModelAndView hello() {   
        String message = "Hello World, Spring 3.0!";  
        System.out.println(message);  
        return new ModelAndView("hello1", "message", message);  
    }   
}


8、timerTask.java :

package com.quartz;

public class timerTask {
	public void printMethod() {
		System.out.println("hello!");
	}
}



这里说一下遇到的问题:

1、少依赖:

<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-tx</artifactId>
		<version>3.1.1.RELEASE</version>
	</dependency>

如果没有加这个依赖,就会报这个错误:

严重: Context initialization failed
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.quartz] for bean with name 'timerTaskJob' defined in 

ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.quartz



2、在bean配置的时候,如果没有选中要定时运行的类所在完整包的路径(加上类名),会报错:

严重: StandardWrapper.Throwable
java.lang.NoClassDefFoundError: org/springframework/transaction/TransactionException
	at java.lang.Class.getDeclaredConstructors0(Native Method)
	at java.lang.Class.privateGetDeclaredConstructors(Class.java:2585)
	at java.lang.Class.getDeclaredConstructors(Class.java:1906)





补:上面关于quartz配置的添加是直接放在spring-servlet.xml中的,如果想要和此文件分开,那么就在同样是 /WEB-INF 目录下新建一个 quartz-config.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"
	xmlns:context="http://www.springframework.org/schema/context"
	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">
<!-- 定义目标bean和bean中的方法 -->
	<bean id="timerTaskJob" class="com.quartz.timerTask" />
	<bean id="SpringQtzJobMethod"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref bean="timerTaskJob" />
		</property>
		<property name="targetMethod">  <!-- 要执行的方法名称 -->
			<value>printMethod</value>
		</property>
	</bean>

	<!-- ======================== 调度触发器 ======================== -->
	<bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="SpringQtzJobMethod"></property>
		<property name="cronExpression" value="0/5 * * * * ?"></property>
	</bean>

	<!-- ======================== 调度工厂 ======================== -->
	<bean id="SpringJobSchedulerFactoryBean"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="CronTriggerBean" />
			</list>
		</property>
	</bean>

</beans>



然后在 web.xml 文件中加入监听此文件的配置,内容如下:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app 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">  
  
    <display-name>Spring3MVC</display-name>  
    <servlet>  
        <servlet-name>spring</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    
	<context-param>
    	<param-name>contextConfigLocation</param-name>
    	<param-value>
      		/WEB-INF/quartz-config.xml
    	</param-value>
  	</context-param>
  	
  	<listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>
  
    <servlet-mapping>  
        <servlet-name>spring</servlet-name>  
        <url-pattern>*.html</url-pattern>  
     </servlet-mapping>  
  
     <welcome-file-list>  
        <welcome-file>index.jsp</welcome-file>  
    </welcome-file-list>   
</web-app>



1
0
查看评论

springmvc中实现quartz定时任务(每分钟的第3秒执行任务调度方法)

1:实现触发器,最大的问题是jar包的处理(*.jar定时jar和sourcecode): 此处,最关键的jar为第二个,名字最长。 2:触发器在web.xml中配置: <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-in...
  • typa01_kk
  • typa01_kk
  • 2015-06-24 12:27
  • 15068

利用SpringMvc和MyBatis实现员工列表显示页面

利用SpringMvc和MyBatis实现员工列表显示页面   首先使用MapperScannerConfigurer将带有@MyBatisRespository注解的Dao接口扫描生成MapperFacotryBean实例   然后通过MapperFactoryBean...
  • llpp8888
  • llpp8888
  • 2016-08-21 14:02
  • 3114

maven-quartz(定时任务)最简单版本

  • 2016-05-12 13:53
  • 39KB
  • 下载

Quartz(09) quartz spring web 项目的整合(终极版)

这一章我们将采用最简单的一种方式整合quartz spring web. 达到的效果是,我们只需要编写自己的job类,关于job,trigger 的配置信息都存放到数据库.(注:我们公司的的项目就是这么配置的,非常方便)
  • china_shrimp
  • china_shrimp
  • 2016-08-07 21:03
  • 1721

Spring web项目使用Quartz定时任务

配置定时器在Spring中
  • wang542943220
  • wang542943220
  • 2017-02-20 14:14
  • 1044

spring+Quartz+maven 简单的定时任务

这里就不贴上maven的pom.xml的全部文件信息了,就把Quartz相应的需要的jar包贴上了: org.quartz-scheduler quartz 2.2.1 ...
  • shasiqq
  • shasiqq
  • 2016-03-31 10:09
  • 9456

Spring整合Quartz框架实现定时任务跑批(Maven完整版)

Quartz 介绍 Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java appli...
  • reggergdsg
  • reggergdsg
  • 2016-08-01 06:25
  • 10868

Maven 下的spring框架(4定时器quartz)

清明放假三天,没有时间写博客,今天把定时器quartz的使用方法贴出来。通过前三篇的文章,大家的框架都是可以运行的了,那么下面开始配置quartz。 首先,先把jar包导进来 org.quartz-scheduler quartz 2.2.2 然后在applicati...
  • viewgroup
  • viewgroup
  • 2016-04-05 14:12
  • 2426

spring mvc-quartz的maven配置管理项目

在项目中
  • zzq19900502
  • zzq19900502
  • 2014-09-20 10:33
  • 2050

spring+quartz+maven项目实例

  • 2015-12-19 16:37
  • 12KB
  • 下载
    个人资料
    • 访问:300318次
    • 积分:5215
    • 等级:
    • 排名:第6246名
    • 原创:201篇
    • 转载:156篇
    • 译文:0篇
    • 评论:68条
    文章分类
    最新评论