zoukankan      html  css  js  c++  java
  • Spring3.0中的AOP配置方法

    http://zywang.iteye.com/blog/974226

    http://www.cnblogs.com/garinzhang/p/java_spring_aop_aspect.html

    第一种配置方法:使用@AspectJ标签

    1. 在配置文件中添加<aop:aspectj-autoproxy/>注解
    2. 创建一个Java文件,使用@Aspect注解修饰该类
    3. 创建一个方法,使用@Before、@After、@Around等进行修饰,在注解中写上切入点的表达式

    说明:上述Java文件创建好后,需要将其在Spring的容器中进行声明,可以在配置文件中定义<bean/>节点,也可以使用@Component组件进行修饰

    示例:

     1 import org.aspectj.lang.ProceedingJoinPoint;
     2 import org.aspectj.lang.annotation.After;
     3 import org.aspectj.lang.annotation.AfterThrowing;
     4 import org.aspectj.lang.annotation.Around;
     5 import org.aspectj.lang.annotation.Aspect;
     6 import org.aspectj.lang.annotation.Before;
     7 import org.springframework.stereotype.Component;
     8 
     9 /**
    10  * 基于注解的AOP日志示例
    11  * @author ZYWANG 2011-3-24
    12  */
    13 @Component
    14 @Aspect
    15 public class AopLog {
    16     
    17     //方法执行前调用
    18     @Before("execution (* com.zywang.services.impl.*.*(..))")
    19     public void before() {
    20         System.out.println("before");
    21     }
    22     
    23     //方法执行后调用
    24     @After("execution (* com.zywang.services.impl.*.*(..))")
    25     public void after() {
    26         System.out.println("after");
    27     }
    28     
    29     //方法执行的前后调用
    30     @Around("execution (* com.zywang.services.impl.*.*(..))")
    31     public Object around(ProceedingJoinPoint point) throws Throwable{
    32         System.out.println("begin around");
    33         Object object = point.proceed();
    34         System.out.println("end around");
    35         return object;
    36     }
    37     
    38     //方法运行出现异常时调用
    39     @AfterThrowing(pointcut = "execution (* com.zywang.services.impl.*.*(..))",throwing = "ex")
    40     public void afterThrowing(Exception ex){
    41         System.out.println("afterThrowing");
    42         System.out.println(ex);
    43     }
    44 }

    上面这段代码中多次使用了重复的切入点,这种情况下,可以使用@Pointcut标注,来修改一个切入点方法(这个方法不需要参数和方法体),然后就可以在@Before等标注中引用该方法作为切入点,示例如下:

     1 import org.aspectj.lang.ProceedingJoinPoint;
     2 import org.aspectj.lang.annotation.Around;
     3 import org.aspectj.lang.annotation.Aspect;
     4 import org.aspectj.lang.annotation.Before;
     5 import org.aspectj.lang.annotation.Pointcut;
     6 import org.springframework.stereotype.Component;
     7 
     8 /**
     9  * 基于注解的AOP日志示例
    10  * @author ZYWANG 2011-3-24
    11  */
    12 @Component
    13 @Aspect
    14 public class AopLog {
    15     
    16     @Pointcut("execution (* com.iflysse.school.services.impl.*.*(..))")
    17     public void pointcut(){}
    18     
    19     //方法执行前调用
    20     @Before("pointcut()")
    21     public void before() {
    22         System.out.println("before");
    23     }
    24     
    25     //方法执行的前后调用
    26     @Around("pointcut()")
    27     public Object around(ProceedingJoinPoint point) throws Throwable{
    28         System.out.println("begin around");
    29         Object object = point.proceed();
    30         System.out.println("end around");
    31         return object;
    32     }
    33 }

    第二种配置方法:基于配置文件的配置

    1. 创建一个Java文件,并指定一个用于执行拦截的方法,该方法可以有0个或多个参数
    2. 在Spring配置文件中注册该Java类为一个Bean
    3. 使用<aop:config/>、<aop:aspect/>等标签进行配置

    示例:

    Java文件

     1 import org.aspectj.lang.ProceedingJoinPoint;
     2 
     3 /**
     4  * 基于配置文件的AOP日志示例
     5  * @author ZYWANG 2011-3-24
     6  */
     7 public class AopLog {
     8     
     9     //方法执行的前后调用
    10     public Object runOnAround(ProceedingJoinPoint point) throws Throwable{
    11         System.out.println("begin around");
    12         Object object = point.proceed();
    13         System.out.println("end around");
    14         return object;
    15     }
    16     
    17 }
    Spring配置文件
    1     <bean id="aopLog" class="com.iflysse.school.aop.AopLog"></bean>
    2     
    3     <aop:config>
    4         <aop:aspect ref="aopLog">
    5             <aop:around method="runOnAround" pointcut="execution (* com.zywang.services.impl.*.*(..))"/>
    6         </aop:aspect>
    7     </aop:config>
     
     注意:上面这个示例使用的是around方式的拦截,该方法要求Java类中的方法有一个ProceedingJoinPoint类型的参数

    使用第二种方式的AOP配置,在Eclipse(有SpringIDE插件)中被拦截到的方法中有标识显示

    以上配置基于Spring 3.0.5 进行设置,参考其《Reference Documentation》

  • 相关阅读:
    C语言ll作业01
    C语言寒假大作战04
    C语言寒假大作战03
    C语言寒假大作战02
    C语言寒假大作战01
    C语言I作业12—学期总结
    C语言I博客作业11
    C语言I博客作业10
    第三章预习笔记-运算方法和运算部件
    非数值数据的编码表示
  • 原文地址:https://www.cnblogs.com/wxdlut/p/5688152.html
Copyright © 2011-2022 走看看