ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ public class JdkProxy implements InvocationHandler { private Object target; public JdkProxy(Object target) { this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = null; System.out.println("before"); result = method.invoke(target, args); System.out.println("after"); return result; } } ~~~ ~~~ // 传入被代理对象 IUserDao userDao = new UserDaoImpl(); JdkProxy jdkProxy = new JdkProxy(userDao); ClassLoader classLoader = userDao.getClass().getClassLoader(); Class<?>[] interfaces = userDao.getClass().getInterfaces(); IUserDao newProxyInstanc = (IUserDao)Proxy.newProxyInstance(classLoader, interfaces, jdkProxy); newProxyInstanc.save(); ~~~