代理机制
1.代理模式特点
说明:使用代理模式主要的目的时为了解耦. 将公共的通用的方法(功能 / 业务) 放到代理对象中.
有业务层专注于业务执行.
特点:
1.为什么使用代理? – 因为没有资源
2.代理的作用? – 代理要解决(扩展)某些实际问题
3.用户最终执行目标方法
2.动态代理 --JDK模式
JDK代理的说明
1.JDK代理模式是java原生提供的API, 无需导包
2.JDK代理要求: 被代理者必须是**接口** 或者 **实现接口** ( interface / Impl )
3.灵活: 代理对象看起来和被代理者一模一样 ( 方法相同 )
JDK代理类代码
package com.jt.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JDKProxy {
//传入target目标对象 获取代理对象
//利用代理对象 实现方法的扩展
public static Object getProxy(Object target){
//1.获取类加载器
ClassLoader classLoader = target.getClass().getClassLoader();
//2.获取接口数组类型
Class[] interfaces = target.getClass().getInterfaces();
//3.代理对象执行方法时的回调方法(代理对象调用方法时,执行InvocationHandler)
return Proxy.newProxyInstance(classLoader, interfaces, invocationHandler(target));
}
//要求必须传递目标对象
public static InvocationHandler invocationHandler(Object target){
return new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("事务开启");
//获取目标方法的返回值
Object result = method.invoke(target, args);
System.out.println("事务提交");
return result;
}
};
}
}
JDK测试类代码
package com.jt;
import com.jt.config.SpringConfig;
import com.jt.proxy.JDKProxy;
import com.jt.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestSpring {
@Test
public void demo1(){
ApplicationContext context = new
AnnotationConfigApplicationContext(SpringConfig.class);
//获取目标对象
UserService userService = context.getBean(UserService.class);
System.out.println(userService.getClass());
//传入目标对象 获取代理对象
UserService proxy = (UserService) JDKProxy.getProxy(userService);
System.out.println(proxy.getClass());
//代理对象执行方法
proxy.addUser();
}
}
3.动态代理 – CGLIB模式
CGLIB代理的说明
1.要求被代理者有无接口都可以.
2.代理对象是目标对象的子类 重写子类的方法.
CBLIB代理类代码
package com.jt.proxy;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class CGlibProxy {
public static Object getProxy(Object target){
//1.创建增强器对象
Enhancer enhancer = new Enhancer();
//2.设定父级 目标对象
enhancer.setSuperclass(target.getClass());
//3.定义回调的方法 (代理对象执行目标方法时调用)
enhancer.setCallback(getMethodInterceptor(target));
//4.创建代理对象
return enhancer.create();
}
public static MethodInterceptor getMethodInterceptor(Object target){
return new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("事务开始");
//执行目标方法
Object result = method.invoke(target,args);
System.out.println("事务提交");
return result;
}
};
}
}
CGLIB测试类代码
@Test
public void demo3(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean(UserService.class);
UserService proxy = (UserService) CGlibProxy.getProxy(userService);
proxy.addUser();
}
4.关于JDK代理和CGLIB代理总结
jdk代理:
1.要求必须有/实现接口. 如果没有接口,则JDK代理不能正常执行.
2.JDK代理工具API:
1) 类加载器
ClassLoader classLoader = target.getClass().getClassLoader();
2) 获取接口数组类型
Class[] interfaces = target.getClass().getInterfaces();
3) 代理对象执行方法时的回调方法(代理对象调用方法时,执行InvocationHandler)
Proxy.newProxyInstance(类加载器classLiader , 接口数组interfaces , invocationHandler接口 )
3. JDK中执行目标方法 -- method.invoke(target,args);
cglib代理:
1.要求被代理者有无接口都可以. 代理对象是目标对象的子类 重写子类方法
2.JDK代理工具API:
1) 创建增强器对象
Enhancer enhancer = new Enhancer();
2) 设定父级 目标对象
enhancer.setSuperclass(target.getClass());
3) 定义回调的方法 (代理对象执行目标方法时调用)
enhancer.setCallback(getMethodInterceptor(target));
4) 创建代理对象
enhancer.create();
5) 回调接口
MethodInterceptor
public static MethodInterceptor getMethodInterceptor(Object target){
return new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy
methodProxy) throws Throwable {
System.out.println("事务开始");
//执行目标方法
Object result = method.invoke(target,args);
System.out.println("事务提交");
return result;
}
};
}
3. 执行目标方法 -- Object result = method.invoke(target,args);