Spring AOP With AspectJ

一.AOP和拦截器

   某些情况下,AOP和拦截器包括Filter能够实现同样的功能,一般都是请求即controller层的操作,这三个执行顺序为Filter>Interceptor>AOP,当然这里面的区别我会重新写一篇文章讲解,这里面提一下就是想告诉大家,不一定要使用AOP,个人感觉用Filter和Interceptor实现的更方便简单一点。

二.AOP 准备

    在spring框架下,你还需要添加aspectjrt,aspectjweaver和cglib 的相关jar包,maven项目的pom.xml中可直接在maven公服获取配置。

    在spring-mvc.xml中配置aop,并添加<aop:aspectj-autoproxy /> 。

    对于aspect类,对类添加@Aspect和@Component即可。

三.Spring AOP简介

    AOP这个概念看文档比较抽象,网上的举例子也基本上是操作日志这些,不过日志记录的确是一个非常形象的例子。说白了,当我们想对一些批量的方法进行一些补充操作,在service层和dao层,我们一般选择AOP。

    日志的例子,当我们dao层我们执行crud的时候同时想在数据库中生成一条日志记录,这时候日志的处理代码其实是一样的,我们可以提取出来。

    再比如,某些操作需要发短息通知用户(更改密码,账单到期等等),发短信的代码其实是一样的,就是一个手机号和短信内容。这时候我们用AOP就能解决,所有需要发短信的方法都可以用AOP来解决。好处是:只需要 一个地方维护代码,动态的决定哪些地方给用户发短信。

    一般用AOP的时候我们都是配合一些注解来完成的,注解的主要作用有两个方面:1.可以用来判断是否拦截该方法,2.传递一些aop方法中需要的参数或者变量值。

Glossory  of AOP:

    1.Pointcut: aop拦截的是哪些方法,必须要告诉aop方法,一般我们都是通过类似正则的表示,pointcut有不同的几种类型,通常我们见到的应该就是execution,其实目前我用的也就是execution,其他类型比如:within,this,args,target,@args,@target,@annotation...

      下面是pointcut的几个例子: pointcut的表达式可以通过‘||‘,‘&&‘和‘!‘连接

      @Pointcut("execution(pubic * *(..))")

      private void allOperation() {}

      @Pointcut("within(com.cnblog.service.User..") =>@Pointcut("execution(* com.cnblog.service.User.*(..))")

      private void userOperation() {}

      @Pointcut("allOperation() && userOperation() ")

      private void unionOperation() {}

2.Advice: Advice is associated with a pointcut expression, and runs before, after, or around method executions matched by the pointcut. The pointcut expression may be either a simple reference to a named pointcut, or a pointcut expression declared in place.

我们通常最简洁的用法就是@Around("execution(public *.update*(..))"),其实它等同于@Around(pointcut="execution(public *.update*(..))"),pointcut的值可以为上面我们定义的pointcut表达式的函数。下面主要介绍四种advice:

    Before:在拦截函数执行之前执行,一般数据权限或者用户验证的信息都是这样,类似Interceptor的prehandle。

     @Before("execution(public * * (..))")

      public void doCheck() {...}

    Around:around 是最通常用的,,因为可以before和after都融合,而且可以决定被拦截的方法什么时候执行,advice method的第一个参数必须是ProceedingJoinPoint.通过调用ProceedingJoinPoint的proceed方法就可以执行被拦截的函数。

      @Around("execution(public * * (..))")

      public Object doAround(ProceedingJoinPoint pjp ,..) {

          Object[] args = pjp.getArgs();

          ...

          return pjp.proceed(args);

      }

    After:在函数执行完之后执行,执行完包含被拦截函数返回值或者出现异常,一般情况我们是针对出现异常做一个记录等。

      @After("execution(public * * (..))")

      public void doFinal() {...}

  注:advice ordering:如果多个advices在同一个连接点执行,这时候就有一个执行顺序,一般情况下我们可以尽量避免,如果无法避免可以通过@Order(org.springframework.core.annotation.Order)来表示先后顺序。优先级越高,在"on  the way in"先执行,"on the way out"后执行.

4.Other keys: Introductions,perthis,PessimisticLockingFailureException(详情参照spring官方文档)

四.AOP的选择

    Spring AOP or AspectJ      ,Aspect language style ,@AspectJ or Spring xml style(aspect schema_based 这里面就不讲了,就是aop的xml配置)。

    1.Spring AOP  or full AspectJ

       spring aop 更简单,因为aspectj还需要引入compiler/weaver相关的jar 包。

       如果你的aop切入的是spring beans,可以直接使用spring  aop就可以了,如果你想拦截的对象不是spring容器管理的只能使用AspectJ,个人一直使用Aspectj,因                   为比较方便,如果用maven的话 就更简单了。

   2.个人建议直接使用@Aspect语法风格,使用spring xml配置很繁琐,而且容易遗漏。

五.AOP代理

  spring AOP使用JDK动态代理或者cglib来创建目标对象的代理,JDK动态代理会优先选择。

  默认情况下:如果被代理的目标对象至少实现了一个接口,那么这时候默认使用JDK动态代理,反之,则使用cglib。

  当然,你也可以强制使用cglib,如果你很偏爱它,不过在spring3.x你首先得提供cglib的相关jar包。然后需要在spring 中配置<aop:config proxy-target-class="true" />,to force CGLIB proxying when using @AspectJ autoproxy support ==> <aop:aspectj-autoproxy proxy-target-class="true" />

  理解AOP代理:

    一般object 调用:interface pojo  ,class SimplePojo implements pojo

public classMain {
    public  static voidmain(String[] args) {
      Pojo pojo = new SimplePojo();
    // this is a direct method call on the ‘pojo‘ reference
    pojo.foo();
  }
}

   我们稍微改变一下,当客户端只有一个代理的时候 ,如图: 

public classMain {
    public static voidmain(String[] args) {
        ProxyFactory factory = newProxyFactory(newSimplePojo());
        factory.addInterface(Pojo.class);
        factory.addAdvice(newRetryAdvice());
        Pojo pojo = (Pojo) factory.getProxy();
        // this is a method call on the proxy!
        pojo.foo();
    }
}

  

时间: 2024-08-27 18:12:17

Spring AOP With AspectJ的相关文章

比较分析 Spring AOP 和 AspectJ 之间的差别

面向方面的编程(AOP) 是一种编程范式,旨在通过允许横切关注点的分离,提高模块化.AOP提供方面来将跨越对象关注点模块化.虽然现在可以获得许多AOP框架,但在这里我们要区分的只有两个流行的框架:Spring AOP和AspectJ.这里将会帮助你基于一些关键信息,为你的项目选择正确的技术. Spring AOP不同于大多数其他AOP框架.Spring AOP的目的并不是为了提供最完整的AOP实现(虽然Spring AOP具有相当的能力):而是为了要帮助解决企业应用中的常见问题,提供一个AOP实

Spring AOP和AspectJ支持

学了Spring之后发现我都不知道java为何物-- 在这一章中有好几节,讲的切面编程 第一节:在项目中启用Spring的AspectJ注解支持 第二节:用AspectJ注解声明aspect 第三节:访问连接点信息 第四节:指定aspect的优先级 第五节:重用切入点定义 第六节:编写AspectJ切入点表达式 第七节:在Bean中引入行为 第八节:为Bean引入状态 第九节:用基于XML的配置声明aspect 如果JVM版本低于1.4时后者不想依赖于AspectJ,就不应该使用AspectJ来

Spring AOP框架 AspectJ

1 AspectJ简介 v  AspectJ是一个基于Java语言的AOP框架 v  Spring2.0以后新增了对AspectJ切点表达式支持 v  @AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面 v  新版本Spring框架,建议使用AspectJ方式来开发AOP v  主要用途:自定义开发 2 切入点表达式[掌握] execution() 用于描述方法 [掌握] 语法:execution(修饰符  返回值  包.类.方法名(参数) t

spring AOP 编程--AspectJ注解方式 (4)

1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) 的补充. AOP 的主要编程对象是切面(aspect), 而切面模块化横切关注点. 在应用 AOP 编程时, 仍然需要定义公共功能, 但可以明确的定义这个功能在哪里, 以什么方式应用, 并且不必修改受影响的类. 这样一来横切关注点就被模块化到特殊的对象(切面)里. AOP 的好处:

spring aop ---基于AspectJ

因为自己在引用jar包上吃过很多亏,也浪费过很多时间,所以每次都会把使用到的jar包都标明,谢谢理解! 引用jar包: 1.运行类: @Service public class Fit { public String say(){ System.out.println("册数使用的数据!!!!"); // throw new RuntimeException("失败!!!"); return "你好啊!!!"; } } 2.切面类:该类需@Com

关于 Spring AOP (AspectJ) 你该知晓的一切

[版权申明]未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/54629058 出自[zejian的博客] 关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上最近比较忙,所以这篇文件写得比较久,也分了不同的时间段在写,已尽最大能力去连贯博文中的内容

Spring AOP + AspectJ Annotation Example---reference

In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simple, Spring AOP + AspectJ allow you to intercept method easily. Common AspectJ annotations : @Before – Run before the method execution @After – Run aft

Spring源码阅读:使用标准AOP的API模拟Spring AOP + AspectJ的设计与实现

在上一篇博客中,提到了标准AOP与Spring AOP.这一篇就来把他们模拟出来. 在模拟之前,还需要提及的是,在Spring框架中,对于AOP的支持: Spring 支持的AOP AspectJ是另外一个有名的AOP框架,Spring也集成AspectJ,同时Spring AOP与AspectJ有一定程度的集成,这样一来Spring中就支持两种AOP:1)Spring AOP.2)AspectJ.而使用AOP的方式却又三种: 1)完全使用Spring AOP 2)完全使用AspectJ(分为注

关于 Spring AOP (AspectJ) 该知晓的一切

关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上最近比较忙,所以这篇文件写得比较久,也分了不同的时间段在写,已尽最大能力去连贯博文中的内容,尽力呈现出简单易懂的文字含义,如文中有错误请留言,谢谢. OOP的新生机 OOP新生机前夕 神一样的AspectJ-AOP的领跑者 AspectJ的织入方式及其原理概要 基于Aspect Spring AOP