zoukankan      html  css  js  c++  java
  • spring task 配置

    使用注解时,如果配置default-lazy-init="true",则需要在类上加上@Lazy(false)。

    http://my.oschina.net/xldc/blog/297864

    摘要

    SpringMVC启动Task任务同时加载两次的解决方法

        关于 启动Task任务同时加载两次的解决方法:

            将spring MVC部分的定义另外建立一个文件,同时把Task配置放在此处,然后在web.xml文件中的处加载

    <servlet>
    		<servlet-name>SpringMVC</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>classpath:spring-mvc.xml</param-value>
    		</init-param>
    
    		<init-param>
    			<param-name>listings</param-name>
    			<param-value>false</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>

    =============转载原文===========

    项目使用的Spring版本比较旧是3.0.6版本,由于需要进行定时任务,就决定使用Spring自带的scheduled task。

    在网上找了很多文章,也查看了Spring3.0.6的官方文档,按照网上和文档所说,可以使用注解或者配置两种方法之一都行,但是我发现单独使用两种方法都不行,怎么配置任务都无法运行。

    最后看到一篇文章说两种方法同时用,才成功执行定时任务,可能是个Bug,我试了下,同时使用注解和XML配置后,任务确实成功执行了。

    XML配置中,只需要配置一个方法即可,其他方法也能跟着运行了,而且XML中配置的定时时间会被注解覆盖掉,只能先这么做了,期待高手解答原因。

    难道真的是Spring3.0.6的Bug??

    Spring配置如下:

    <?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:jee="http://www.springframework.org/schema/jee"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    	   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
           default-lazy-init="true" default-autowire="byName">
    
        <!-- 配置注解扫描 -->
        <context:annotation-config />
    
        <!-- 自动扫描的包名 -->
        <context:component-scan base-package="com.demo" />
    
        <!-- Spring定时器注解开关-->
        <task:annotation-driven />
        <!-- 此处对于定时时间的配置会被注解中的时间配置覆盖,因此,以注解配置为准 -->
        <task:scheduled-tasks scheduler="myScheduler">
            <task:scheduled ref="scheduledTaskManager" method="autoCardCalculate" cron="1/5 * * * * *"/>
        </task:scheduled-tasks>
        <task:scheduler id="myScheduler" pool-size="10"/>
    
        <aop:aspectj-autoproxy />
    
        <!-- 加载配置文件 -->
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:config.properties</value>
                </list>
            </property>
        </bean>
    </beans>

    执行任务的POJO类如下:

    package com.demo.schedule;
    
    import org.apache.log4j.Logger;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    import java.util.Date;
    /**
     * Created with IntelliJ IDEA.
     * Author: 
     * Date: 2013-10-09 14:39
     * Function: Spring定时任务管理
     */
    @Component("scheduledTaskManager")
    public class ScheduledTaskManager {
        /**
         * cron表达式:* * * * * *(共6位,使用空格隔开,具体如下)
         * cron表达式:*(秒0-59) *(分钟0-59) *(小时0-23) *(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT)
         */
    
        /**
         * 定时卡点计算。每天凌晨 02:00 执行一次
         */
        @Scheduled(cron = "0 0 2 * * *")
        public void autoCardCalculate() {
            System.out.println("定时卡点计算... " + new Date());
        }
    
        /**
         * 心跳更新。启动时执行一次,之后每隔1分钟执行一次
         */
        @Scheduled(fixedRate = 1000*60*1)
        public void heartbeat() {
            System.out.println("心跳更新... " + new Date());
        }
    
        /**
         * 卡点持久化。启动时执行一次,之后每隔2分钟执行一次
         */
        @Scheduled(fixedRate = 1000*60*2)
        public void persistRecord() {
            System.out.println("卡点持久化... " + new Date());
        }
    }
     
    • 粉丝: 3 博客数: 28 共码了 5423 字
    评论(2)
    骚男子
    使用注解时,如果配置default-lazy-init="true",则需要在类上加上@Lazy(false)。
     
    saraphines123

    引用来自“骚男子”的评论

    使用注解时,如果配置default-lazy-init="true",则需要在类上加上@Lazy(false)。
    79
  • 相关阅读:
    substring(x)和substring(x,y)的用法
    js 判断字符串中是否包含某个字符串
    js如何将纯数字字符串转换为long型
    union和union all的区别
    JavaScript 中 var 和 let 和 const 关键字的区别
    JS操作文件
    java 字符串和集合互相转换
    关于sql中in 和 exists 的效率问题,in真的效率低吗
    sql 同一张表查询不同数据合并之后关联查询
    Android input输入框 移动页面input手机键盘中的“搜索”按键
  • 原文地址:https://www.cnblogs.com/adolfmc/p/5839110.html
Copyright © 2011-2022 走看看