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>

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

      

  • 相关阅读:
    用python3实现AES/CBC/PKCS5padding算法加解密
    python之逆向某贷款app破解sign参数
    用Python实现RSA签名和验签
    python3 RSA 长字符串分段加密解密
    PyCharm 字体大小颜色常用功能设置
    pycharm2019.3/pycharm2020.2 专业版 安装教程永久激活
    Android 四大组件和Intent
    Linux的查找命令
    Linux(centos)系统各个目录的作用详解
    linux ls文件颜色和底色设置
  • 原文地址:https://www.cnblogs.com/wl0000-03/p/6635895.html
Copyright © 2011-2022 走看看