`
duanfei
  • 浏览: 720080 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Spring AOP 的@Aspect (转)

    博客分类:
  • J2EE
 
阅读更多

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

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

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

 1. 一个最简单的AOP

     共有2步。

     1.1 定义一个Aspect

   

 package com.sarkuya.aop.aspect;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    @Aspect
    public class SampleAspect {
        @Before("execution(* com.sarkuya.service..*.*(..))")
        public void doBeforeInServiceLayer() {
           System.out.println("=====================================");
           System.out.println("Aop: do before in Service layer");
           System.out.println("=====================================");
        }
     }

 

    第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

     

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

 

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

     2.2 定义Advice

    

package com.sarkuya.aop.advice;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    @Aspect
    public class SampleAdvice {
      @Before("com.sarkuya.aop.aspect.SampleAspect.inServiceLayer()")
      public void logInfo() {
         System.out.println("=====================================");
         System.out.println("Aop: do before in service layer");
         System.out.println("=====================================");
       }
    }

 

     第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。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics