mirror of
https://github.com/Estom/notes.git
synced 2026-04-09 22:09:28 +08:00
动态代理
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package com.ykl.proxy;
|
||||
|
||||
import com.ykl.proxy.handler.LogInterceptor;
|
||||
import com.ykl.proxy.service.UserService;
|
||||
import net.sf.cglib.proxy.Enhancer;
|
||||
|
||||
public class CglibDynamicProxy {
|
||||
|
||||
public static void main(String[] args) {
|
||||
LogInterceptor logInterceptor = new LogInterceptor();
|
||||
Enhancer enhancer = new Enhancer();
|
||||
enhancer.setSuperclass(UserService.class); // 设置超类,cglib是通过继承来实现的
|
||||
enhancer.setCallback(logInterceptor);
|
||||
UserService userService = (UserService)enhancer.create(); // 创建代理类
|
||||
|
||||
|
||||
userService.update();
|
||||
userService.select();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.ykl.proxy;
|
||||
|
||||
import com.ykl.proxy.handler.LogHandler;
|
||||
import com.ykl.proxy.service.UserService;
|
||||
import com.ykl.proxy.service.UserServiceImpl;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
public class DynamicProxy {
|
||||
|
||||
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
|
||||
// 设置变量可以保存动态代理类,默认名称以 $Proxy0 格式命名
|
||||
// System.getProperties().setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
|
||||
// 1. 创建被代理的对象,UserService接口的实现类
|
||||
UserServiceImpl userServiceImpl = new UserServiceImpl();
|
||||
// 2. 获取对应的 ClassLoader
|
||||
ClassLoader classLoader = userServiceImpl.getClass().getClassLoader();
|
||||
// 3. 获取所有接口的Class,这里的UserServiceImpl只实现了一个接口UserService,
|
||||
Class[] interfaces = userServiceImpl.getClass().getInterfaces();
|
||||
// 4. 创建一个将传给代理类的调用请求处理器,处理所有的代理对象上的方法调用
|
||||
// 这里创建的是一个自定义的日志处理器,须传入实际的执行对象 userServiceImpl
|
||||
InvocationHandler logHandler = new LogHandler(userServiceImpl);
|
||||
/*
|
||||
5.根据上面提供的信息,创建代理对象 在这个过程中,
|
||||
a.JDK会通过根据传入的参数信息动态地在内存中创建和.class 文件等同的字节码
|
||||
b.然后根据相应的字节码转换成对应的class,
|
||||
c.然后调用newInstance()创建代理实例
|
||||
*/
|
||||
UserService proxy = (UserService) Proxy.newProxyInstance(classLoader, interfaces, logHandler);
|
||||
// 调用代理的方法
|
||||
proxy.select();
|
||||
proxy.update();
|
||||
|
||||
// 保存JDK动态代理生成的代理类,类名保存为 UserServiceProxy
|
||||
// ProxyUtils.generateClassFile(userServiceImpl.getClass(), "UserServiceProxy");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.ykl.proxy.handler;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
|
||||
public class LogHandler implements InvocationHandler {
|
||||
Object target; // 被代理的对象,实际的方法执行者
|
||||
|
||||
public LogHandler(Object target) {
|
||||
this.target = target;
|
||||
}
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
before();
|
||||
Object result = method.invoke(target, args); // 调用 target 的 method 方法
|
||||
after();
|
||||
return result; // 返回方法的执行结果
|
||||
}
|
||||
// 调用invoke方法之前执行
|
||||
private void before() {
|
||||
System.out.println(String.format("log start time [%s] ", new Date()));
|
||||
}
|
||||
// 调用invoke方法之后执行
|
||||
private void after() {
|
||||
System.out.println(String.format("log end time [%s] ", new Date()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.ykl.proxy.handler;
|
||||
|
||||
import net.sf.cglib.proxy.MethodInterceptor;
|
||||
import net.sf.cglib.proxy.MethodProxy;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
|
||||
public class LogInterceptor implements MethodInterceptor {
|
||||
|
||||
/**
|
||||
* @param object 表示要进行增强的对象
|
||||
* @param method 表示拦截的方法
|
||||
* @param objects 数组表示参数列表,基本数据类型需要传入其包装类型,如int-->Integer、long-Long、double-->Double
|
||||
* @param methodProxy 表示对方法的代理,invokeSuper方法表示对被代理对象方法的调用
|
||||
* @return 执行结果
|
||||
* @throws Throwable
|
||||
*/
|
||||
@Override
|
||||
public Object intercept(Object object, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
|
||||
before();
|
||||
Object result = methodProxy.invokeSuper(object, objects); // 注意这里是调用 invokeSuper 而不是 invoke,否则死循环,methodProxy.invokesuper执行的是原始类的方法,method.invoke执行的是子类的方法
|
||||
after();
|
||||
return result;
|
||||
}
|
||||
private void before() {
|
||||
System.out.println(String.format("log start time [%s] ", new Date()));
|
||||
}
|
||||
private void after() {
|
||||
System.out.println(String.format("log end time [%s] ", new Date()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.ykl.proxy.service;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
void select();
|
||||
|
||||
void update();
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ykl.proxy.service;
|
||||
|
||||
public class UserServiceImpl implements UserService {
|
||||
@Override
|
||||
public void select() {
|
||||
System.out.println("selecting");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
System.out.println("updating");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user