开启注解扫描

<context:component-scan base-package="aopSpring"></context:component-scan>

将AOP的注解应用到容器中
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

把横切关注点的代码添加到切面类中

@component

@Aspect

应用如下

aop/ArithMath

import org.springframework.stereotype.Component;
@Component
public class ArithMath {
public ArithMath(){}
public int add(int i,int j){
return i + j;
}
public int div(int i,int j){
return i / j;
}
}

在ArithMath方法执行过程中插入日志

编写切面类aop/ArithMathAopImp

@Component
@Aspect
public class ArithMathAopImp {
  //前置增强@Before
@Before("execution(* aopSpring.ArithMath.add(int,int))")
public void loggingArithMath(JoinPoint joinPoint){ //添加参数JoinPoint 可以获取目标的参数
  String methd = joinPoint.getSignature().getName();
  List<Object> list = Arrays.asList(joinPoint.getArgs());
  System.out.println("the mathod "+ methd +" on load begin whih "+list);
}

  //后置增强不可访问方法返回值
  @After(value="execution(* aopSpring.ArithMath.add(int,int))")
  public void AfterMethod(JoinPoint joinPoint){
    String method = joinPoint.getSignature().getName();
    System.out.println("the mathod "+ method +" on end!");
  }


  //返回通知
  @AfterReturning(value="execution(* aopSpring.ArithMath.add(int,int))",returning="rt")
  public void AfterReturn(JoinPoint joinPoint,Object rt){
    String method = joinPoint.getSignature().getName();
    System.out.println("the mathod "+ method +" return "+rt);
  }

  //异常通知,
  @AfterThrowing(value="execution(* aopSpring.ArithMath.*(int,int))",throwing="ex")
  public void AfterThrowingMethod(JoinPoint joinPoint,Exception ex){  //指定特定的异常(Exception )发生时才执行代码
    String method = joinPoint.getSignature().getName();
    System.out.println("the mathod "+ method +" throw "+ex);
  }


  /**
  * 环绕通知@Around
  * 通知必须加参数ProceedingJoinPoint,且必须有返回值
  * proceed()表示执行目标方法
  */
  @Around("execution(* aopSpring.ArithMath.*(int,int))")
  public Object AroundMethed(ProceedingJoinPoint pj){
    Object rt = null;
    String method = pj.getSignature().getName();
    try {
      //前置
      System.out.println(method + "before");
      rt = pj.proceed();
      //后置
      System.out.println(method + "after");
    } catch (Throwable e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.out.println(method + e);
    }
    //后置
    System.out.println(method + "returnning");
    return rt;
  }

 

基于注解方式实现Aop的更多相关文章

  1. 基于AspectJ的注解方式进行AOP开发

    -------------------siwuxie095                                     基于 AspectJ 的注解方式进行 AOP 开发         ...

  2. 基于注解的Spring AOP的配置和使用

    摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...

  3. 基于注解的Spring AOP示例

    基于注解的Spring AOP示例 目录 在XML配置文件中开启 @AspectJ 支持 声明切面及切入点 声明通知 测试 结语 在XML配置文件中开启 @AspectJ 支持 要使用Spring的A ...

  4. Shiro入门之二 --------基于注解方式的权限控制与Ehcache缓存

    一  基于注解方式的权限控制 首先, 在spring配置文件applicationContext.xml中配置自动代理和切面 <!-- 8配置自动代理 -->    <bean cl ...

  5. Spring声明式事务管理(基于注解方式实现)

    ----------------------siwuxie095                                 Spring 声明式事务管理(基于注解方式实现)         以转 ...

  6. (转)使用Spring的注解方式实现AOP的细节

    http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...

  7. (转)使用Spring的注解方式实现AOP入门

    http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...

  8. Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop

    Spring系列之aop aop是什么?+xml方式实现aop+注解方式实现aop 什么是AOP? AOP为Aspect Oriented Programming 的缩写,意识为面向切面的编程,是通过 ...

  9. Elasticsearch-mapper 基于注解方式生成mapping(2.0以上)

    Elasticsearch生成mapping的方式上有多种方式,我们可以把mapping做成配置文件,也可以用spring-data-elasticsearch基于注解生成. 在基于注解生成这种方式上 ...

随机推荐

  1. 修改(同步)linux时间

    新申请下来的机器的时间现在我们所在的时区不一致,需要同步成东八区 查看当前的时间: date -R; date +%z 修改时区: cp /usr/share/zoneinfo/Asia/Shangh ...

  2. CSS选择器大汇总

    CSS选择器是学习CSS以及Web编程的基础. 整理出常用的CSS选择器,供自己和大家一起学习. 基本选择器 * /*通用元素选择器,匹配页面任何元素(这也就决定了我们很少使用)*/ #id /*id ...

  3. 【Linux】查看系统位数

    1.getconf LONG_BIT 2.uname -m 3.arch 4.file /sbin/init 5.file /bin/ls

  4. linux虚拟机ip地址更改

    在虚拟机模式下 进入 cd /etc/sysconfig/network-scripts/ vim ifcfg-eth0 编辑 IPADDR=新的内网ip PREFIX  = 24 (对应255.25 ...

  5. TP-Link路由器的设置向导

    TP-Link路由器的设置向导.. 一.准备工作: 1.首先是路由器的安装,主要是将路由器电源接上,并通电,然后就是网线的连接.如果是拨号上网用户,请将猫引出的网线插入到TP-Link路由器最特别的一 ...

  6. Maven详解(七)------ 创建Web工程以及插件原理

    1.什么是 Maven 插件? 上一篇博客我们将了 Maven 的生命周期,我们知道 Maven 的核心是生命周期,生命周期指定了 Maven 命令执行的流程顺序.但是真正实现流程的工程是由插件来完成 ...

  7. TensorFlow框架(5)之机器学习实践

    1. Iris data set Iris数据集是常用的分类实验数据集,由Fisher, 1936收集整理.Iris也称鸢尾花卉数据集,是一类多重变量分析的数据集.数据集包含150个数据集,分为3类, ...

  8. Bootstrap中的datetimepicker用法,只看一眼就全懂了

    本文实例为大家分享了bootstrap datetimepicker日期插件的简单使用,供大家参考,具体内容如下 首先在文件头部引入必要的文件: 1 2 <link rel="styl ...

  9. 打造基于Clang LibTooling的iOS自动打点系统CLAS(三)

    1. 源码变换 第一章我们提到过,CLAS的本质是对源码做一次非常简单的变换(有些文章里称作变形),即Source-Source-Transformation,将打点代码精确地插入到目标函数的首部,保 ...

  10. 再起航,我的学习笔记之JavaScript设计模式19(状态模式)

    状态模式 概念介绍 状态模式(State):当一个对象的内部状态发生改变时,会导致其行为的改变,这看起来像是改变了对象 示例演示 在我们写项目的过程中或多或少会遇到如下的多分支判断 function ...