小编典典

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

spring

我正在尝试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>

这是我的服务班级:

@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());
  }
}

当在我的eclispe + junit中进行测试时,它工作正常,当测试timeConsumingJob方法时,我可以看到secondly()继续输出消息。

但是,当部署到容器(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)

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

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

(Spring 3.0.5 , Resin 4.0.13)

------------ updated ---------

在深入研究之后,我发现app.xml是由另一个xml导入的。也许这是导致task:annotation-driven无法正常工作的原因。

好了,在重新安排了一些bean的位置之后,问题就解决了,但是我仍然感到困惑。(因为它工作正常,而other.xml在app.xml中需要bean)


阅读 1021

收藏
2020-04-21

共1个答案

小编典典

应用程序上下文已初始化两次,但org.springframework.scheduling.config.AnnotationDrivenBeanDefinitionParser第二次注册bean ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME失败。

我在单元测试中遇到了此问题,其中在父测试类和子测试类上都意外地使用了@ContextConfiguration(“ / path / to / applicationContext.xml”)(默认值为heritLocations true)。

2020-04-21