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
    环绕通知 方法后执行
    不积跬步,无以至千里;不积小流,无以成江海。
  • 相关阅读:
    hdu 4027 Can you answer these queries? 线段树
    ZOJ1610 Count the Colors 线段树
    poj 2528 Mayor's posters 离散化 线段树
    hdu 1599 find the mincost route floyd求最小环
    POJ 2686 Traveling by Stagecoach 状压DP
    POJ 1990 MooFest 树状数组
    POJ 2955 Brackets 区间DP
    lightoj 1422 Halloween Costumes 区间DP
    模板 有源汇上下界最小流 loj117
    模板 有源汇上下界最大流 loj116
  • 原文地址:https://www.cnblogs.com/lovedaodao/p/10082619.html
Copyright © 2011-2022 走看看