什么是代理?顾名思义,代理就是代替别人做一些事情嘛,代理有:智能代理,远程代理,保护代理,虚拟代理四种划分,我们常用的是智能代理。用我的理解解释这种代理的实质可以这么讲:A类中有方法a(),现在我们在调用a()方法时,我们可以执行一些其他的操作例如
执行XXX操作;
执行a();
执行XXX操作;
常用代理模式(代码来自慕课网,看了看,大神写的真不错,直接拿来用,我是知识的搬运工)
1.静态代理
1.1继承方式(通过类的继承实现代理)
1.2聚合方式
public interface Moveable {
void move();
}
import java.util.Random;
public class Car implements Moveable {
@Override
public void move() {
//实现开车
try {
Thread.sleep(new Random().nextInt(1000));
System.out.println("汽车行驶中....");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Car2 extends Car {
@Override
public void move() {
long starttime = System.currentTimeMillis();
System.out.println("汽车开始行驶....");
super.move();
long endtime = System.currentTimeMillis();
System.out.println("汽车结束行驶.... 汽车行驶时间:"
+ (endtime - starttime) + "毫秒!");
}
}
public class Car3 implements Moveable {
public Car3(Car car) {
super();
this.car = car;
}
private Car car;
@Override
public void move() {
long starttime = System.currentTimeMillis();
System.out.println("汽车开始行驶....");
car.move();
long endtime = System.currentTimeMillis();
System.out.println("汽车结束行驶.... 汽车行驶时间:"
+ (endtime - starttime) + "毫秒!");
}
}
public class Client {
/**
* 测试类
*/
public static void main(String[] args) {
//Car car = new Car();
//car.move();
//使用集成方式
//Moveable m = new Car2();
//m.move();
//使用聚合方式实现
Car car = new Car();
Moveable m = new Car3(car);
m.move();
}
}
执行结果:
汽车开始行驶….
汽车行驶中….
汽车结束行驶…. 汽车行驶时间:582毫秒!
2.使用JDK动态代理(只能代理实现了接口的类)
可以这么理解,代码在编译时,会有一个.class的字节码文件产生,而这个字节码文件可以看做被代理类自身的方法与需要加入的一些操作的组合体。
直接上例子,多看,多想,跟源代码,慢慢理解,这块我理解了好久。。。
public interface Moveable {
void move();
}
import java.util.Random;
public class Car implements Moveable {
@Override
public void move() {
//实现开车
try {
Thread.sleep(new Random().nextInt(1000));
System.out.println("汽车行驶中....");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class TimeHandler implements InvocationHandler {
public TimeHandler(Object target) {
super();
this.target = target;
}
private Object target;
/*
* 参数:
* proxy 被代理对象
* method 被代理对象的方法
* args 方法的参数
*
* 返回值:
* Object 方法的返回值
* */
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
long starttime = System.currentTimeMillis();
System.out.println("汽车开始行驶....");
method.invoke(target);
long endtime = System.currentTimeMillis();
System.out.println("汽车结束行驶.... 汽车行驶时间:"
+ (endtime - starttime) + "毫秒!");
return null;
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import com.imooc.jdkproxy.Car;
import com.imooc.jdkproxy.Moveable;
public class Test {
/**
* JDK动态代理测试类
*/
public static void main(String[] args) {
Car car = new Car();
InvocationHandler h = new TimeHandler(car);
Class<?> cls = car.getClass();
/**
* loader 类加载器
* interfaces 实现接口
* h InvocationHandler
*/
Moveable m = (Moveable)Proxy.newProxyInstance(cls.getClassLoader(),
cls.getInterfaces(), h);
m.move();
}
}
3.CGLIB动态代理(可以代理没有实现接口的类)
这种代理是基于对被代理类的方法的拦截而实现功能的,代理过程中会得到被代理类的一个子类,对原方法进行了重写,所以在运行时会被进入一些其他的操作。