0
点赞
收藏
分享

微信扫一扫

【Spring】AOP

独西楼Q 2022-04-13 阅读 61

AOP:

1. 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术

2. AOP是OOP的延伸,可以对业务逻辑的哥哥部分进行隔离,降低业务逻辑部分之间的耦合度。

3. 减少重复代码,提高程序的可重用性,提高开发的效率。

4. 在程序运行期间,可以在不修改源码的情况下对方法进行功能增强。

AOP的动态代理技术

常用的动态代理技术

1. JDK代理:基于接口的动态代理技术

2. cglib代理:基于父类的动态代理技术

1. JDK代理示例:

创建TargetInterface接口及增强类

package cn.leohams.jdk;

public interface TargetInterface {
    public void save();
}

Target类 

package cn.leohams.jdk;

public class Target implements TargetInterface{
    @Override
    public void save() {
        System.out.println("nihao, save...");
    }
}

增强类(Advice) 

package cn.leohams.jdk;

public class Advice {
    public void before(){
        System.out.println("前置增强");
    }

    public void after(){
        System.out.println("后置增强");
    }
}

最后写一个测试类 ProxyTest,具体参数含义可以看看注释

package cn.leohams.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {
    public static void main(String[] args) {
        //创建目标对象
        final Target target = new Target();

        //增强对象
        Advice advice = new Advice();

        //返回值 就是动态生成的代理对象
        TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
                target.getClass().getClassLoader(), //目标对象类加载器
                target.getClass().getInterfaces(), //目标对象相同的接口字节码对象数组
                new InvocationHandler() {
                    //调用代理对象的任何方法,实质执行的都是invoke方法
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //前置增强
                        advice.before();
                        method.invoke(target, args);//执行目标方法
                        //后置增强
                        advice.after();
                        return null;
                    }
                }
        );

        //调用代理对象的方法
        proxy.save();
    }
}

测试结果如下

2. cglib代理(了解就行)

package cn.leohams.cglib;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;

public class ProxyTest {
    public static void main(String[] args) {
        //创建目标对象
        final Target target = new Target();

        //增强对象
        final Advice advice = new Advice();

        //1. 创建增强器
        Enhancer enhancer = new Enhancer();
        //2. 设置父类(目标)
        enhancer.setSuperclass(Target.class);
        //3。 设置回调
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object proxy, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                //执行前置
                advice.before();
                //执行目标
                Object invoke = method.invoke(target, args);
                //执行后置
                advice.after();
                return invoke;
            }
        });
        //4. 创建代理对象
        Target proxy = (Target) enhancer.create();
        //调用代理对象的方法
        proxy.save();
    }
}

3. AOP

Spring的AOP实现底层就是对上面的动态代理的代码进行了封装,有几个名词给大家介绍一下

1. Target(目标对象):代理的目标对象

2. Proxy(代理):一个类被AOP织入增强之后,就产生一个结果代理类

3.Joinpoint(连接点):就是可以被增强的方法

4. Pointcut(切入点):就是我们增强的Joinpoint

5. Advice(增强):就是我们对要增强的方法进行增强的方式

6. Aspect(切面):就是切入点和通知的结合

7. Weaving(织入):就是指把增强应用到目标对象来创建新的代理对象的过程

现在我们进行一个AOP的快速入门

1. 首先导入相关依赖坐标,再有spring-context的前提下需要导入aspectjweaver依赖,因为现在这个包在AOP理念基础上做了集成和封装,而且比较与Spring更加轻量化,也更适合开发使用。

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.4</version>
    </dependency>
</dependencies>

2. 创建目标接口和目标类

package cn.leohams.aop;

public interface TargetInterface {
    public void save();
}
package cn.leohams.aop;

public class Target implements TargetInterface {
    @Override
    public void save() {
        System.out.println("nihao, save...");
    }
}

3. 创建切面类

package cn.leohams.aop;

public class MyAspect {
    public void before(){
        System.out.println("前置增强...");
    }
}

4. 将目标类和切面类的对象创建权交给spring,也就是在spring容器配置文件applicationContext.xml中加入他们的bean

    <!--配置目标对象-->
    <bean id="target" class="cn.leohams.aop.Target"></bean>

    <!--切面对象-->
    <bean id="myAspect" class="cn.leohams.aop.MyAspect"></bean>

5. 在配置文件中配置织入关系

    <!--配置织入,告诉框架哪些方法需要进行哪些增强-->
    <aop:config>
    <!--声明切面-->
        <aop:aspect ref="myAspect">
            <!--切面:切点+增强-->
            <aop:before method="before" pointcut="execution(public void cn.leohams.aop.Target.save())"/>
        </aop:aspect>
    </aop:config>

6. 测试

package com.leo.test;

import cn.leohams.aop.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AOPTest {

    @Autowired
    private TargetInterface target;

    @Test
    public void test1(){
        target.save();
    }
}

结果如下:

举报

相关推荐

Spring Aop

Spring ----AOP

spring Aop

Spring——AOP

Spring AOP

Spring:AOP

spring -- AOP

0 条评论