基于 XML 的 AOP 配置

(一)环境搭建
1、导入依赖

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.9.1</version>
        </dependency>

2、创建 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"
       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">
</beans>

3、配置 spring 的 ioc

4、抽取公共代码制作成通知

(二)配置AOP

配置方式

<aop:config>
    <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/>
    <aop:aspect id="txAdvice" ref="txManager">
        <!-- 配置环绕通知 -->
        <aop:around method="transactionAround" pointcut-ref="pt1"/>
    </aop:aspect>
</aop:config>

1、把通知类用 bean 标签配置起来

<!-- 配置通知 -->
<bean id="txManager" class="com.itheima.utils.TransactionManager">
    <property name="dbAssit" ref="dbAssit"></property>
</bean> 

2、声明 aop 配置

aop:config:
    作用:用于声明开始 aop 的配置
<aop:config>
    <!-- 配置的代码都写在此处 -->
</aop:config>

3、配置切入点表达式

aop:pointcut:
    作用:
        用于配置切入点表达式,就是指定对哪些类的哪些方法进行增强。
    属性:
        expression:用于定义切入点表达式。
        id:用于给切入点表达式提供一个唯一标识
<aop:pointcut 
      id="pt1"
      expression="execution(
          public void com.itheima.service.impl.AccountServiceImpl.transfer(
              java.lang.String, java.lang.String, java.lang.Float)
      )
"/>

4、配置切面

aop:aspect:
    作用:
        用于配置切面。
    属性:
        id:给切面提供一个唯一标识。
        ref:引用配置好的通知类 bean 的 id。
<aop:aspect id="txAdvice" ref="txManager">
    <!--配置通知的类型要写在此处-->
</aop:aspect>

5、配置对应的通知类型

aop:before
    作用:
        用于配置前置通知,指定增强的方法在切入点方法之前执行
    属性:
        method:用于指定通知类中的增强方法名称
        ponitcut-ref:用于指定切入点的表达式的引用
        poinitcut:用于指定切入点表达式
    执行时间点:
        切入点方法执行之前执行
    <aop:before method="beginTransaction" pointcut-ref="pt1"/>

aop:after-returning
    作用:
        用于配置后置通知
    属性:
        method:指定通知中方法的名称。
        pointct:定义切入点表达式
        pointcut-ref:指定切入点表达式的引用
    执行时间点:
        切入点方法正常执行之后,它和异常通知只能有一个执行
    <aop:after-returning method="commit" pointcut-ref="pt1"/>

aop:after-throwing
    作用:
        用于配置异常通知
    属性:
        method:指定通知中方法的名称。
        pointct:定义切入点表达式
        pointcut-ref:指定切入点表达式的引用
    执行时间点:
        切入点方法执行产生异常后执行,它和后置通知只能执行一个
    <aop:after-throwing method="rollback" pointcut-ref="pt1"/>

aop:after
    作用:
        用于配置最终通知
    属性:
        method:指定通知中方法的名称。
        pointct:定义切入点表达式
        pointcut-ref:指定切入点表达式的引用
    执行时间点:
        无论切入点方法执行时是否有异常,它都会在其后面执行。
    <aop:after method="release" pointcut-ref="pt1"/>
aop:around:
    作用:
        用于配置环绕通知
    属性:
        method:指定通知中方法的名称。
        pointct:定义切入点表达式
        pointcut-ref:指定切入点表达式的引用
    说明:
        它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。
        注意:
        通常情况下,环绕通知都是独立使用的
    /**
    * 环绕通知
    * @param pjp
    * spring 框架为我们提供了一个接口:ProceedingJoinPoint,它可以作为环绕通知的方法参数。
    * 在环绕通知执行时,spring 框架会为我们提供该接口的实现类对象,我们直接使用就行。
    * @return
    */
    public Object transactionAround(ProceedingJoinPoint pjp) {
        //定义返回值
        Object rtValue = null;
        try {
            //获取方法执行所需的参数
            Object[] args = pjp.getArgs();
            //前置通知:开启事务
            beginTransaction();
            //执行方法
            rtValue = pjp.proceed(args);
            //后置通知:提交事务
            commit();
        }catch(Throwable e) {
            //异常通知:回滚事务
            rollback();
            e.printStackTrace();
        }finally {
            //最终通知:释放资源
            release();
        }
        return rtValue;
    }
posted @   max_yan  阅读(46)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示