zoukankan      html  css  js  c++  java
  • Spring AOP 笔记

    AOP 面向切面思想。用来分离程序功能,比如讲日志、异常等与功能程序分开,减少代码程序的耦合度。  AOP有3个关键概念、Pointcut切入点、Advice通知、Advisor配置器。

    Join Point(连接点)指的是程序运行中的某个阶段点,如某个方法调用、异常抛出。Pointcut是Join Point的集合,它是程序中需要注入Advice的位置的集合,指明Advice要在什么样的条件下才能被触发。

    Advice是某个连接点所采用的处理逻辑,

    Advisor是Pointcut和Advice的配置器,

    切入点Pointcut有三种状况:

    1、静态切入点:只限于给定的方法和目标类,而不考虑方法的参数。Spring在调用静态切入点时只在第一次的时候计算静态切入点的位置,然后把它缓存起来,以后就不需要再次进行计算。

        <!-- AOP -->
        <!-- 静态切入点 -->
        <!-- org.springframework.aop.support.RegexpMethodPointcutAdvisor可以实现静态切入点 -->
        <bean id="settersAndAbsquatulatePointcut" 
                class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
                <property name="patterns">
                    <list>
                    <!-- 表示所有以save开头的方法都是切入点 -->
                        <value>.*save.*</value>
                        <!-- 表示所有以do开头的方法都是切入点 -->
                        <value>.*do.*</value>
                    </list>
                </property>
        </bean>

    2、动态切入点与静态切入点的区别就是不仅限定给予点的方法和类,动态切入点还可以指定方法的参数,因为参数的变化性,所以动态切入点不能缓存,需要每次调用的时候都进行计算,因此使用动态切入点有很大的性能消耗。(很少用)

    3、自定义切入点

    Spring的通知Advice有五种类型:

    1、Interception Around 在JointPoint(连接点)前后调用

    实现Interception Around 通知的类需要实现接口

    org.aopalliance.intercept.MethodInterceptor
    package spring.init.aop.daoimpl;
    
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    
    /**
     * @author fanbo
     * ADvice。。Spring的通知
     * 实现Interception Around通知的类需要实现MethodInterceptor接口
     *
     */
    public class LogMethodInterceptor implements MethodInterceptor{
    
        public Object invoke(MethodInvocation mi) throws Throwable {
            System.out.println("开始记录日志");
            Object val = mi.proceed();
            System.out.println("结束日志的记录");
            return val;
        }
    
    }

    2、Before 在JointPoint前调用

    package spring.init.aop.daoimpl;
    
    import java.lang.reflect.Method;
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    /**
     * @author fanbo
     * Spring 的通知Advice的第二中类型  Befor 在JointPoint调用之前使用
     *实现 org.springframework.aop.MethodBeforeAdvice;接口
     */
    public class LogMethodBeforeAdvice implements MethodBeforeAdvice{
    
        public void before(Method method, Object[] obj1, Object obj2)
                throws Throwable {
            System.out.println("开始记录日志");
        }
    
    }

    3、After Returning 在JointPoint后调用

    package spring.init.aop.daoimpl;
    
    import java.lang.reflect.Method;
    
    import org.springframework.aop.AfterReturningAdvice;
    
    /**
     * @author fanbo
     *Spring 的通知Advice的第三种类型  Befor 在JointPoint调用之前使用
     *实现 org.springframework.aop.AfterReturningAdvice接口
     */
    public class LogAfterReturningAdvice implements AfterReturningAdvice{
    
        public void afterReturning(Object arg0, Method arg1, Object[] arg2,
                Object arg3) throws Throwable {
            System.out.println("记录日志结束");
        }
    
    }

    4、Throw 在JointPoint抛出异常时调用

    package spring.init.aop.daoimpl;
    
    import java.rmi.RemoteException;
    
    import org.springframework.aop.ThrowsAdvice;
    
    /**
     ** @author fanbo
     * ADvice。。Spring的通知
     * Throw类型是Advice的第四种类型。是在JointPoint连接点抛出异常时调用
     * 实现Throw通知的类需要实现org.springframework.aop.ThrowsAdvice接口
     *
     */
    public class LogThrowAdvice implements ThrowsAdvice{
    
        public void afterThrowing(RemoteException exception) throws Throwable{
            System.out.println("记录日志异常,请检查"+exception.toString());
        }
    }

    5、Introduction 在JointPoint调用完毕之后调用

    package spring.init.aop.daoimpl;
    
    
    import org.aopalliance.aop.Advice;
    import org.aopalliance.intercept.MethodInvocation;
    import org.springframework.aop.ClassFilter;
    import org.springframework.aop.IntroductionAdvisor;
    import org.springframework.aop.IntroductionInterceptor;
    
    /**
     ** @author fanbo
     * ADvice。。Spring的通知
     * Introduction类型是Advice的第五种类型。是在JointPoint调用之后使用
     * 实现Introduction通知的类需要实现org.springframework.aop.IntroductionInterceptor;接口和
     * org.springframework.aop.IntroductionAdvisor接口
     *
     */
    public class LogIntroductionAdvice implements IntroductionAdvisor,IntroductionInterceptor{
    
        public Object invoke(MethodInvocation arg0) throws Throwable {
            // TODO Auto-generated method stub
            return null;
        }
    
        public boolean implementsInterface(Class arg0) {
            // TODO Auto-generated method stub
            return false;
        }
    
        public ClassFilter getClassFilter() {
            // TODO Auto-generated method stub
            return null;
        }
    
        public void validateInterfaces() throws IllegalArgumentException {
            // TODO Auto-generated method stub
            
        }
    
        public Advice getAdvice() {
            // TODO Auto-generated method stub
            return null;
        }
    
        public boolean isPerInstance() {
            // TODO Auto-generated method stub
            return false;
        }
    
        public Class[] getInterfaces() {
            // TODO Auto-generated method stub
            return null;
        }
    
        
    }

    Spring 的Advisor是Pointcut和Advice的配置器。它是将Advice注入程序中Pointcut位置的代码,在Spring中主要通过XML来配置Pointcut和Advice

    用ProxyFactoryBean创建AOP代理

    。。。。。。笔记待续

    此笔记用来自我学习和分享知识,有不对的地方还请大家互相指教
  • 相关阅读:
    Delphi实战中讲解FormCreate,FormShow,FormActivate
    delphi Try except on e:Exception do
    Delphi处理数据网格DBGrid的编辑框 获取还没有提交到数据集的字段文本
    delphi dbgrid中如何自动生成序号
    DBDateTimePicker;
    Delphi控件开发浅入深出(八)
    delphi中日期类型TDateTime使用总结
    在DBGrid录数据时,如何判断光标位置是在数据的最左或最右,如果是最左或最右则在按左右光标键时光标跳到上一格或下一格,如果是在数据中
    请问如何按Enter键让DBGrid的光标向右移以及换行?(0分)
    tdbgrid中用enter仿真tab键盘_delphi教程
  • 原文地址:https://www.cnblogs.com/willbesuccess/p/3460058.html
Copyright © 2011-2022 走看看