java – @Async通过代理类在Spring 3.2中创建一个异常

我似乎想出了一个问题.

我上课了

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

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

和一个包含的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" />

在我的代码的某些部分

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

但我得到了这个例外

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)

我可以理解它的代理类,但不确定为什么Spring不允许转换代理.

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

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

简而言之,我希望在调用时将方法设置为Async.

最佳答案 由于您有CGLIB,您可能希望将@Scope更改为

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