开启注解扫描

<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. js中的分支与循环

    一.js的分支结构 js的分支结构包括:if-else结构.多重if结构.嵌套if结构和switch-case结构 1.if-else结构 1.结构的写法:    if(判断条件){    //条件为 ...

  2. 【Spring】XML配置整合Mybatis

    注意:项目开发使用了mybatis的mapper代理! 首先是mybatis自己的配置文件,被spring整合之后,只有typeAliases存在了,其他都整合在了spring-mybatis.xml ...

  3. 面试的妹纸问我:web缓存设置不是后台的事情吗?

    背景介绍 团队最近在招前端开发,早上收到一封简历,是个妹纸,从技能点来看还算符合要求,于是约了下午3点过来面试. 整个面试过程持续了大约40分钟,问的题目也比较常规,其中一道题就是"常见的性 ...

  4. maven使用实战

    maven使用实战 创建项目 在eclipse中创建maven项目之后,会生成.classpath / .setting / .project 三个文件或者文件夹 .classpath 这个文件是用来 ...

  5. 并发是个什么鬼之同步工具类CountDownLatch

    扯淡 写这篇文章,我先酝酿一下,实不相瞒,脱离底层太久了,更确切的情况是,真没曾认真研究过.就目前来说,很多框架包括工具类已经把实现封装的很深,你只需轻轻的调用一下API,便不费半点力气.以至于大家会 ...

  6. QQ无法通过ISA2006&TMG2010代理收发图片问题解决

    近期公司有业务需求通过TMG访问QQ,但配置多次均无法通过QQ收发图片,文字输入正常. 目前已解决,供参考: 这个问题是SSL端口默认使用了443,但QQ的离线文件不使用这个端口.所以ISA会把QQ的 ...

  7. 网络唤醒原理浅析(Wake On LAN)

    之前我的一篇文章<网络唤醒全攻略(Wake On Lan)>介绍过如何设置远程唤醒电脑,着重于使用,这篇主要从原理方面解析一下当中的奥妙: 原理 将唤醒魔术包发送的被唤醒机器的网卡上,魔术 ...

  8. SVN的branch合并到trunk的过程思考

    SVN branch合并到主线的整个过程相对来说还是比较繁琐的,下面一个图揭示了一个大概的过程: 1. 将branch上的代码update到本地. 2.将 trunk上的代码也update到本地. 3 ...

  9. Cognos 11.0快速开发指南 Ⅰ

    1. 概述 Cognos Analysics 11,是IBM在Cognos BI 10的版本基础上,吸取业界流行的敏捷BI理念,强化了自助式分析的一款强大BI开发平台工具.其官方文档内容丰富,但是较为 ...

  10. [js高手之路]node js系列课程-创建简易web服务器与文件读写

    web服务器至少有以下几个特点: 1.24小时不停止的工作,也就是说这个进程要常驻在内存中 2.24小时在某一端口监听,如: http://localhost:8080, www服务器默认端口80 3 ...