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代理

 

 

。。。。。。笔记待续

转载于:https://www.cnblogs.com/willbesuccess/p/3460058.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值