使用注解时,如果配置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">
            

相关文章:

  • 2021-12-11
  • 2021-07-18
  • 2022-12-23
  • 2021-06-28
  • 2021-06-04
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-02
  • 2021-05-14
  • 2021-11-23
  • 2021-12-03
  • 2021-10-09
  • 2022-12-23
相关资源
相似解决方案