关闭

springmvc quartz maven web

标签: spring mvcquartz
626人阅读 评论(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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:244839次
    • 积分:4599
    • 等级:
    • 排名:第7070名
    • 原创:200篇
    • 转载:135篇
    • 译文:0篇
    • 评论:49条
    文章分类
    最新评论