27

Spring3の@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()が2番目にメッセージを出力し続けることがわかります。

しかし、コンテナ(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)

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

深く掘り下げてみると、app.xmlが別のxmlによってインポートされていることがわかりました。たぶんこれが機能しない理由task:annotation-drivenです。

さて、いくつかの豆の場所を再配置した後、それは解決されましたが、それでも私は戸惑いを感じます。(正常に機能し、other.xmlにはapp.xmlにBeanが必要なため)

4

7 に答える 7

15

アプリケーションコンテキストは 2 回初期化されていますが、org.springframework.scheduling.config.AnnotationDrivenBeanDefinitionParser は 2 回目の Bean ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME の登録に失敗します。

@ContextConfiguration("/path/to/applicationContext.xml") が誤って親テスト クラスと子テスト クラスの両方にある単体テストでこの問題が発生しました (デフォルト値は inheritLocations true)。

于 2011-11-03T15:08:34.013 に答える
8

独自の AsyncTaskExecutor を実装し、デフォルトを削除するのを忘れた後、これに一度直面しました<task:annotation-driven/>

このようなものがあるかどうかを確認し、ある場合はタスクの 1 つを削除します。

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

<task:annotation-driven/>
于 2014-03-28T22:56:59.823 に答える
7

<task:annotation-driven/>これは、Spring が構成 XML でテキストを 2 回解析したときに発生します。

applicationContext-root.xml私にとってこれは、との両方が私のセクションapplicationContext-where-annotation-driven-is-specififed.xmlにインポートされたために起こっていました。WEB.xml<context-param>

にのみ残すことで問題applicationContext-root.xmlWEB.xml解決しました。

于 2016-02-07T13:34:52.520 に答える
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>

2番目の名前空間から名前空間を削除すると、問題は解決しました。多分それは誰かを助けるでしょう。

于 2013-07-01T13:40:50.487 に答える