@Async 通过代理类在 Spring 3.2 中创建异常

Ben*_*Ben 5 java spring cglib spring-scheduled

我似乎想到了一个问题。

我有课

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

....
...
 @Async
 public void doSomething() {
 ....   
 }
....
} 
Run Code Online (Sandbox Code Playgroud)

和一个 Spring 配置,其中包含

<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" />
Run Code Online (Sandbox Code Playgroud)

在我的代码的某些部分

BaseClass bean = springBeans.getBean(MyClass.class);
Run Code Online (Sandbox Code Playgroud)

但我得到了这个例外

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)
Run Code Online (Sandbox Code Playgroud)

我可以理解它是一个代理类,但不知道为什么Spring不允许转换代理类。

我在类路径上有 cglib 2.2 no dep,以及 Spring 3.2 核心库。

任何人都可以指出解决此问题的任何线索吗?

简而言之,我希望一个方法在调用时是异步的。

Sot*_*lis 3

由于您有 CGLIB,您可能需要将其更改@Scope为

@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
Run Code Online (Sandbox Code Playgroud)