代理分两种:静态代理、动态代理
静态代理:创建一个工具,传入实体引用,在通过引用点方法执行
接口名 target;
public 构造方法(接口名 target){
this.target=target;
}
@Override
public Object execute(){
//操作前
target.execute()
//操作后
}
=======================以上是静态代理====================
动态代理:
通过jdk proxy 或 cglib 等工具生成一个代理的类再加载到jvm中运行
InvocationHandlerTool
MainTest
MethedInvocationHandler
ProxyGeneratorUtils
UserService
UserServiceImp
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @author XQQ
* @create 2018-09-21 11:07
* @desc
**/
public class MainTest {
public static void main(String[] args) {
UserService userService=new UserServiceImp();
InvocationHandlerTool proxy=new InvocationHandlerTool(userService);
UserService o = (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), proxy);
o.sayHello();
System.out.println("--proxy---简单粗暴-------");
UserService o2 =(UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
method.invoke(userService, args);
return null;
}
});
o2.sayHello();
System.out.println("--proxy---简单粗暴-------");
System.out.println("--proxy---简单实用-------");
UserService bind =(UserService) proxy.bind(userService);
bind.sayHello();
System.out.println("--proxy---简单实用-------");
System.out.println("--methodInterceptor---常用-------");
MethodInterceptor methodInterceptor=new MethedInvocationHandler();
Enhancer enhancer=new Enhancer();
enhancer.setSuperclass(UserServiceImp.class);
enhancer.setCallback(methodInterceptor);
UserService o1 = (UserService)enhancer.create();
o1.sayHello();
System.out.println("--methodInterceptor---常用-------");
System.out.println("--CGLIB---简单粗暴-------");
enhancer.setSuperclass(UserServiceImp.class);
enhancer.setCallback(new MethodInterceptor(){
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
methodProxy.invokeSuper(o,objects);
return null;
}
});
UserService service= (UserService) enhancer.create();
userService.sayHello();
System.out.println("--CGLIB---简单粗暴-------");
System.out.println("--proxyGenerator 生成代理类-------");
ProxyGeneratorUtils.writeProxyClassToHardDisk("$Proxy11.class");
System.out.println("--CGLIB---简单粗暴-------");
}
}
执行结果:
进入:sayHello
方法业务代码
进入 end:sayHello
--proxy---简单粗暴-------
方法业务代码
--proxy---简单粗暴-------
--proxy---简单实用-------
进入:sayHello
方法业务代码
进入 end:sayHello
--proxy---简单实用-------
--methodInterceptor---常用-------
sayHello
方法业务代码
sayHello
--methodInterceptor---常用-------
--CGLIB---简单粗暴-------
方法业务代码
--CGLIB---简单粗暴-------
--proxyGenerator 生成代理类-------
--CGLIB---简单粗暴-------
Process finished with exit code 0
public interface UserService {
void sayHello();
}
public class UserServiceImp implements UserService {
@Override
public void sayHello() {
System.out.println("方法业务代码");
}
}
public class InvocationHandlerTool implements InvocationHandler {
private Object target;
public InvocationHandlerTool(Object target){
this.target=target;
}
public Object bind(Object target){
Object o = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
return o;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("进入:"+method.getName());
Object invoke = method.invoke(target, args);
System.out.println("进入 end:"+method.getName());
return invoke;
}
}
public class MethedInvocationHandler implements MethodInterceptor{
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println(method.getName());
Object o1 = methodProxy.invokeSuper(o, objects);
System.out.println(method.getName());
return o1;
}
}
public class ProxyGeneratorUtils {
/**
* 把代理类的字节码写到硬盘上
*
* @param path 保存路径
*/
public static void writeProxyClassToHardDisk(String path) {
// 第一种方法,这种方式在刚才分析ProxyGenerator时已经知道了
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", true);
// 第二种方法
// 获取代理类的字节码
byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy11", UserServiceImp.class.getInterfaces());
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
out.write(classFile);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
全部代码都在上面了。proxy 和 cglib两种方式
<!-- https://mvnrepository.com/artifact/cglib/cglib --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency>
4866

被折叠的 条评论
为什么被折叠?



