zoukankan      html  css  js  c++  java
  • Spring 中面向AOP之一系列做法

    Spring的AOP实现是通过集成AspectJ框架实现的。

    想必这句话大家都知道了吧,不知道也没关系,最起码你现在知道了。

    四种实现方案,接下来我们一一去揭开它的神秘的面纱.......

    第一种(伪代码的形式)

    经典AOP形式的:

    <bean id="userDao" class="UserDaoImpl"></bean>

    <bean id="service" class="UserServiceImpl">

       <property name="dao" ref="userDao"></property>

    </bean>

    <bean id="myBefore" class="MyBeforeAdvice"></bean>

    <bean id="myAfter" class="MyAfterAdvice"></bean>

    <aop:config>

       <aop:pointcut id="point" expression="execution(* *..biz.*.*(..))"></aop:pointcut>

       <aop:advisor pointcut-ref="point" advice-ref="myBefore"></aop:advisor>

       <aop:advisor pointcut-ref="point" advice-ref="myAfter"></aop:advisor>

    </aop:config>

    第二种(伪代码形式)

    使用注解方式

    @Aspect

    public class MyBeforeAspect{

             @Before(value="execution(* *..biz.*.*(..))")

             public void before(){

                    System.out.println("====before===");

             }

           @AfterReturning(value="execution(* *..biz.*.*(..))")

            public void after(){

                   System.out.println("=====after=====");

            }

    }

    <bean id="stuDao" class="StudentDaoImpl"></bean>

    <bean id="stuService" class="StudentService">

         <property name="dao" ref="stuDao">

    </bean>

    <!-- 准备aop:aspectj-autoproxy-->

    <bean class="MyBeforeAspect"></bean>

    <aop:aspectj-autoproxy />

    第三种(伪代码形式)

    使用AspectJ基于xml形式

     public class MyAspectJMethod{

            public void myBefore(){

                 System.out.println("AspectJ=========before");

           }

           public void myAfter(){

                 System.out.println("AspectJ==========after");

            }

    }

    <bean id="stuDao" class="StudentDaoImpl"></bean>

    <bean id="stuBiz" class="StudentBizImpl"></bean>

    <bean id="myAspectJ" class="MyAspectJMethod">

    <aop:config>

        <aop:pointcut id="point" expression="execution(* *..biz.*.*(..))"></aop:pointcut>

       <aop:aspect ref="myAspectJ">

               <aop:before method="myBefore" ponitcut-ref="point"></aop:before>

              <aop:after method="myAfter" pointcut-ref="point"></aop:after>

        </aop:aspect>

    </aop:config>

    上面配置<aop:pointcut>节点也可以放到<aop:aspect>节点内,如下:

    <aop:config>

       <aop:aspect ref="myAspectJ">

               <aop:pointcut id="point" expression="execution(* *..biz.*.*(..))"></aop:pointcut>

               <aop:before method="myBefore" ponitcut-ref="point"></aop:before>

              <aop:after method="myAfter" pointcut-ref="point"></aop:after>

        </aop:aspect>

    </aop:config>

    上述两种配置都可以实现该功能。。。。。

      

  • 相关阅读:
    【JS】415- JS面向对象之 7 大基本原则
    【每周小回顾】14- 一起回顾上周精彩内容
    【全栈修炼】414- CORS和CSRF修炼宝典
    【全栈修炼】CORS和CSRF修炼宝典
    【JS】413- JavaScript中的位运算和权限设计
    【面试题】412- 35 道必须清楚的 React 面试题
    【CSS】410- 关于CSS盒子模型、BFC及其应用
    15.app后端怎么设计用户登录方案
    14.app后端如何设计api
    13.app后端为什么要用到消息队列
  • 原文地址:https://www.cnblogs.com/wl0000-03/p/6635895.html
Copyright © 2011-2022 走看看