spring MVC定时器注解配置说明

本文介绍了Spring MVC中使用@Scheduled注解进行定时任务配置的方法,包括spring-servlet.xml的配置,以及@Scheduled注解的fixedDelay、fixedRate、initialDelay和cron表达式的使用方式,详细解析了cron表达式的各个字段含义,并提供了多个示例。
摘要由CSDN通过智能技术生成

一、spring-servlet.xml配置

1、<beans> 标签加上命名空间和 schemaLocation:

<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.3.xsd

">


2、 配置任务调度驱动和bean

<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<!-- 开启这个配置,识别@Scheduled注解 -->
<task:annotation-driven executor="executor" scheduler="scheduler" />


附:我的springMVC-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"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"    
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="     
           http://www.springframework.org/schema/beans     
           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd     
           http://www.springframework.org/schema/context     
           http://www.springframework.org/schema/context/spring-context-4.3.xsd    
           http://www.springframework.org/schema/mvc     
           http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
           http://www.springframework.org/schema/task 
           http://www.springframework.org/schema/task/spring-task-4.3.xsd
           ">  

    <!-- 启动扫描的controller,多个包可用','隔开, 扫描包下面的子包,可用.*  -->  
    <!--  Spring容器优先加载由ServletContextListener(对应applicationContext.xml)产生的父容器,而SpringMVC(对应mvc_dispatcher_servlet.xml)产生的是子容器。子容器Controller进行扫描装配时装配的@Service注解的实例是没有经过事务加强处理,即没有事务处理能力的Service,而父容器进行初始化的Service是保证事务的增强处理能力的。如果不在子容器中将Service exclude掉,此时得到的将是原样的无事务处理能力的Service,因为在多上下文的情况下,如果同一个bean被定义两次,后面一个优先。 -->
    <context:component-scan base-package="com.med.web.controller, com.hpe.web.*,com.hpe.web.dao.impl,com.hpe.web.controller,com.hpe.web.service, com.hpe.task">
    	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan> 
    <!-- 启用spring mvc 注解 -->
    <context:annotation-config />
    
	<task:executor id="executor" pool-size="5" />  
	<task:scheduler id="scheduler" pool-size="10" />
	<!-- 开启这个配置,识别@Scheduled注解   -->  
    <task:annotation-driven executor="executor" scheduler="scheduler" />
    
    <!--  主要作用于@Controller,激活该模式  
        下面是一种简写形式,完全可以手动配置替代这种简写形式;  
         它会自动注册 DefaultAnnotationHandlerMapping 与 AnnotationMethodHandlerAdapter 两个bean, 完成请求和注解POJO的映射
           是spring MVC为@Controllers分发请求所必须的   -->  
    <mvc:annotation-driven/>  
    
    <!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/views/目录下,查找XXX.jsp文件-->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/view/"></property>  
        <property name="suffix" value=".jsp"></property>  
    </bean> 
    
    <!-- don't handle the static resource 将静态资源的处理经由Spring MVC框架交回Web应用服务器处理。而<mvc:resources />更进一步,由Spring MVC框架自己处理静态资源,并添加一些有用的附加值功能。 -->
    <!--  <mvc:default-servlet-handler /> -->
     
     <!-- SpringMVC上传文件专用 -->
    <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
        <property name="maxUploadSize" value="3221225472"></property>
        <property name="maxInMemorySize" value="4096"></property>
    </bean>
     
     <!-- 全局配置     
    <mvc:interceptors> 
        <bean class="com.capinfo.common.interceptor.PermissionInterceptor"></bean>  
    </mvc:interceptors>  
	-->
	
    <!--  
       配置/WEB-INF/views/目录里面的jsp文件不被DispatcherServlet处理,可以直接通过浏览器访问。  
       view-name - /WEB-INF/views/里面的jsp文件名(不许后缀即可)  
       path -  访问地址  
  
    <mvc:view-controller path="/header" view-name="header"/>      --> 
  
    
    
    <!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring bean对象 
	<bean class="com.capinfo.common.SpringContextHolder" lazy-init="false" />
    -->
    <!-- 对静态资源文件的访问  方案一 (二选一)
    <mvc:default-servlet-handler/> 
    -->
     <!-- 对静态资源文件的访问  方案二 (二选一)
	<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>  
	<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>  
	<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>  -->

	<!-- 全局异常处理 -->
	<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
	    <!-- 异常提示页面
	    <property name="exceptionMappings">
			<props>
				<prop key="com.capinfo.user.exception.LoginException">loginError</prop>
			</props>
		</property>  -->
	    <property name="defaultErrorView">    
	        <value>error</value>  
	    </property>  
	    <property name="defaultStatusCode">    
	        <value>500</value>  
	    </property>     
		<property name="warnLogCategory">    
	        <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver</value>  
	    </property> 
	</bean> 
	
</beans>



二、代码注解配置说明:

  注解@Scheduled 可以作为一个触发源添加到一个方法中,例如,以下的方法将以一个固定延迟时间5秒钟调用一次执行,这个周期是以上一个调用任务的完成时间为基准,在上一个任务完成之后,5s后再次执行:

1
2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值