栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring AOP的五种通知方式代码实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring AOP的五种通知方式代码实例

这篇文章主要介绍了Spring AOP的五种通知方式代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

AOP的五种通知方式:

前置通知:在我们执行目标方法之前运行(@Before)

后置通知:在我们目标方法运行结束之后,不管有没有异常(@After)

返回通知:在我们的目标方法正常返回值后运行(@AfterReturning)

异常通知:在我们的目标方法出现异常后运行(@AfterThrowing)

环绕通知:目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,joinPoint.procced()就是执行目标方法的代码 。环绕通知可以控制返回对象(@Around)

一、导jar包

  • com.springsource.net.sf.cglib-2.2.0.jar
  • com.springsource.org.aopalliance-1.0.0.jar
  • com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
  • commons-logging-1.1.3.jar
  • spring-aop-4.0.0.RELEASE.jar
  • spring-aspects-4.0.0.RELEASE.jar
  • spring-beans-4.0.0.RELEASE.jar
  • spring-context-4.0.0.RELEASE.jar
  • spring-core-4.0.0.RELEASE.jar
  • spring-expression-4.0.0.RELEASE.jar
  • spring-jdbc-4.0.0.RELEASE.jar
  • spring-orm-4.0.0.RELEASE.jar
  • spring-tx-4.0.0.RELEASE.jar
  • spring-web-4.0.0.RELEASE.jar
  • spring-webmvc-4.0.0.RELEASE.jar

二、在类路径下建applicationContext.xml配置文件



  
  

  
  

三、接口

//接口
public interface ArithmeticCalculator {
  int add(int i, int j);
  int sub(int i, int j);
  int mul(int i, int j);
  int div(int i, int j);
}

四、实现类

package com.atguigu.spring.aop;

import org.springframework.stereotype.Component;



//实现类
@Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
  @Override
  public int add(int i, int j) {
    int relust = i+j;
    return relust;
  }

  @Override
  public int sub(int i, int j) {
    int relust = i-j;
    return relust;
  }

  @Override
  public int mul(int i, int j) {
    int relust = i*j;
    return relust;
  }

  @Override
  public int div(int i, int j) {
    int relust = i/j;
    return relust;
  }
}

五、定义切面类

package com.atguigu.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;


@Component
@Aspect
public class LoggingAspect {
  

  @Before("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))")
  public void beforeMethod(JoinPoint joinPoint){
    String methodName = joinPoint.getSignature().getName();
    Object[] args = joinPoint.getArgs();

    System.out.println("The method "+methodName+" begins with "+ Arrays.asList(args));
  }

  @After("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))")
  public void afterMethod(JoinPoint joinPoint){
    String methodName = joinPoint.getSignature().getName();

    System.out.println("The method "+methodName +" end......");
  }


  
   
  @AfterReturning(value = "execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))",
   returning = "result")
  public void afterReturning(JoinPoint joinPoint,Object result){
    String methodName = joinPoint.getSignature().getName();
    System.out.println("The method "+methodName +" end......result:"+result);
  }


  
   
  @AfterThrowing(value = "execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))", throwing = "ex")
  public void afterThrowing(JoinPoint joinPoint, Exception ex){
    String methodName = joinPoint.getSignature().getName();
    System.out.println("The method "+methodName +"occurs exception :" +ex);
  }

  
  
}

六、测试

public class Test_aop {
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) context.getBean("arithmeticCalculator");
    System.out.println(arithmeticCalculator.getClass().getName());
    int result = arithmeticCalculator.add(1,2);
    System.out.println("result:"+result);
    result = arithmeticCalculator.div(200,0);
    System.out.println("result:"+result);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/135871.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号