Spring的@Scheduled错误:上下文中只能存在一个AsyncAnnotationBeanPostProcessor

sma*_*ufo 25 spring scheduler crontrigger spring-3

我正在尝试Spring 3的@Scheduled注释.这是我的配置(app.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:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  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/context http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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
      "
>

  <context:component-scan base-package="destiny.web"/>  
  <context:annotation-config/>
  // other beans

  <task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
  <task:executor  id="myExecutor"  pool-size="5"/>
  <task:scheduler id="myScheduler" pool-size="10"/>
</beans>
Run Code Online (Sandbox Code Playgroud)

这是我的服务类:

@Service
public class ServiceImpl implements Service , Serializable
{
  //other injections

  @Override
  @Transactional
  public void timeConsumingJob()
  {
    try
    {
      Thread.sleep(10*1000);
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }
  }

  @Override
  @Scheduled(cron="* * * * * ?") 
  public void secondly()
  {
    System.err.println("secondly : it is " + new Date());
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的eclispe + junit中测试时工作正常,在测试timeConsumingJob方法时,我可以看到其次继续输出消息.

但是当部署到容器(Resin/4.0.13)时,它会抛出:

[11-03-26 12:10:14.834] {main} org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Only one AsyncAnnotationBeanPostProcessor may exist within the context.
Offending resource: class path resource [app.xml]
 at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
 at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
 at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:72)
 at org.springframework.scheduling.config.AnnotationDrivenBeanDefinitionParser.parse(AnnotationDrivenBeanDefinitionParser.java:82)
Run Code Online (Sandbox Code Playgroud)

我搜索但很少发现类似的情况,我认为这是最基本的设置,但不知道为什么它不起作用.

有人可以看一下吗?非常感谢 !

(Spring 3.0.5,Resin 4.0.13)

------------ 更新 ---------

在深入挖掘之后,我发现app.xml是由另一个xml导入的.也许这就是task:annotation-driven不起作用的原因.

好吧,重新安排一些豆子的位置后,它就解决了,但我仍然感到困惑.(因为它运行正常,而other.xml需要app.xml中的bean)

Tom*_*ský 15

应用程序上下文正在初始化两次,但org.springframework.scheduling.config.AnnotationDrivenBeanDefinitionParser第二次无法注册bean ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME.

我在单元测试中遇到了这个问题,其中@ContextConfiguration("/ path/to/applicationContext.xml")意外地在父测试类和子测试类上(默认值为inheritLocations为true).

  • 谢谢这个答案,它给了我最后的线索.我在我的web.xml中包含了app.xml,在java中包含了@ImportResource.在我开始使用任务命名空间之前哪个有效! (2认同)

Raj*_*esh 8

在实现我们自己的AsyncTaskExecutor并忘记删除默认值之后,我遇到过这个问题 <task: annotation-driven/>

检查您是否有这样的东西,如果是,则删除其中一项任务.

<task:annotation-driven executor="customAsyncTaskExecutor" scheduler="taskScheduler"/>

<task:annotation-driven/>
Run Code Online (Sandbox Code Playgroud)


Bab*_*yan 7

当spring <task:annotation-driven/>在配置XML中解析文本两次时会发生这种情况.

对我来说,这是怎么回事,因为这两个applicationContext-root.xmlapplicationContext-where-annotation-driven-is-specififed.xml我的是进口WEB.xml<context-param>部分.

只留下applicationContext-root.xmlWEB.xml解决了问题.


Bet*_*sta 6

当我复制applicationContext.xml并创建一个名为的新问题时,我遇到了这个问题applicationContextAdditional.xml.我没有试图找到原因,但两者都包含名称空间

<bean ...
    xmlns:task="http://www.springframework.org/schema/task"
    ...
    xsi:schemaLocation="
   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" >

    ...

</bean>
Run Code Online (Sandbox Code Playgroud)

当我从第二个删除命名空间时,我的问题得到了解决.也许它有助于某人.