Spring框架(3)——Spring的AOP技术

目录

引入

一、AOP相关的概念

1.什么是AOP的技术?

 2. AOP的优势

二、Spring的AOP技术-配置文件方式

1. AOP相关的术语

 2. 基本准备工作

3. AOP配置文件方式的入门

三、Spring的AOP技术-注解方式

1. AOP注解方式入门程序


引入

我们通过一个小例子来理解 ,登录的原理,如下

如上图所示这是一个基本的登录原理图,但是如果我们想要在这个登录之上添加一些新的功能,比如权限校验

那么我们能想到的就有两种方法:

①:通过对源代码的修改实现

②:不通过修改源代码方式添加新的功能 ——AOP实现

一、AOP相关的概念

1.什么是AOP的技术?

        在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程。

        AOP是一种编程范式,隶属于软工范畴,指导开发者如何组织程序结构。

        AOP最早由AOP联盟的组织提出的,制定了一套规范.Spring将AOP思想引入到框架中,必须遵守AOP联盟的规范,通过预编译方式或者运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。

        利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

        AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(事务管理、安全检查、缓存)。为什么要学习AOP,其可以在不修改源代码的前提下,对程序进行增强!!

 2. AOP的优势

运行期间,不修改源代码的情况下对已有的方法进行增强

优势:

① 减少重复的代码

② 提供开发的效率

③ 维护方便

二、Spring的AOP技术-配置文件方式

1. AOP相关的术语

Joinpoint(连接点): 类里面有哪些方法可以增强这些方法称为连接点

Pointcut(切入点) :所谓切入点是指我们要对哪些Joinpoint进行拦截的定义

Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.

        通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)

Aspect(切面)-- 是 切入点+通知 的结合,以后咱们自己来编写和配置的

 2. 基本准备工作

AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法,AspectJ实际上是对AOP编程思想的一个实践。

3. AOP配置文件方式的入门

创建maven项目,导入坐标依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!--AOP联盟-->
    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
    </dependency>
    <!--Spring Aspects-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <!--aspectj-->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.3</version>
    </dependency>
</dependencies>

创建被增强的类

在这个类中,我们要增强的方法为login();

public class Cat {
    //连接点/切入点
    public void add(){
        System.out.println("add......");
    }
    public void update(){
        System.out.println("update......");
    }
    //连接点/切入点
    public void delete(){
        System.out.println("delete......");
    }
    public void login(String username,String password){

        System.out.println("进行数据库对比");
    }
}

 将目标类配置到xml文件中

<bean id="cat" class="com.qcby.service.Cat"></bean>

定义切面类

package com.qcby.service;


public class 权限 {
    public void 验证(){
//        这是一个前置通知
        System.out.println("before.............");
    }
}

 将切面类配置到xml文件中

    <bean id="qx" class="com.qcby.service.权限"></bean>

在xml配置文件中完成aop的配置

<!--配置切面-->
    <aop:config>
        <!--配置切面 = 切入点 + 通知组成-->
        <aop:aspect ref="qx">
            <!--前置通知:UserServiceImpl的save方法执行前,会增强-->
            <!--pointcut:后边是切入点表达式,作用是知道对对面的那个方法进行增强-->
            <aop:before method="验证" pointcut="execution(public void com.qcby.service.Cat.login(..))"/>
        </aop:aspect>
    </aop:config>

  <aop:before method="验证"

                      pointcut="execution(public void com.qcby.service.Cat.login(..))"/>

在这条语句中,before为通知的类型。pointcut后为目标类的方法路径,当方法需要传参时用“..”代替。

切入点表达式的格式如下:

                        execution([修饰符] [返回值类型] [类全路径] [方法名 ( [参数] )])

修饰符可以省略不写,不是必须要出现的。返回值类型是不能省略不写的,根据你的方法来编写返回值,可以使用 * 代替。

Test测试类

        @org.junit.Test
        public void aopTest1(){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring.xml");
            Cat cat = (Cat) applicationContext.getBean("cat");
            cat.login("小猫","1234");

测试结果如下:

我们会发现,检查方法在login方法前执行


以上的例子为前置通知,其他的均类似,环绕通知在切面类和xml配置文件处略有不同,如下:

切面类

 public void 验证(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("before.............");
        //  执行被增强的方法
        proceedingJoinPoint.proceed();
        System.out.println("after.............");
    }
  • ProceedingJoinPoint proceedingJoinPoint:这是方法的参数,类型是ProceedingJoinPoint。——它是Spring AOP框架提供的一个接口,用于访问连接点(Join Point)的信息,如方法的签名和参数。在环绕通知中,这个对象还提供了proceed()方法,用于执行被增强的方法。

 xml配置文件

 <aop:config>
        <!--配置切面 = 切入点 + 通知组成-->
        <aop:aspect ref="qx">
            <!--前置通知:UserServiceImpl的save方法执行前,会增强-->
            <!--pointcut:后边是切入点表达式,作用是知道对对面的那个方法进行增强-->
            <aop:around method="验证" pointcut="execution(public void com.qcby.service.Cat.login(..))"/>
        </aop:aspect>
  </aop:config>

结果如下: 

我们来总结一下流程:

下图是我们实现AOP的框架结构和流程图

三、Spring的AOP技术-注解方式

1. AOP注解方式入门程序

创建maven工程,导入坐标。编写接口,完成IOC的操作。步骤略。

编写切面类

给切面类添加注解 @Aspect,编写增强的方法,使用通知类型注解声明

配置xml扫描注解

  <!--开启注解扫描-->
 <context:component-scan base-package="com.qcby"></context:component-scan>

配置注解

@Component设置bean

@Component
public class Cat {
    //连接点/切入点
    public void add(){
        System.out.println("add......");
    }
    public void update(){
        System.out.println("update......");
    }
    //连接点/切入点
    public void delete(){
        System.out.println("delete......");
    }
    public void login(String username,String password){

        System.out.println("进行数据库对比");
    }
}

给切面类添加注解 @Aspect,编写增强的方法,使用通知类型注解声明。在这里我把前置通知和环绕通知写在了一起。注意观察区别。

package com.qcby.service;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect  //生成代理对象
public class 权限 {
    @Before(value = "execution(public void com.qcby.service.Cat.login(..))")
    public void before(){
        System.out.println("before.............");

    }

    @Around(value = "execution(public void com.qcby.service.Cat.login(..))")
    public void 验证(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("before.............");
        //  执行被增强的方法
        proceedingJoinPoint.proceed();
        System.out.println("after.............");
    }
  
}

通知类型注解

        @Before -- 前置通知

        @AfterReturing -- 后置通知

        @Around -- 环绕通知(目标对象方法默认不执行的,需要手动执行)

        @After -- 最终通知

        @AfterThrowing -- 异常抛出通知

 在xml配置文件中开启自动代理

<!--    开启Aspect生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

测试类Test

 @org.junit.Test
        public void aopTest1(){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring.xml");
            Cat cat = (Cat) applicationContext.getBean("cat");
            cat.login("小猫","1234");
    }

测试结果如图:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值