问题
动态代理报错:
java.lang.ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.cx.bank.dao.BankDaoImpl at com.cx.bank.factory.UserDaoFactory.getBankDao(UserDaoFactory.java:57) at com.qijian.cx.bank.manager.TestManager.<clinit>(TestManager.java:17) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:250) at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:260) at org.junit.runners.BlockJUnit4ClassRunner$2.runReflectiveCall(BlockJUnit4ClassRunner.java:309) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) java.lang.ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to com.cx.bank.dao.BankDaoImpl
截图:
这里不断进入视野的就是:ClassCastException
我们知道 ClassCastException是JVM在检测到两个类型间转换不兼容时引发的运行时异常
也就是说:只有可能是我们的返回类型有问题
解决问题最好的方法当然是
看源码:
方法开头看到这么一句话:
Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.
看了更乱了,在这返回的到底是要接口类型还是对象都怪自己英语不好,哭
再继续向下看
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
慢慢发现:
newProxyInstance()得到的应该是接口
于是做出了以下的修改:
BankDaoInterface bankDao1 = (BankDaoInterface) Proxy.newProxyInstance( MyBeanFactory.class.getClassLoader(), new Class[]{BankDaoInterface.class}, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { myAspect.myBefore(); // 前增强 Object obj = method.invoke(bankDao, args); myAspect.myAfter(); // 后增强 return obj; } }); return bankDao1;
问题解决。