Spring中的环绕通知

Spring中的环绕通知

           

aop:around:

作用:
     用于配置环绕通知
属性:
   method:指定通知中方法的名称。
   pointct:定义切入点表达式
   pointcut-ref:指定切入点表达式的引用
说明:
   它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式

                 注意:   通常情况下,环绕通知都是独立使用的 
                     
 * 环绕通知
 * spring 框架为我们提供了一个接口:ProceedingJoinPoint,它可以作为环绕通知的方法参数。
 * 在环绕通知执行时,spring 框架会为我们提供该接口的实现类对象,我们直接使用就行。
 * 它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。
 * 通常情况下,环绕通知都是独立使用的

用于记录日志的工具类,里面提供了公共的代码

package com.utils;

import org.aspectj.lang.ProceedingJoinPoint;

//用于记录日志的工具类,里面提供了公共的代码
public class Logger {
    /*
    * 前置通知
    * */
    public void beforePrintLog(){
        System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
    }

    /*
     * 后置通知
     * */
    public void afterReturningPrintLog(){
        System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
    }

    /*
     * 异常通知
     * */
    public void afterThrowingPrintLog(){
        System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
    }

    /*
     * 最终通知
     * */
    public void afterPrintLog(){
        System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
    }
    
    /*
     * 环绕通知
     * spring 框架为我们提供了一个接口:ProceedingJoinPoint,它可以作为环绕通知的方法参数。
     * 在环绕通知执行时,spring 框架会为我们提供该接口的实现类对象,我们直接使用就行。
     * 它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。
     * 通常情况下,环绕通知都是独立使用的
     * */
    public Object aroundPrintLog(ProceedingJoinPoint pjp){
        //定义返回值
        Object rtValue = null;
         try {
             Object[] args = pjp.getArgs(); // 获取方法执行所需的参数

             System.out.println("环绕通知Logger类中的aroundPrintLog方法开始记录日志了。。。前置"); //前置通知:开启事务

             System.out.println("环绕通知Logger类中的aroundPrintLog方法开始记录日志了。。。后置");//后置通知

             rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)

             return rtValue;
             }catch(Throwable e) {
             System.out.println("环绕通知Logger类中的aroundPrintLog方法开始记录日志了。。。异常");// 异常通知
             throw new RuntimeException(e);
         }finally {
             System.out.println("环绕通知Logger类中的aroundPrintLog方法开始记录日志了。。。最终");//最终通知
         }
    }
}

配置文件bean.xml

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

<!--    配置spring的IOC-->
    <bean id="accountService" class="com.service.AccountServiceImpl"></bean>

    <bean id="logger" class="com.utils.Logger"></bean>

<!--    配置AOP-->
    <aop:config>
        <!--配置切点,在aspect外配置切点,此切点可为所以切面可用-->
        <aop:pointcut id="pt1" expression="execution(* com.service.*.*(..))"/>
        <!--配置切面-->
        <aop:aspect id="logAdvice" ref="logger">
            <!--配置前置通知
            <aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>-->

            <!--配置后置通知
            <aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>-->

            <!--配置异常通知
            <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>-->

            <!--配置最终通知
            <aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>-->

            <!--配置环绕通知-->
            <aop:around method="aroundPrintLog" pointcut-ref="pt1"></aop:around>
        </aop:aspect>
    </aop:config>
</beans>

账户的业务层接口

package com.service;

/*
* 账户的业务层接口
* */
public interface IAccountService {
    /*
    * 模拟保存账户
    * */
    public void saveAccount();

    /*
     * 模拟更新账户
     * */
    public void updateAccount(int i);

    /*
     * 模拟删除账户
     * */
    public int deleteAccount();

}

账户的业务层实现类

package com.service;

//账户的业务层实现类
public class AccountServiceImpl implements IAccountService {
    public void saveAccount() {
        System.out.println("执行了保存账户。。。");
    }

    public void updateAccount(int i) {
        System.out.println("执行了更新账户。。。"+i);
    }

    public int deleteAccount() {
        System.out.println("执行了删除账户。。。");
        return 0;
    }
}

测试类

import com.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService accountService = (IAccountService) ac.getBean("accountService");
        accountService.saveAccount();
    }

}

执行结果:
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mitsuha三葉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值