Spring中的AOP操作(动态代理)

AOP

1.AOP概述
2.AOP底层原理
3.AOP操作相关术语

AOP概念

AOP:面向切面(方面)编程,简单理解就是 扩展功能不用修改源代码就能够实现。

  • AOP采取 横向抽取机制,取代了传统 纵向继承体系 重复性代码。(性能监视、事务管理、安全检查、缓存)
AOP底层原理:

一张不是用动态代理实现图
这里有两张动态代理实现图
这里有两张动态代理实现图

AOP操作相关术语:

Joinpoint(连接点):所谓连接点是指那些被拦截到的点。也可以说是把业务无关的功能加入到业务的时机,而这些时机包括:调用方法,调用属性,抛出异常三个时机。在Spring中,这些点指的是调用方法时机,因为Spring只支持方法类型的连接点。

Pointcut(切入点):所谓切入点是指我们要对哪儿些Joinpoint进行拦截定义。也就是所我们指定要增强的方法叫做切入点。

Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知,通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)。也就是说增强的逻辑,称为增强,比如扩展日志功能,这个日志功能称为增强。

  • 前置通知:在方法之前执行
  • 后置通知:在方法之后执行
  • 异常通知:方法出现异常
  • 最终通知:在后置之后执行
  • 环绕通知:在方法之前和之后执行

Aspect(切面):是切入点和通知(引介)的结合。把增强应用到具体方法上面,这个过程被称为切面。

Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下,Introduction 可以在运行期为类动态地添加一些方法或Field。

Target(目标对象):代理的目标对象(要增强的类)

Weaving(织入):是把增强应用到目标的过程。把advice应用到target的过程。

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

//对应执行顺序底层结构:

try
{
    //  执行前置通知;

    //  执行目标方法;

    // 执行返回通知;
}
catche(Exception e)
{
    // 执行异常通知;
}
finally
{
    // 执行后置通知;
}

Spring 的 AOP 操作

在Spring里面进行 AOP 操作,使用Aspect实现
  • Aspect不是Spring的一部分,和Spring一起使用进行AOP操作
Aspect简介

1.Aspect是一个面向切面的框架,它扩展了Java语言。Aspect定义了AOP语法所以它有一个专门的编译器用来生成遵循Java字节编码规范的Class文件。
2.Aspect是一个基于Java语言的AOP框架
3.Spring2.0以后新增了对Aspect切点表达式支持
4.@Aspect是Aspect 1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
5.新版本Spring框架,建议使用Aspect方式类开发AOP
6.使用Aspect需要导入Spring AOP和 Aspect相关jar包

Spring AOP(同时也是Spring的JavaBean注解管理类jar包,但是在xml中要引入xmlns:context=”http://www.springframework.org/schema/context” 这个约束,如果是直接使用AOP的动态代理注入方法的话,就不需要引入上面的约束而是有 xmlns:aop=”http://www.springframework.org/schema/aop”就够了,两个注解相同的地方就是都要引入aop这个jar包,现在不懂,到了后面用AOP注解注入方法就知道了)下载地址
AspectJ相关jar包下载

使用AspectJ实现AOP由两种方式:

1.基于Aspect的XML配置
2.基于Aspect的注解方式

基于AspectJ的XML配置

AOP操作准备:

1.除了导入基本jar包之外,还需要导入aop相关的jar包
2.创建Spring的核心配置文件,导入aop的约束

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd<!-- 关于JavaEean管理的约束,能够通过xml配置实现 -->
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd<!-- 关于AOP动态代理的约束 -->
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 关于JavaEean管理通过注解实现的约束,也就是添加注解后自动创建JavaBean,但是这个注解不是AOP动态代理的注解,使用AOP动态代理注解时不需要这个,而需要上面aop这个约束。 -->
通过下面方式能够找到上面的约束代码:

这里写图片描述

从最下面开始找

这里写图片描述

使用表达式配置切入点

1.通过配置实现Spring创建对象给Spring管理
2.配置切入点:实际增强的方法
3.配置切入点常用的表达式:execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

  • execution(* cn.domarvel.aop.Book.add(..))其中:第一个*表示任意修饰符的方法,”..”表示有参数或者无参数。表达式的意思是对类cn.domarvel.aop.Book里面的add方法进行增强
  • execution(* cn.domarvel.aop.Book.*(..))表示对类里面的所有方法进行增强
  • execution(* *.*(..))对所有类的所有方法进行增强
  • 匹配所有save开头的方法 execution(* save*(..))
  • execution(* cn.domarvel.service..*.*(..)),匹配service下,所有的类的所有方法。
例子:前置增强

要被增强的类:

public class LangDao {

    public void showLangDao(){
        System.out.println("LangDao");
    }
}

增强类:

public class StrengLangDao {

    public void strengLangDao(){
        System.out.println("前置增强LangDao");
    }
}

通过配置实现增强:

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Spring核心配置文件中配置 -->

<!-- 配置对象 -->
<bean id="langDao" class="cn.domarvel.dao.LangDao"></bean>
<bean id="strengLangDao" class="cn.domarvel.dao.StrengLangDao"></bean>
<!-- 配置AOP操作 -->
<aop:config>
    <!-- 配置切入点 -->
    <aop:pointcut expression="execution(* cn.domarvel.dao.LangDao.showLangDao(..))" id="pointcutLangDao"/>
    <!-- 
        配置切面:把增强用到方法上面
    -->
    <aop:aspect ref="strengLangDao">
        <!-- 
            配置增强类型:method增强类里面使用哪儿个方法作为前置
        -->
        <aop:before method="strengLangDao" pointcut-ref="pointcutLangDao"/>
    </aop:aspect>
</aop:config>
</beans>

测试代码:

public class LangDaoTest {

    @Test
    public void showLangDaoStreng(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        LangDao langDao=(LangDao) context.getBean("langDao");
        langDao.showLangDao();
    }
}
/*
输出:

前置增强LangDao
LangDao
*/
前置增强和后置增强配置和使用都是很类似的。

需要改变的是:

前置:<aop:before method="strengLangDao" pointcut-ref="pointcutLangDao"/>
后置:“`

我们不需要改变其它的类写法,只需要改变配置文件信息:
整体配置:

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Spring核心配置文件中配置 -->

<!-- 配置对象 -->
<bean id="langDao" class="cn.domarvel.dao.LangDao"></bean>
<bean id="strengLangDao" class="cn.domarvel.dao.StrengLangDao"></bean>
<!-- 配置AOP操作 -->
<aop:config>
    <!-- 配置切入点 -->
    <aop:pointcut expression="execution(* cn.domarvel.dao.LangDao.showLangDao(..))" id="pointcutLangDao"/>
    <!-- 
        配置切面:把增强用到方法上面
    -->
    <aop:aspect ref="strengLangDao">
        <!-- 
            配置增强类型:method增强类里面使用哪儿个方法作为后置
        -->
        <aop:after-returning method="strengLangDao" pointcut-ref="pointcutLangDao"/><!-- 注意:这里写aop:after标签也是能够执行的 -->
    </aop:aspect>
</aop:config>
</beans>
aop:after-returning和aop:after的区别就是:

aop:after-returning:

当被增强方法出现异常后,增强方法不会执行

aop:after:

不管被增强方法是否成功执行都会执行后置方法

不懂可以看前面的增强方法执行顺序。
例子:环绕增强

被增强类:

public class LangDao {

    public void showLangDao(){
        System.out.println("LangDao");
    }
}

增强类:

public class StrengLangDao {

    public void strengHuanRaoLangDao(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println("方法之前");
        try {
            proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("方法之后");
    }
}

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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Spring核心配置文件中配置 -->

<!-- 配置对象 -->
<bean id="langDao" class="cn.domarvel.dao.LangDao"></bean>
<bean id="strengLangDao" class="cn.domarvel.dao.StrengLangDao"></bean>
<!-- 配置AOP操作 -->
<aop:config>
    <!-- 配置切入点 -->
    <aop:pointcut expression="execution(* cn.domarvel.dao.LangDao.showLangDao(..))" id="pointcutLangDao"/>
    <!-- 
        配置切面:把增强用到方法上面
    -->
    <aop:aspect ref="strengLangDao">
        <!-- 
            配置增强类型:method增强类里面使用哪儿个方法作为环绕
        -->
        <aop:around method="strengHuanRaoLangDao" pointcut-ref="pointcutLangDao"/>
    </aop:aspect>
</aop:config>
</beans>

测试代码;

public class LangDaoTest {

    @Test
    public void showLangDaoStreng(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        LangDao langDao=(LangDao) context.getBean("langDao");
        langDao.showLangDao();
    }   
}
//输出:
/*
方法之前
LangDao
方法之后
*/

AOP个人理解

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值