代理模式
定义:为其他对象提供一种代理,以控制这个对象的访问。 特点:代理对象在客户端和目标对象之间起到中介的作用。
场景:保护目标对象、增强目标对象。
一、静态代理
例如:
一)、
Dao
Service
@Autowired Dao dao;
Controller
@Autowired Service service;
二)、
/**
* 都是人,都要吃饭
*/
public interface IPerson {
void eatfood();
}
/**
* 张三
*/
public class ZhangSan implements IPerson {
public void eatfood() {
System.out.println("吃鱼");
}
}
/**
* 外卖员
*/
public class WaiMaiYuan implements IPerson {
private ZhangSan zhangsan;
public WaiMaiYuan (ZhangSan zhangsan) {
this.zhangsan = zhangsan;
}
public void eatfood() {
System.out.println("外卖员拿到外卖");
zhangsan.eatfood();
}
}
/**
* 给指定的张三送外卖
*/
public class Test {
public static void main(String[] args) {
WaiMaiYuan waiMaiYuan= new WaiMaiYuan(new ZhangSan());
WaiMaiYuan.eatfood();
}
}
输出:
外卖员拿到外卖
吃鱼
二、动态代理
例如:
一)、
Spring Aop
作用:增强功能、代码增强、将非功能性代码进行解耦。
事务管理、日志监控、权限控制。
非功能性代码就是所谓的切面。
conn.begin();//开始事务、非功能性代码
try{
dao.insert();//功能性代码
conn.commit();//提交事务、非功能性代码
}catch(e){
conn.rollback();//回滚、非功能性代码
}finally{
conn.close();//关闭事务、非功能性代码
}
二)、jdkproxy
JDK动态代理原理:(字节码重组)
1、拿到目标引用
2、反射拿到目标对象的class
3、拿到目标对象所实现的所有接口
4、在Proxy工具类中,用代码生成一个新的类,新类会实现目标类所有的接口,保证和目标类属于同一个继承体系
5、用户拿到的对象是新类对象(偷梁换柱)
6、最终调动新对象的功能,实现了功能的增强
/**
* 人都要寻找食物
*/
public interface IPerson {
void findFood();
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 外卖系统帮你找食物
*/
public class JdkWaiMai implements InvocationHandler {
private IPerson target;
public IPerson getInstance(IPerson target){
//1、拿到目标引用
this.target = target;
//2、反射拿到目标对象的class
Class<?> clazz = target.getClass();
//通过clazz.getClassLoader()获取ClassLoader对象
//通过clazz.getInterfaces()获取target所实现的所有接口。
//当前类
return (IPerson) Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),this);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
before();
Object result = method.invoke(this.target,args);//获取目标对对象的方法
after();
return result;
}
private void after() {
System.out.println("收到外卖订单");
}
private void before() {
System.out.println("通知外卖员送餐");
}
}
/**
* 张三想吃什么
*/
public class Zhangsan implements IPerson {
public void findFood() {
System.out.println("张三要牛肉");
}
}
/**
* 老刘想吃什么
*/
public class ZhaoLiu implements IPerson {
public void findFood() {
System.out.println("赵六要鸡肉");
}
}
/**
* 外卖系统帮助寻找食物
*/
public class Test {
public static void main(String[] args) {
JdkWaiMai jdkWaiMai = new JdkWaiMai();
//zhangsan = $Proxy0 这是jdk生成的代理类对象
IPerson zhangsan = jdkWaiMai.getInstance(new Zhangsan());
//findFood()调用代理对象的invoke()
zhangsan.findFood();
System.out.println("————————");
IPerson zhaoliu = jdkWaiMai.getInstance(new ZhaoLiu());
zhaoliu.findFood();
}
}
输出
收到外卖订单
张三要牛肉
通知外卖员送餐
————————
收到外卖订单
赵六要鸡肉
通知外卖员送餐
三)、CGLib动态代理原理(字节码重组)
原理:
生成一个新的类,它对目标类没有任何要求
继承目标类,新类变成目标类的子类
重写父类所有的方法,在子类中实现增强
/**
* 外卖系统
*/
public class CGlibWaiMai implements MethodInterceptor {
public Object getInstance(Class<?> clazz) throws Exception{
//相当于Proxy,代理的工具类
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(clazz);
enhancer.setCallback(this);
return enhancer.create();
}
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
before();
Object obj = methodProxy.invokeSuper(o,objects);
after();
return obj;
}
private void before(){
System.out.println("收到外卖订单");
}
private void after(){
System.out.println("通知外卖员送餐");
}
}
/**
* 客户点外卖
*/
public class Customer {
public void findFood(){
System.out.println("点了鱼");
}
}
public class CglibTest {
public static void main(String[] args) {
try {
//JDK是采用读取接口的信息
//CGLib覆盖父类方法
//目的:都是生成一个新的类,去实现增强代码逻辑的功能
//JDK Proxy 对于用户而言,必须要有一个接口实现,目标类相对来说复杂
//CGLib 可以代理任意一个普通的类,没有任何要求
//CGLib 生成代理逻辑更复杂,效率,调用效率更高,生成一个包含了所有的逻辑的FastClass,不再需要反射调用
//JDK Proxy生成代理的逻辑简单,执行效率相对要低,每次都要反射动态调用
//CGLib 有个坑,CGLib不能代理final的方法
Customer obj = (Customer) new CGlibWaiMai.getInstance(Customer.class);
System.out.println(obj);
obj.findFood();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出
收到外卖订单
点了鱼
通知外卖员送餐