title: Why @Scheduled and <task:scheduled ../> behave differently for dependencies injection in Spring 3.1?
body: <p>I need to schedule a task using Spring 3.1.
  I'm using Roo's with ActiveRecord pattern for entities.</p>
  
  <p>Method to be scheduled is very simple, in a dedicated service bean:</p>
  
  <pre><code>public void sessionCleanup() {
      featureSessionManager.sessionCleanup();
  }
  </code></pre>
  
  <p>...configured as:</p>
  
  <pre><code>&lt;bean id="scheduledTaskService" class="it.blablabla.ScheduledTaskService"&gt;
      [...dependencies...]
  &lt;/bean&gt;
  </code></pre>
  
  <p>If I schedule it using <code>@Scheduled(fixedRate=120000)</code> and enabling annotation-driven scheduling with <code>&lt;task:annotation-driven/&gt;</code> everything works as expected.</p>
  
  <p>But if I want to schedule the same method using XML configuration...</p>
  
  <pre><code>&lt;task:scheduler id="scheduler" pool-size="1"/&gt;
  &lt;task:scheduled-tasks scheduler="scheduler" &gt;
      &lt;task:scheduled ref="scheduledTaskService" method="sessionCleanup" fixed-rate="120000"/&gt;
  &lt;/task:scheduled-tasks&gt;
  </code></pre>
  
  <p>...application throws an exception on initialization.</p>
  
  <pre><code>2012-07-24 12:26:56,293 [pool-2-thread-1] INFO  org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler - Initializing ExecutorService  'scheduler'
  2012-07-24 12:26:56,671 [scheduler-1] ERROR org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task.
  java.lang.IllegalStateException: Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)
      at it.blablabla.model.AbstractFeatureSession_Roo_Jpa_ActiveRecord.entityManager_aroundBody0(AbstractFeatureSession_Roo_Jpa_ActiveRecord.aj:38)
      at it.blablabla.model.AbstractFeatureSession_Roo_Jpa_ActiveRecord.ajc$interMethod$it.blablabla_model_AbstractFeatureSession_Roo_Jpa_ActiveRecord$it.blablabla_model_AbstractFeatureSession$entityManager(AbstractFeatureSession_Roo_Jpa_ActiveRecord.aj:1)
      at it.blablabla.model.AbstractFeatureSession.entityManager(AbstractFeatureSession.java:1)
      at it.blablabla.model.AbstractFeatureSession_Roo_Jpa_ActiveRecord.ajc$interMethodDispatch1$it.blablabla_model_AbstractFeatureSession_Roo_Jpa_ActiveRecord$it.blablabla_model_AbstractFeatureSession$entityManager(AbstractFeatureSession_Roo_Jpa_ActiveRecord.aj)
      at it.blablabla.model.FeatureSession.updateAsDroppedAllExpiredAndNotDropped_aroundBody10(FeatureSession.java:245)
      at it.blablabla.model.FeatureSession.updateAsDroppedAllExpiredAndNotDropped(FeatureSession.java:1)
      at it.blablabla.service.FeatureSessionManagementServiceImpl.dropExpiredSessions(FeatureSessionManagementServiceImpl.java:154)
      at it.blablabla.service.FeatureSessionManagementServiceImpl.sessionCleanup(FeatureSessionManagementServiceImpl.java:178)
      at it.blablabla.task.ScheduledTaskWorker.sessionCleanup(ScheduledTaskWorker.java:37)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64)
      at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
      at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317)
      at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:180)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:204)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:662)
  </code></pre>
  
  <p>Apparently it fails to inject EntityManager into entities instantiated by the task.
  (Roo's ActiveRecords are <code>@Configurable</code> and EntityManager is injected on instance creation) </p>
  
  <p>This happens either in Tomcat or in a JUnit.</p>