设计模式:代理模式

本文介绍了Java中的静态代理和动态代理,包括JDK动态代理的Proxy类和CGLIB库。静态代理通过实现相同接口实现代理,而动态代理利用反射创建代理对象。CGLIB通过子类化目标类实现代理,但无法代理final类。Spring AOP基于代理实现切面编程,可通过XML配置或注解方式定义切点和通知。
摘要由CSDN通过智能技术生成

静态代理:

一个对象使用多个代理,实现代理的类为xxxProxy,proxy也实现被代理对象的接口,就可以实现嵌套代理。静态代理的代理类时事先写好的,前提是我们通过实现指定接口,知道被代理对象需要代理的方法。
在这里插入图片描述

动态代理:

通过反射,由JDK动态创建代理对象。这个方法完全依赖于接口,被代理对象必须实现某一接口,否则jdk不知道要写入什么方法。这也是jdk动态代理的局限之处。jdk动态代理底层是用asm类库实现的,asm类库是一个可以操纵二进制码的类库,asm直接修改二进制文件进行方法的代理。
以下是关于Java的Proxy类的详解。
在这里插入图片描述
newProxyInstance​(ClassLoader loader, 类<?>[] interfaces, InvocationHandler h) ClassLoader loader:被代理对象的类加载器 类<?>[] interfaces:被代理对象所实现的接口数组
InvocationHandler h:代理方法的处理过程

代码实现

    public static void main(String[] args) {
        Tank tank = new Tank();
        Moveble proxy1 = (Moveble)Proxy.newProxyInstance(Tank.class.getClassLoader(), new Class[]{Moveble.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("proxy1");
                Object o = method.invoke(tank, args);
                return o;
            }
        });
        proxy1.move();
}

cglib

和jdk动态代理比起来适用范围更广,底层同样使用的是asm。cglib通过生成一个被代理对象的子类,重写父类方法来实现代理。缺陷是如果被代理对象被final修饰则不能生成子类也就无法使用cglib进行代理。

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Tank.class);
        enhancer.setCallback(new MethodInterceptor() {
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("before");
                Object invoke = methodProxy.invokeSuper(o, objects);
                System.out.println("after");
                return invoke;
            }
        });
        Tank tank = (Tank)enhancer.create();
        tank.move();
    }

spring aop代理

首先加入spring所需依赖

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.19</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.19</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.19</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.19</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.1</version>
        </dependency>

编写appContext.xml配置文件
这里有一个注意事项:aop:config中的 proxy-target-class设置为true否则会报一个Exception in thread “main“ java.lang.ClassCastException: com.sun.proxy.$Proxy8 cannot be cast to 的异常,如果被代理的类本身是一个接口,那默认就是true。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="Tank" class="StaticProxy.Tank"/>
    <bean id="TankProxy" class="spring.aop.TankProxy"/>

  <aop:config proxy-target-class="true">
        <aop:aspect id="proxy" ref="TankProxy" >
<!--           切面:插入的功能-->
            <aop:pointcut id="move" expression="execution(void StaticProxy.Tank.move())"/>
<!--            切点:被切入的方法-->
            <aop:after method="after" pointcut-ref="move"/>
<!--            在方法之后执行-->
            <aop:before method="before" pointcut-ref="move"/>
<!--            在方法之前执行-->
         </aop:aspect>
    </aop:config>
</beans>

测试

package spring.aop;

import StaticProxy.Tank;
import javafx.application.Application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");
        Tank tank = (Tank)context.getBean("Tank");
        tank.move();
    }
}

还有一种方法是通过spring注解来实现的更加简单快捷。
配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

   <aop:aspectj-autoproxy/>

    <bean id="Tank" class="StaticProxy.Tank"/>
    <bean id="TankProxy" class="spring.aop.TankProxy"/>

</beans>

代理类

package spring.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class TankProxy  {
    @Before("execution(void StaticProxy.Moveble.move())")
    void before(){
        System.out.println("before");
    }
    @After("execution(void StaticProxy.Moveble.move())")
    void after(){
        System.out.println("after");
    }
}

还记得我们刚才的报错吗?嘿嘿!使用这种方法也是会出现相同的报错的,只需要把cast的类型改成一个接口就没有问题啦!例如Tank实现了Moveable接口,我就把他cast成Moveable而不是直接cast成Tank。因为如果是接口的话proxy-target-class默认为true。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值