开启注解扫描

<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. GC选择之CMS 并发标记清除

    CMS收集器 Concurrent Markup Sweep 并发标记清除 使用了标记-清除算法 与标记-压缩相比,并发阶段会降低吞吐量 算法作用在老年代以及永久区(新生代使用ParNew) -XX: ...

  2. windows 资源监视器

    windows的资源监视器有很强大的资源监视能力 win+r输入resmon.exe即可打开

  3. JS中的DOM操作和事件

    [DOM树节点] DOM节点分为三大类: 元素节点. 属性节点. 文本节点: 文本节点.属性节点属于元素节点的子节点.操作时,均需要先取到元素节点,再操作子节点:可以使用getElement系列方法, ...

  4. 【DDD】领域驱动设计实践 —— 架构风格及架构实例

    概述 DDD为复杂软件的设计提供了指导思想,其将易发生变化的业务核心域放置在限定上下文中,在确保核心域一致性和内聚性的基础上,DDD可以被多种语言和多种技术框架实现,具体的框架实现需要根据实际的业务场 ...

  5. Springboot 框架实现rest接口风格

    在springboot中的一些注解解释: http://blog.csdn.net/u010399316/article/details/52913299 书写规则可参照这个: http://blog ...

  6. sqlserver自定义函数

    标量函数 RETURNS 子句指定一种标量数据类型,则函数为标量值函数. 语法 Create function 函数名(参数) Returns 返回值数据类型 [with {Encryption | ...

  7. Spring事务管理(一)

    对于Spring相信很多做web开发的小活动一定不陌生,Spring中我们经常谈到的就是IOC和AOP,但是对于Spring的事务管理,相信大家一定也很感兴趣,今天我们就探讨一下Spring中的事务管 ...

  8. Httprequest 获取url 常用方法

    HttpServletRequest常用获取URL的方法         1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路 ...

  9. MITNIK ATTACK

    Https 443 http 80 TCP/IP 协议栈:将数据封装包头 传输层报头 Ack回复确认位 FIN结束位 SIN 开始位 RST 重置位 Seq 序号位 网络层报头 目的地址 原地址 报文 ...

  10. 第二次项目冲刺(Beta阶段)5.23

    1.提供当天站立式会议照片一张 会议内容: ①检查前一天的任务情况,将遇到的瓶颈反馈,看看团队成员是否有好的建议. ②制定新一轮的任务计划. 2.每个人的工作 (1)工作安排 队员 今日进展 明日安排 ...