// 动态代理
public static BaseUser getBaseUser(BaseUserService baseUserService){
// 方法执行器,帮我们执行目标的方法
InvocationHandler invocationHandler = new InvocationHandler() {
@Override
/**
* proxy:代理对象,jdk用,不要动
* method:当前目标要执行的方法
* args:传入的参数
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 利用反射执行目标方法
Object result = method.invoke(baseUserService,args);
return result;
}
};
Class<?>[] interfaces = baseUserService.getClass().getInterfaces();
ClassLoader classLoader = baseUserService.getClass().getClassLoader();
Object proxy = Proxy.newProxyInstance(classLoader,interfaces,invocationHandler);
return (BaseUser) proxy;
}