代理模式
要学习动态代理,那么必须要知道代理模式。
代理,现在的房地产中介就是一种代理,如果没有中介,那么房主和买主之间就是付房款,那么多了代理(中介),中介就多做了一些功能,比如收中介费。
代理最终要将请求(买房需要)转发给真正的被访问者(房主),它可以在转发请求之前或之后加入特定的逻辑(收中介费)
实例:
public interface owner {
public int sell_house();
}
public class ownerimpl implements owner {
@Override
public int sell_house() {
return 200;
}
}
public class house_proxy implements owner {
private owner owner;
public house_proxy(owner owner) {
this.owner = owner;
}
@Override
public int sell_house() {
int myfee=100;
int i = owner.sell_house();
return myfee+i;
}
}
当我们要买ownerimpl 这个房子时,我们不仅需要支付房款200,还要中介费100
类关系如图:
- owner : 该接口是对被访问者或者被访问资源的抽象,是卖房子的抽象
- ownerImpl: 被访问者或者被访问资源的具体实现类,是卖房子的具体个人
- Owner_Proxy: 被访问者的代理实现类,在这个场景中,我们要对OwnerImpl进行代理,那么代理类就需要持有OwnerImpl实例
- Client: 左边的人,代表访问者。CLient要请求具体的OwnerImpl实例,但是Client无法直接请求到其真正要访问的资源OwnerImpl(这就和现实中买主无法直接联系到卖主一样,所以需要中介),而是需要通过访问代理类Owner_Proxy进行
我们请求代理类,然后代理类又去请求具体的实现类,这样看代理类好像多此一举,但是实际上代理类不仅限于请求的转发,更多的是对请求添加访问限制。
动态代理:
JDK动态代理
前面的实例是静态代理的,如果接口修改了,那么代理类也要修改;针对不同的接口类型,我们需要单独为其实现一个代理对象,但是有可能需要添加的特定逻辑又是相同的。所以静态代理在这种情况下是不实用的。
动态代理是Java提供的一种代理方式,这个技术的核心点就是在运行期的时候对接口进行增强,生成class 对象,然后加载进虚拟机,说简单点就是虚拟机帮你创建了一个实现你接口的class
动态代理机制的实现主要就是由一个类(java.lang.reflect.Proxy
)和一个接口(java.lang.reflect.InvocationHandler
)组成,
现在的代理类就是变成了InvocationHandler
,我们只需要将代理的逻辑写在这里面即可:
public class MyProxy implements InvocationHandler {
private Object target;
public MyProxy(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("sell_house")){
System.out.println("我先收你代理费100元");
return (Integer)method.invoke(target,args)+100;
}
return null;
}
}
前面的接口和实现类再在这里放一下:
public interface owner {
public int sell_house();
}
public class ownerimpl implements owner {
@Override
public int sell_house() {
System.out.println("当前主人要卖200元");
return 200;
}
}
然后我们客户端client就使用Proxy类来获取代理后的对象:
public class client {
public static void main(String[] args) {
owner owner =(owner) Proxy.newProxyInstance(owner.class.getClassLoader(),new Class[]{owner.class},new MyProxy(new ownerimpl()));
int all=owner.sell_house();
System.out.println("总费用是 "+all);
}
}
执行后:
可以发现,我们在InvocationHandler.invoke()
方法中定义的代理逻辑已经在我们通过Proxy.newProxyInstance()
方法创建的具体对象中了.
通过这样的方式,我们的代理类并不需要继承owner接口了,然后对于不同的接口,方法类似:
现在是要代理 owner2接口:
public interface owner_02 {
public int sell_house();
}
public class owner2_impl implements owner_02{
@Override
public int sell_house() {
System.out.println("这里是owner2的接口");
return 0;
}
}
这里我们想要代理 owner2_impl 这个类。但是现在并不需要多创建一个代理类了(如果代理逻辑不变的话,变的话就需要),直接使用之前的:
public class client {
public static void main(String[] args) {
owner_02 own=(owner_02)Proxy.newProxyInstance(owner_02.class.getClassLoader(),new Class[]{owner_02.class},new MyProxy(new owner2_impl()));
own.sell_house();
}
}
如图,这就比前面的静态代理方便太多了
CGLIB(Code Generation Library)
我们前面可以发现,不管是静态代理还是动态代理,都需要我们代理的类继承了一个接口。如果某个类没有实现任何的Interface,就无法使用动态代理机制为其生成相应的动态代理对象、那么我们就可以使用CGLIB(动态字节码生成类库),来为目标对象生成动态的代理对象实例…
原理是:我们可以对目标对象进行继承扩展,为其生成相应子类,而子类可以通过重写来扩展父类的行为,只要将代理的逻辑放到子类中,然后使用子类,就可以达到代理模式的效果了。
但是,使用继承的方式来进行代理,也不能像静态代理那样,为每一个不同的目标对象对象都创建相应的子类,所以,CGLIB是一个动态的字节码生产库,在系统运行期间动态地为目标对象生成相应的扩展子类。
要使用CGLIB的例子,那么需要导入这个jar包:
spring-core
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.8</version>
</dependency>
代码如下:
/**
* @description:这是需要被代理的类,没有实现任何接口
*/
public class Be_Proxyed {
public void my_method(){
System.out.println("请代理我");
}
}
使用CGLIB需要实现MethodInterceptor
这个 接口:
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class CgLib_Proxy implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
if(method.getName().equals("my_method")){
System.out.println("这里使用CGLIB进行代理");
}
return methodProxy.invokeSuper(o,objects);
}
}
然后客户端使用:
import org.springframework.cglib.proxy.Enhancer;
public class test {
public static void main(String[] args) {
Enhancer enhancer=new Enhancer();
enhancer.setSuperclass(Be_Proxyed.class);
enhancer.setCallback(new CgLib_Proxy());
Be_Proxyed proxyed = (Be_Proxyed) enhancer.create();
proxyed.my_method();
}
}
然后执行:
可以发现,这个没有实现任何接口的类也依旧被代理了。
可以发现,我们要代理的类是在运行时,使用setSuperclass()
方法确定的,所以这是动态代理