设计模式之代理模式

代理对象
被代理对象
代理模式分为静态代理和动态代理
静态代理
我认为静态代理就是通过对象组合的方式实现
代理模式就是在一个方法前后加上一些自定义的业务处理
动态代理
JDK:内部通过接口实现动态代理
CGLIB:内部通过继承实现动态代理,最终代理类继承了被代理类,所以final类不能通过CGLIB实现动态代理。
实际应用比如:一张大图片一时半会加载不出来,这时候就可以用代理模式,先用一张图片代替它,等加载出来再替换
分布计算方式RMI和Corba等都是Proxy模式的应用

静态代理

代理类和被代理类都需要实现一个共同的抽象接口MoveAble
所谓静态代理就是在运行之前(编译期间)就存在代理类的字节码文件,代理类和被代理类的关系在运行之前就确定了
一、字节码文件:.java文件编译之后的.class文件,操作系统不能直接运行,必须在jvm上运行
二、这里的代理类就是日志代理,时间代理;被代理类就是汽车类(具体是move这个方法)
抽象接口也称之为抽象角色,代理类也称之为代理角色,被代理类也称之为真实角色

接口(Moveable )
public interface Moveable {
	void move();
}

被代理类
public class Car implements Moveable {
	@Override
	public void move() {	
		try {
			Thread.sleep(new Random().nextInt(1000));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("汽车行驶中...");
	}
}

日志代理类(CarLogProxy )
public class CarLogProxy implements Moveable {
	private Moveable m;
	public CarLogProxy(Moveable m) {
		super();
		this.m = m;
	}
	@Override
	public void move() {
		System.out.println("静态代理,开始记录日志");
		m.move();
		System.out.println("静态代理,结束记录日志");
	}
}

计时代理类(CarTimeProxy )
public class CarTimeProxy implements Moveable {
	private Moveable m;
	public CarTimeProxy(Moveable m) {
		super();
		this.m = m;
	}
	@Override
	public void move() {
		System.out.println("静态代理,汽车开始记录时间");
		long startTime = System.currentTimeMillis();
		m.move();
		long endTime = System.currentTimeMillis();
		System.out.println("静态代理,汽车结束记录时间,用时:"+ (endTime - startTime));
	}
}

测试类
public class Test {
	public static void  main(String args[]){
		Car car = new Car();
		CarLogProxy carLogProxy = new CarLogProxy(car);
		CarTimeProxy carTimeProxy = new CarTimeProxy(carLogProxy);
		carTimeProxy.move();
	}
}

控制台打印:
静态代理,汽车开始记录时间
静态代理,开始记录日志
汽车行驶中…
静态代理,结束记录日志
静态代理,汽车结束记录时间,用时:914

动态代理

JDK动态代理:

在动态代理中,代理类的源码是在运行期间由jvm根据反射机制自动生成的,所以不存在代理类的字节码文件,代理类与被代理类的联系在程序运行期间确定

这里写图片描述

timeHandler事务处理器
public class TimeHandler implements InvocationHandler {
	private Object target;
	public TimeHandler(Object target) {
		super();
		this.target = target;
	}
	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {	
		//timeHandler事务处理器的业务代码如下,此处简单表示 
		System.out.println("动态代理,汽车开始记录时间");
		long startTime = System.currentTimeMillis();
		method.invoke(target);	
		long endTime = System.currentTimeMillis();
		System.out.println("动态代理,汽车结束记录时间,用时:"+ (endTime - startTime));
		return null;
	}
}

测试类
public class Test {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Car car = new Car();
		Class<?> cls = car.getClass();
		InvocationHandler timeHandler = new TimeHandler(car);
		/**
		 * 动态生成代理类
		 * loader 被代理类的类加载器
		 * interface 被代理类实现的接口
		 * h 要绑定的事务处理器
		 */
		Moveable m = (Moveable)Proxy.newProxyInstance(cls.getClassLoader(),
					cls.getInterfaces(), timeHandler);	
		m.move();
	}
}

控制台打印:
动态代理,汽车开始记录时间
汽车行驶中…
动态代理,汽车结束记录时间,用时:185
CGLIB:
  public class MyInterceptor implements MethodInterceptor {
        @Override
        public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
            System.out.println("cglib interceptor 代理前打印");
            Object object = methodProxy.invokeSuper(o,objects);
            System.out.println("cglib interceptor 代理前打印");
            return object;
        }
    }
public class Car {
    public void run(){
        System.out.println("开车...");
    }
}
public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Car.class);
        enhancer.setCallback(new MyInterceptor());
        Car car = (Car) enhancer.create();
        car.run();
    }
cglib interceptor 代理前打印
开车...
cglib interceptor 代理前打印

Spring aop

@Component
@Aspect
public class MyAop  {

    @Pointcut("execution(* com.tuzhihai.sourceanalysis.aop.CarHasInterface.*(..))")
    public void pointCut(){}

    @Around("pointCut()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("spring aop @Around 代理前打印");
        joinPoint.proceed();
        System.out.println("spring aop @Around 代理后打印");
    }
}
@Component
public class CarHasInterface implements Vehicle{

    @Override
    public void run(){
        System.out.println("开车...");
    }
}
@Controller
@RequestMapping("/aop")
public class TestController {

    @Autowired
    CarHasInterface carHasInterface;

    @GetMapping("/test")
    public void test(){
       carHasInterface.run();
    }
}
spring aop @Around 代理前打印
开车...
spring aop @Around 代理后打印

一、简单来说:
  JDK动态代理生成的代理类需要 实现被代理类(真实类)的接口,重写其中的方法
CGLIB动态代理生成的代理类 需要继承被代理类(真实类),覆盖其中的方法

二、Spring在选择用JDK还是CGLiB的依据:

(1)具体使用哪个,首先取决于配置,再取决于bean自身是否实现接口。关键配置( spring.aop.proxy-target-class) ,这个与spring-configuration包版本有关系

spring.aop.proxy-target-class=true :强制全部使用Cglib动态代理织入增强
spring.aop.proxy-target-class=false :自动选择代理方式(若类实现接口,则使用JDK动态代理,若没有实现接口,则使用Cglib动态代理)
org.springframework.boot:spring-boot-configuration 版本是2.x时,此配置默认是true; 版本1.x时: 当使用spring事务管理时,默认是true,其他情况,默认是false
2.x
{
      "name": "spring.aop.proxy-target-class",
      "type": "java.lang.Boolean",
      "description": "Whether subclass-based (CGLIB) proxies are to be created (true), as opposed to standard Java interface-based proxies (false).",
      "defaultValue": true
    }
1.x
 {
      "name": "spring.aop.proxy-target-class",
      "description": "Whether subclass-based (CGLIB) proxies are to be created (true) as opposed to standard Java interface-based proxies (false). Defaults to \"true\" when using Spring Transaction Management, otherwise \"false\".",
      "type": "java.lang.Boolean"
    }

三、CGlib比JDK快?

(1)使用CGLib实现动态代理,CGLib底层采用ASM字节码生成框架,使用字节码技术生成代理类,比使用Java反射效率要高。唯一需要注意的是,CGLib不能对声明为final的方法进行代理,因为CGLib原理是动态生成被代理类的子类。

(2)在对JDK动态代理与CGlib动态代理的代码实验中看,1W次执行下,JDK7及8的动态代理性能比CGlib要好20%左右。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值