`
yangzisai
  • 浏览: 85739 次
  • 性别: Icon_minigender_1
  • 来自: 东莞
社区版块
存档分类
最新评论

Spring AOP @AspectJ 入门实例

阅读更多
从Spring 2.0开始,可以使用基于schema及@AspectJ的方式来实现AOP,本文以一个简单的实例介绍了如何以@AspectJ方式在Spring中实现AOP。由于@Aspect是基于注解的,因此要求支持注解的5.0版本以上的JDK。

环境要求:
    1. Web应用
    2. 有一个专门提供系统服务的Service层

我们的目标是,如果用户调用Service层中任一方法,都在其插入一个记录信息的功能。

1. 一个最简单的AOP

     共有2步。

     1.1 定义一个Aspect

    1.  package com.sarkuya.aop.aspect;
    2.  import org.aspectj.lang.annotation.Aspect;
    3.  import org.aspectj.lang.annotation.Before;
    4.  @Aspect
    5.  public class SampleAspect {
    6.      @Before("execution(* com.sarkuya.service..*.*(..))")
    7.      public void doBeforeInServiceLayer() {
    8.          System.out.println("=====================================");
    9.          System.out.println("Aop: do before in Service layer");
    10.         System.out.println("=====================================");
    11.   }
    12. }

    第4行,必须使用@Aspect在类名之前注解。

    第6行,当用户调用com.sarkuya.service包中任一类的任一方法,在调用前,Spring将自动执行下面的doBeforeInServiceLayer()方法,此方法只是简单地打印一些信息。

     1.2 在Spring配置文件applicationContext.xml中配置

     <beans xmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

         <aop:aspectj-autoproxy />
        <bean class="com.sarkuya.aop.aspect.SampleAspect" />

         <!-- ================ YOUR CONTENTS GOES BELOW =================== -->
    </bean>

     就这么简单。

2. 将Pointcut及Advice分开

     上面的Aspect中混杂了Pointcut及Advice,因此最好将其分开。共有3步。

     2.1 定义Pointcut

     1.  package com.sarkuya.aop.aspect;
    2.  import org.aspectj.lang.annotation.Aspect;
    3.  import org.aspectj.lang.annotation.Pointcut;
    4.  @Aspect
    5.  public class SampleAspect {
    6.      @Pointcut("execution(* com.sarkuya.service..*.*(..))")
    7.      public void inServiceLayer() {
    8.      }
    9.  }

    Pointcut是植入Advice的触发条件。每个Pointcut的定义包括2部分,一是表达式,如第6行;二是方法签名,如第7行。方法签名必须是public及void型。可以将Pointcut中的方法看作是一个被Advice引用的助记符,因为表达式不直观,因此我们可以通过方法签名的方式为此表达式命名。因此Pointcut中的方法只需要方法签名,而不需要在方法体内编写实际代码。

     2.2 定义Advice

    1.  package com.sarkuya.aop.advice;
    2.  import org.aspectj.lang.annotation.Aspect;
    3.  import org.aspectj.lang.annotation.Before;
    4.  @Aspect
    5.  public class SampleAdvice {
    6.      @Before("com.sarkuya.aop.aspect.SampleAspect.inServiceLayer()")
    7.      public void logInfo() {
    8.          System.out.println("=====================================");
    9.          System.out.println("Aop: do before in service layer");
    10.         System.out.println("=====================================");
    11.     }
    12. }

     第4行,对于Advice,也只能使用@Aspect来注解。

    第6行,与第1.1节中第6行不同,这次不是直接使用Pointcut的表达式,而是使用了Pointcut中的方法签名。

    单独定义Pointcut的好处是,一是通过使用有意义的方法名,而不是难读的Pointcut表达式,使代码更加直观;二是Pointcut可以实现共享,被多个Advice直接调用。若有多个Advice调用某个Pointcut,而这个Pointcut的表达式在将来有改变时,只需修改一个地方,维护更加方便。

    第7行,我们将Advice的方法法改为logInfo(),以更加明确此Advice的作用。

     2.3 配置文件

    <aop:aspectj-autoproxy />
    <bean class="com.sarkuya.aop.advice.SampleAdvice" />

     只需配置SampleAdvice,无需配置SampleAspect。

3. 重构:明确Pointcut职责

     对于SampleAspect来说,其主要职责是定义Pointcut,可以在此类中同时定义多个Pointcuts。但其类名反映不出这个特点,因此,应将其重构以明确其职责。

    package com.sarkuya.aop.pointcut;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    @Aspect
    public class PointcutsDefinition {
        @Pointcut("execution(* com.sarkuya.service..*.*(..))")
        public void inServiceLayer() {
        }
    }

     将SampleAspect重命名为PointcutsDefinition,并移到com.sarkuya.aop.pointcut包中。

     对于SampleAdvice来说,只需改变@Before()的注解,指向
    @Before("com.sarkuya.aop.pointcut.PointcutsDefinition.inServiceLayer()")

     而Spring配置文件保持不变。

小结:
    我们先从一个最简单的Aspect实例开始,了解AOP的作用及最基本的要求,再重构为更有意义的代码,明确了AOP中的Pointcut及Advice的概念,有助于我们构建更复杂的Aspect。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cnham/archive/2009/03/11/3980993.aspx
分享到:
评论

相关推荐

    spring AOP 实例(@AspectJ)

    一个基于@AspectJ的spring2.0 AOP应用实例,很小很简单,没有任何额外信息,最适合AOP入门学习。使用log4j打印信息。把项目直接import进myeclipse就可以使用啦......

    Spring的AOP实例(XML+@AspectJ双版本解析+源码+类库)

    NULL 博文链接:https://moshow.iteye.com/blog/1613947

    Spring 使用AspectJ 实现 AOP之前置通知小例子

    Spring 使用AspectJ 实现 AOP之前置通知小例子,实际跑过,验证可信。

    spring的aop切面编程实例

    实现spring的aop的操作,采用AspectJ技术,通过xml的配置来实现,本人亲自测试过,aop相关架包已引入

    Spring AOP 基于注解详解及实例代码

    Spring AOP 基于注解详解及实例代码 1.启用spring对@AspectJ注解的支持: &lt;beans xmlns:aop=http://www.springframework.org/schema/aop...&gt; &lt;!--启动支持--&gt; &lt;aop&gt; 也可以配置...

    spring-aop实例demo

    spring3.2.8使用aop的简单实例

    详解Spring Aop实例之AspectJ注解配置

    本篇文章主要介绍了详解Spring Aop实例之AspectJ注解配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    跟我学spring3(1-7).pdf

    —— 5.1 概述 5.2 SpEL基础5.3 SpEL语法5.4在Bean定义中使用EL6.1 AOP基础6.2 AOP的HelloWorld6.3 基于Schema的AOP6.4 基于@AspectJ的AOP 6.5 AspectJ切入点语法详解6.6 通知参数6.7 通知顺序6.8 切面实例化模型

    Spring AOP 动态代理(二)

    Spring AOP 的本质其实就是实现了动态代理,这个和Aspectj不一样,AspectJ 是直接修改你的字节码 完成的 代理模式其实很简单 主要包括 接口+真实的实现类+代理类 在真实类的实例化的时候 使用代理类,所以需要 ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1....

    SpringAOP切面实例讲解及应用场景(通俗易懂)

    AOP切面的实践 下面是一个在spring mvc中关于切面如何使用的例子,可以指直观的理解切面到底有什么作用 1、引用 AspectJ jar 包依赖 pom.xml 文件添加依赖 org.aspectj aspectjrt 1.9.2 2、创建两个新的包 ...

    SpringAOP使用介绍,从前世到今生

    SpringAOP发展到现在出现的全部3种配置方式。由于Spring强大的向后兼容性,实际代码中往往会出现很多配置混杂的情况,而且居然还能工作,本文希望帮助大家理清楚这些知识。我们先来把它们的概念和关系说说清楚。AOP...

    Spring4—AOP编程

    aop写一个简单的计算器:(1)日志功能:在程序执行期间追踪正在发生的活动(打印出调用的方法,以及参数的参数值);...利用简单的实例来理解spring的aop编程,以aspectJ为例,理解环绕通知的真正含义。

    跟我学spring3(1-7)

    【第六章】 AOP 之 6.8 切面实例化模型 ——跟我学spring3 【第六章】 AOP 之 6.9 代理机制 ——跟我学spring3 【第七章】 对JDBC的支持 之 7.1 概述 ——跟我学spring3 【第七章】 对JDBC的支持 之 7.2 ...

    spring2.5学习PPT 传智博客

    使用Spring的注解方式实现AOP入门 18.使用Spring的注解方式实现AOP的细节 19.使用Spring配置文件实现AOP 20.aspectj的切入点语法定义细节 21.搭建和配置Spring与jdbc整合的环境 22.Spring集成的jdbc编码和事务...

Global site tag (gtag.js) - Google Analytics