spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.27k stars 37.98k forks source link

Exception when @Scheduled and @Async placed on one bean [SPR-7566] #12223

Closed spring-projects-issues closed 5 years ago

spring-projects-issues commented 14 years ago

Nikita Koksharov opened SPR-7566 and commented

Here is my spring config:

\<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:sec="http://www.springframework.org/schema/security" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
  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-3.0.xsd
  http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
  http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

<task:annotation-driven/>

<bean class="com.tests.TestBean"/>

\

Here is my classes:

package com.tests;

import org.springframework.scheduling.annotation.*;

public class TestBean implements ITestBean {

@Scheduled(fixedDelay = 1000)
public void scheduleMethod() {

}

@Async
public void asyncMethod() {

}

}

package com.tests;

public interface ITestBean {

void asyncMethod();

}

On spring context creation i got follow exception:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ru.flexis.officewars.battle.TestBean#0' defined in class path resource [test-context.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: failed to prepare task at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) at org.unitils.spring.util.ApplicationContextManager.createInstanceForValues(ApplicationContextManager.java:117) ... 46 more Caused by: java.lang.IllegalStateException: failed to prepare task at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor$1.doWith(ScheduledAnnotationBeanPostProcessor.java:114) at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:452) at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:430) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:98) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) ... 55 more Caused by: java.lang.NoSuchMethodException: $Proxy12.scheduleMethod() at java.lang.Class.getMethod(Class.java:1605) at org.springframework.util.MethodInvoker.prepare(MethodInvoker.java:178) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor$1.doWith(ScheduledAnnotationBeanPostProcessor.java:111) ... 61 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ru.flexis.officewars.battle.TestBean#0' defined in class path resource [test-context.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: failed to prepare task at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) at org.unitils.spring.util.ApplicationContextManager.createInstanceForValues(ApplicationContextManager.java:117) ... 46 more Caused by: java.lang.IllegalStateException: failed to prepare task at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor$1.doWith(ScheduledAnnotationBeanPostProcessor.java:114) at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:452) at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:430) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:98) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) ... 55 more Caused by: java.lang.NoSuchMethodException: $Proxy12.scheduleMethod() at java.lang.Class.getMethod(Class.java:1605) at org.springframework.util.MethodInvoker.prepare(MethodInvoker.java:178) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor$1.doWith(ScheduledAnnotationBeanPostProcessor.java:111) ... 61 more


Affects: 3.0.4

5 votes, 6 watchers

spring-projects-issues commented 14 years ago

Juergen Hoeller commented

I'm afraid that you'll have to expose your scheduleMethod on the ITestBean interface, since @Async enforces an interface-based proxy for your TestBean - and @Scheduled invocations will be delegated to that proxy as well then.

Juergen

spring-projects-issues commented 14 years ago

Nikita Koksharov commented

Do you suggest to add "void scheduleMethod()" signature to ITestBean interface? But this method should be invoked by spring only. Don't want to add it to interface.

spring-projects-issues commented 13 years ago

chris marx commented

I don't really mind having to add it to an interface, but it might be nice if the error said as much, rather than having it say "failed to prepare task" and then searching google for this post...(thanks though, interface method added and it works)

spring-projects-issues commented 12 years ago

Archie Cobbs commented

I have seen this problem as well.

In my case, I had a method that originally looked like this:

@Scheduled(fixedDelay = 10000)
@Transactional
public void foo() {
    ...
}

and it worked fine. When I tried to add my own custom @AspectJ style annotation, it failed with the same exception as reported in this bug:

@Scheduled(fixedDelay = 10000)
@Transactional
@PostCompletionSupport
public void foo() {
    ...
}
spring-projects-issues commented 12 years ago

Archie Cobbs commented

Following up on my previous comment above...

The addition of the @PostCompletionSupport annotation was actually not necessary to trigger this bug: the bug still occurs without that annotation, if I simply add <aop:aspectj-autoproxy/> to the application context.

spring-projects-issues commented 5 years ago

Sébastien Deleuze commented

Closing groups of outdated issues. Please reopen if still relevant.