zoukankan      html  css  js  c++  java
  • AOP中环绕通知的写法

    package com.hope.utils;

    import org.aspectj.lang.ProceedingJoinPoint;

    /**
    * @author newcityman
    * @date 2019/11/22 - 23:29
    * 记录日志的类
    */
    public class Logger {
    /**
    * 前置通知
    */
    public void beforePrintLog(){
    System.out.println("前置通知");
    }

    /**
    * 后置通知
    */
    public void afterReturningPrintLog(){
    System.out.println("后置通知");
    }

    /**
    * 异常通知
    */
    public void afterThrowingPrintLog(){
    System.out.println("异常通知");
    }

    /**
    * 最终通知
    */
    public void afterPrintLog(){
    System.out.println("最终通知");
    }

    /**
    * 环绕通知
    */
    public Object aroundPrintLog(ProceedingJoinPoint pjp){
    Object rtValue = null;
    try {
    System.out.println("前置通知");
    Object[] args = pjp.getArgs();
    rtValue= pjp.proceed(args);
    System.out.println("后置通知");
    return rtValue;
    } catch (Throwable t) {
    System.out.println("异常通知");
    throw new RuntimeException(t);
    }finally {
    System.out.println("最终通知");
    }

    }
    }
    <?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">

    <!--配置spring的ioc,把service对象配置进来-->
    <bean id="accountService" class="com.hope.service.impl.AccountService"></bean>
    <!--配置logger类-->
    <bean id="logger" class="com.hope.utils.Logger"></bean>
    <!--配置AOP-->
    <aop:config>
    <!--配置切面-->
    <aop:aspect id="logAdvice" ref="logger">
    <aop:pointcut id="pt1" expression="execution(* *..*.*(..))"/>
    <!--配置通知的类型,并且建立通知方法和切入点方法的关联-->
    <!--<aop:before method="printLog" pointcut="execution(public void com.hope.service.impl.AccountService.saveAccount())"></aop:before>-->
    <!--<aop:before method="printLog" pointcut="execution(void com.hope.service.impl.AccountService.saveAccount())"></aop:before>-->
    <!--<aop:before method="printLog" pointcut="execution(* com.hope.service.impl.AccountService.saveAccount())"></aop:before>-->
    <!--<aop:before method="printLog" pointcut="execution(* *.*.*.*.AccountService.saveAccount())"></aop:before>-->
    <!--<aop:before method="printLog" pointcut="execution(* *.*.*.*.AccountService.saveAccount())"></aop:before>-->
    <!--<aop:before method="printLog" pointcut="execution(* *..AccountService.saveAccount())"/>-->
    <!--<aop:before method="printLog" pointcut="execution(* *..*.*())"/>-->
    <!--<aop:before method="beforePrintLog" pointcut-ref="pt1"/>-->
    <!--<aop:after-returning method="afterReturningPrintLog" pointcut="execution(* *..*.*(..))"/>-->
    <!--<aop:after-throwing method="afterThrowingPrintLog" pointcut="execution(* *..*.*(..))"/>-->
    <!--<aop:after method="afterPrintLog" pointcut-ref="pt1"/>-->
    <!--<aop:before method="printLog" pointcut="execution(* *..*.*(..))"/>-->
    <aop:around method="aroundPrintLog" pointcut-ref="pt1"/>
    </aop:aspect>
    </aop:config>
    </beans>


  • 相关阅读:
    再战徐州
    hibernate系列1
    linux 下python操作mysql
    java synchronized
    深入ASP.NET MVC之九:Ajax支持
    深入ASP.NET MVC之六:Model Binding的实现
    深入ASP.NET MVC之十:服务器端Model Validation
    深入ASP.NET MVC之四:Filter和Action的执行
    深入ASP.NET MVC之七:ActionResult的执行(View的加载和渲染)
    深入ASP.NET MVC之五:Model Binding
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11919321.html
Copyright © 2011-2022 走看看