这是推动我疯狂的场景。春天 - 作为CGLIB代理的Classcast例外不能被强制
- 我有一个具有一个查找方法的类 - (+)
- createOther应该创建其它类型的目的createOther。其他实现OtherInterface和另外有一个方法doSomething标记@Async
- 由于其他实现OtherInterface,Spring给了我一个JDK代理,我不能转换为其他。
- 春季文档建议使用
<aop:config proxy-target-class="true">
- 但我是一个新手,并使用它似乎没有帮助。
问题:我如何告诉Spring我需要一个定位于其他类的CGLib代理?
以下代码因类classcastexception失败。
Exception in thread "main" java.lang.ClassCastException: $Proxy4 cannot be cast to scratch.Other
at scratch.App$$EnhancerByCGLIB$$82d16307.createOther(<generated>)
at scratch.App.main(App.java:19)
App.java:
public class App {
public Other createOther() {
throw new UnsupportedOperationException();
}
public static void main(final String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appcontext.xml");
App app = (App) context.getBean("app");
Other oth = app.createOther();
oth.doSomething();
System.out.println("Other created");
}
}
** ** Other.java
public interface OtherInterface {
}
class Other implements OtherInterface {
@Async
public void doSomething() {
System.out.println("did something");
}
}
** ** appcontext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:config proxy-target-class="true"></aop:config>
<bean name="app" class="scratch.App">
<lookup-method bean="otherBean" name="createOther" />
</bean>
<bean name="otherBean" class="scratch.Other" scope="prototype">
</bean>
<task:executor id="workflowExecutorSvcPool" pool-size="5-50"
queue-capacity="1000" keep-alive="60" />
<task:annotation-driven executor="workflowExecutorSvcPool" />
</beans>
什么是确切的异常消息。和堆栈跟踪。 – Bozho 2011-04-10 08:29:07
添加到帖子 – Raghu 2011-04-10 08:44:14