💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
~~~ 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(); ~~~