开启注解扫描

<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安装MariaDB(Mysql)和简单配置

    1.安装MariaDB 安装命令 yum -y install mariadb mariadb-server 安装完成MariaDB,首先启动MariaDB systemctl start maria ...

  2. Ubuntu 14.04下Redis安装报错:“You need tcl 8.5 or newer in order to run the Redis test”问题解决

    Redis简介: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工 ...

  3. Windows桌面快捷图标上的小箭头的恢复

    Windows桌面快捷图标上的小箭头的恢复.. 桌面快捷图标上的小箭头的恢复: cmd /k reg add "HKEY_CLASSES_ROOT\lnkfile" /v IsSh ...

  4. sphinx随笔记了一下

    sphinx笔记 一:下载中文版coreseek包1:解压后,将etc下的mysql.conf文件复制一份放到上级目录下,改名为sphinx.conf2:配置文件: 2.1:source配置数据源so ...

  5. Java基础二

    1 关键字 定义:被java语言赋予了特殊含义的单词. 特点:关键字中的所有字母都为小写. 用于定义数据类型的关键字 class.interface.byte.short.int.long.float ...

  6. 挖个坑,写一个Spring+SpringMVC+Mybatis的项目

    想挖个坑督促自己练技术,有时候想到一个项目,大概想了一些要实现的功能,怎么实现.现在觉得自己差不多能完成QQ空间的主要功能了.准备立个牌坊,写一个类似功能的网站.并且把进度放到这里来. 初步计划实现以 ...

  7. [Caffe]使用经验积累

    Caffe使用经验积累 本贴记录Caffe编译好了,使用过程的常用命令与常见错误解决方式.如果对编译过程还存在问题,请参考史上最全的caffe安装过程配置Caffe环境. 1 使用方法 训练网络 xx ...

  8. Electron 实战桌面计算器应用

    前言 Electron 是一个搭建跨平台桌面应用的框架,仅仅使用 JavaScript.HTML 以及 CSS,即可快速而容易地搭建一个原生应用.这对于想要涉及其他领域的开发者来说是一个非常大的福利. ...

  9. c++ new 的相关

    首先是一个链接  这里 说的很详细了 http://www.cnblogs.com/alephsoul-alephsoul/archive/2012/10/17/2728019.html 关于c++ ...

  10. [REST] 1.REST的起源

    0. 世界上第一个网站 1990年12月20日,这一天对于现在的互联网来说意义非凡.欧洲核子研究组织(CREN)的科学家Tim Berners-Lee在一台NeXT电脑上启动了世界上的第一个网站(当然 ...