@Async via proxy classes is creating an exception in Spring 3.2

1.5k views Asked by At

I seem to have come up with a problem.

I have a class

@Component
@Scope("prototype")
public class MyClass extends BaseClass {

....
...
 @Async
 public void doSomething() {
 ....   
 }
....
} 

and a Spring Config that contains

<context:annotation-config />
<context:component-scan base-package="com.company.project" />
<task:annotation-driven executor="taskExecutor"/>
<task:executor id="taskExecutor" pool-size="10" queue-capacity="10" />

and in some part of the code i have

BaseClass bean = springBeans.getBean(MyClass.class);

but i am getting this exception

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'myClass' must be of type [com.company.project.MyClass], but was actually of type [$Proxy19]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:361)

I can understand its a proxy class, but not sure why Spring is not allowing the Proxy to be converted.

I have the cglib 2.2 no dep on the class path, along with the Spring 3.2 core libs.

can anyone point to any clues as to fixing this ?

In short, i want a method to be Async when called.

1

There are 1 answers

3
Sotirios Delimanolis On

Since you have CGLIB, you might want to change the @Scope to

@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)