public class TestJdkProxy { public static void main(String[] args) { UserService userService = new UserServiceImpl(); JdkProxy jdkProxy = new JdkProxy(userService); UserService proxyUserService = (UserService)Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), jdkProxy); proxyUserService.save(); } } class JdkProxy implements InvocationHandler { private Object tagetObject; public JdkProxy(Object tagetObject){ this.tagetObject = tagetObject; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("开始拦截"); Object result = method.invoke(tagetObject, args); System.out.println("结束"); return result; } } interface UserService{ void save(); } class UserServiceImpl implements UserService{ @Override public void save() { System.out.println("save method"); } }