0
点赞
收藏
分享

微信扫一扫

Spring AOP JDK动态代理报错:class com.sun.proxy.$Proxy0 cannot be cast to class com.aha.Spring5.jdkproxy.

棒锤_45f2 2022-05-02 阅读 59

报错时候的书写

public class JDKProxy {
    public static void main(String[] args) {
        UserDaoImpl userDao = new UserDaoImpl();
        UserDao o = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), UserDao.class.getInterfaces(), new MyInvocation(userDao));
        int add = o.add(1, 1);
        System.out.println(add);
    }
}

解决方法:需要修改Proxy.newProxyInstance()中类加载的类和接口,修改为Invocationhandle传入的类对应的加载器与实现接口即可。

修改后

运行成功

 

 

举报

相关推荐

0 条评论