zoukankan      html  css  js  c++  java
  • Spring AOP

    AOP概念

    AOP(Aspect Oriented Programming),即面向切面编程(也叫面向方面编程,面向方法编程)。其主要作用是,在不修改源代码的情况下给某个或者一组操作添加额外的功能。像日志记录,事务处理,权限控制等功能,都可以用AOP来“优雅”地实现,使这些额外功能和真正的业务逻辑分离开来,软件的结构将更加清晰

    MyAspect.java

    //这里必须要抛异常
    public Object around( ProceedingJoinPoint proceedingJoinPoint) throws Throwable
    {
        System.out.print("环绕通知 方法前执行");
        Object result=proceedingJoinPoint.proceed();
        System.out.print("环绕通知 方法后执行");
        return result;   
    }

    ApplicationContext.xml

    <bean id="OrderService" class="cn.itcast.spring.c_aop.impl.OrderServiceimpl" />
    
    <!-- AspectJ AOP -->
    <!-- 配置目标 -->
    <bean id="CustomerService" class="cn.itcast.spring.d_aspectj.CustomerService"></bean>
     <!-- 配置切面类 -->
    <bean id="MyAspect" class="cn.itcast.spring.d_aspectj.MyAspect"></bean>
      <aop:config>
         <!-- ref引用切面类 -->
         <aop:aspect ref="MyAspect">
             <aop:pointcut expression="execution(* cn.itcast.spring.d_aspectj.CustomerService.*(..))" id="mypointcut2"/>
              <aop:around method="around" pointcut-ref="mypointcut2"/>
          </aop:aspect>
      </aop:config>
    </beans>
    @Test
    public void testaround()
    {
         customerService.delete(); 
    }

    输出结果

    环绕通知 方法前执行
    this is delete
    环绕通知 方法后执行
    不积跬步,无以至千里;不积小流,无以成江海。
  • 相关阅读:
    Array.prototype.slice.call()
    闭包与变量
    XML处理指令
    XSLT学习(九)通过JavaScript转化xml
    chrome浏览器canvas画图不显示
    B.储物点的距离
    A.约数个数的和
    F.求最大值
    STVD+COSMIC编译工程时出现Error creating process for executable mapinfo
    STVD+COSMIC编译工程时can't open file crtsi0.sm8
  • 原文地址:https://www.cnblogs.com/lovedaodao/p/10082619.html
Copyright © 2011-2022 走看看