zoukankan      html  css  js  c++  java
  • 面向切面编程AOP:基于XML文件的配置

    除了使用AspectJ注解声明切面,Spring也支持在bean的配置文件中声明切面,这种声明是通过aop scheme中的XML元素完成的。

    首先建立一个类:

    package com.sevenhu.AOPTests;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Before;
    
    import java.util.Arrays;
    
    /**
     * Created by hu on 2016/4/1.
     */
    public class AspectDemo1 {
        //前置通知
        @Before("execution(public int com.sevenhu.AOPTests.Calculator.*(int ,int ))")
        public void beforeMethod(JoinPoint joinPoint){
            String methodName=joinPoint.getSignature().getName();
            Object[] args=joinPoint.getArgs();
            System.out.println("The method "+methodName+" begins with "+ Arrays.asList(args));
        }
    }
    

    配置如下:

           <!--使AspesctJ的注解起作用-->
           <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
           <bean id="aspectDemo1" class="com.sevenhu.AOPTests.AspectDemo1"></bean>
           <aop:config>
                  <aop:aspect id="aspect1" ref="aspectDemo1"></aop:aspect>
           </aop:config>
    

    基于XML声明切入点:

           <!--使AspesctJ的注解起作用-->
           <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
           <bean id="aspectDemo1" class="com.sevenhu.AOPTests.AspectDemo1"></bean>
           <aop:config>
                  <aop:pointcut id="cutExpression" expression="execution(* *.*(..))"/>
                  <aop:aspect id="aspect1" ref="aspectDemo1"></aop:aspect>
           </aop:config>
    

    声明通知的示例代码:

           <!--加入自动扫描的包-->
           <context:component-scan base-package="com.sevenhu.AOPTests"></context:component-scan>
           <!--使AspesctJ的注解起作用-->
           <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
           <bean id="aspectDemo1" class="com.sevenhu.AOPTests.AspectDemo1"></bean>
           <aop:config>
                  <aop:pointcut id="cutExpression" expression="execution(* *.*(..))"/>
                  <aop:aspect id="aspect1" ref="aspectDemo1">
                         <aop:before method="beforeMethod" pointcut-ref="cutExpression"></aop:before>
                  </aop:aspect>
           </aop:config>
    

      

  • 相关阅读:
    RAID卡 BBU Learn Cycle周期的影响
    Linux下查看Raid磁盘阵列信息的方法
    ROS导航包的介绍
    ROS源码解读(二)--全局路径规划
    ROS源码解读(一)--局部路径规划
    VS运行release版本正常,直接执行exe文件会出现问题
    IFM设备 Linux方面资料
    Map-making Robots: A Review of the Occupancy Grid Map Algorithm
    Eigen 介绍及简单使用
    绘制二维障碍栅格地图
  • 原文地址:https://www.cnblogs.com/hujingwei/p/5344647.html
Copyright © 2011-2022 走看看