jdk动态代理
涉及的类InvocationHandler,Proxy
InvocationHandler 用来编写具体的代理逻辑
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable; //参数:1 代理对象, 2 代理的方法,3 代理方法的参数
Proxy用来生成代理对象
Proxy.newProxyInstance(getClass().getClassLoader(), obj.getClass().getInterfaces(), this); //参数:1 类加载器, 2 代理接口的数组, 3 InvocationHandler类型的对象具体的代理逻辑
注意: 在调用代理方法时, method.invoke(arg1, arg2), 第一个参数应该传入真实对象
src
package com.uioqv.test; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class MyJDKProxy { static interface UserService { boolean login(String username, String password); } static class UserServiceImpl implements UserService { public boolean login(String username, String password) { if("zhaoyun".