java – Spring – 不能强制使用CGLIB代理作为Classcast异常

java – Spring – 不能强制使用CGLIB代理作为Classcast异常
这是让我疯狂的情景.

>我有一个具有查找方法的类 – createOther()
> createOther应该创建一个Other类型的对象.其他实现OtherInterface,另外还有一个标记为@Async的方法doSomething
>由于Other实现了OtherInterface,Spring给了我一个JDK代理,我不能将其作为其他.
> Spring文档建议使用< aop:config proxy-target-class =“true”> – 但我是新手,使用它似乎没有帮助.

问题:我如何告诉Spring我需要一个针对Other类的CGLib代理?

下面的代码失败,带有classcastexception.

Exception in thread "main" java.lang.ClassCastException: $Proxy4 cannot be cast to scratch.Otherat 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 {@Asyncpublic 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.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><aop:config proxy-target-></aop:config><bean name="app" >    <lookup-method bean="otherBean" name="createOther" /></bean><bean name="otherBean"  scope="prototype"></bean><task:executor  pool-size="5-50"    queue-capacity="1000" keep-alive="60" /><task:annotation-driven executor="workflowExecutorSvcPool" /></beans>

一切似乎都很好 – 这是告诉spring使用cglib代理的正确方法.实际上,默认情况下它会生成cglib代理的 documentation states.唯一的要求是在类路径上使用cglib.确保你有cglib jar.

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部