开启注解扫描

<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. 【Ubuntu 16】DEB软件包管理

    一.背景介绍 开源软件最早的时候没有软件包和软件包管理器,用户只能下载源码包自行配置 编译 安装. 后来linux各发行版本推出了软件包格式和软件包管理程序 Red Hat.Centos使用RPM格式 ...

  2. 【Centos7】5分钟理解防火墙firewalld

    Centos7中默认将原来的防火墙iptables升级为了firewalld,firewalld跟iptables比起来至少有两大好处: 1.firewalld可以动态修改单条规则,而不需要像ipta ...

  3. DHCP解析

    DHCP解析... ------------------------ DHCP的工作原理: ====================== ======================DHCP动态主机地 ...

  4. 面试 | 商汤科技面试经历之Promise红绿灯的实现

    说在前面 说实话,刚开始在听到这个面试题的实话,我是诧异的,红绿灯?这不是单片机.FPGA.F28335.PLC的实验吗?! 而且还要用Promise去写,当时我确实没思路,只好硬着头皮去写,下来再r ...

  5. Virtualbox虚拟机Ubuntu共享文件夹设置 自动挂载

    1. 安装增强功能包(Guest Additions) 安装好Ubuntu 14.04 后,运行Ubuntu并登录.然后在VirtualBox的菜单里选择"设备(D)" -> ...

  6. golang channel无缓冲通道会发生阻塞的验证

    公司搞了午间技术par,本周我讲的主题是关于无缓冲通道channel是否会发生阻塞,并进行了验证. go语言中channel分为无缓冲通道和有缓冲通道两种 channel提供了一种在goroutine ...

  7. lnmp架构(第一篇)

    lnmp 架构 第一篇 nginx 源码安装 nginx的安装包:nginx-1.12.0.tar.gz 建议安装前的修改: 在nginx的解压包中修改文件nginx-1.12.0/src/core/ ...

  8. 如何修改int的打印内容——史上最难的JAVA面试题

    序 今天看到了一个比较特别的面试题,考察的是如何改变int的System.out.print的结果.题目如下: 下面的一句话"这是初级java实习生面试题"非常挑衅的激起了大家做题 ...

  9. node简单配置一台服务器

    要想使用nodeJS来搭建服务器,首先需要一个必备的条件:node必须安装,建议为4.0版本及以上: 在node中,为我们封装了好多类,搭建服务器需要的一个类是"http"类. 用 ...

  10. Httprequest 获取url 常用方法

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