天天看點

什麼是AOP?

  在運作時,動态的将代碼切入到類的指定方法、指定位置上的程式設計思想就是面向切面的程式設計。AOP即面向切面程式設計。使用切面程式設計,可以将一些系統性的代碼提取出來,獨立實作,與核心業務代碼剝離,比如權限管理、事務管理、日志記錄等等。AOP是spring提供的關鍵特性之一。

AOP的實作原理

  AOP分為靜态AOP和動态AOP,靜态AOP是由AspectJ實作的AOP,動态AOP是指将切面代碼進行動态織入實作的AOP,Spring的AOP為動态AOP。實作的技術為:JDK提供的動态代理技術和CGLIB(動态位元組碼增強技術)兩種。盡管實作技術不一樣,但 都是基于代理模式 , 都是生成一個代理對象 。

  JDK動态代理實作:檢視我前面的博文-動态代理,我們看到該方式有一個要求, 被代理的對象必須實作接口,而且隻有接口中的方法才能被代理 。也就是jdk代理方式是基于接口的一種代理方式。

  CGLIB:位元組碼生成技術實作AOP,其實就是繼承被代理對象,然後Override需要被代理的方法,在覆寫該方法時,自然是可以插入我們自己的代碼的。

因為需要Override被代理對象的方法,是以自然CGLIB技術實作AOP時,就 必須要求需要被代理的方法不能是final方法,因為final方法不能被子類覆寫 。

我們使用CGLIB實作上面的例子:

package net.aazj.aop;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class CGProxy implements MethodInterceptor{
    private Object target;    // 被代理對象
    public CGProxy(Object target){
        this.target = target;
    }
    public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy proxy) throws Throwable {
        System.out.println("do sth before....");
        Object result = proxy.invokeSuper(arg0, arg2);
        System.out.println("do sth after....");
        return result;
    }
    public Object getProxyObject() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.target.getClass());    // 設定父類
        // 設定回調
        enhancer.setCallback(this);    // 在調用父類方法時,回調 this.intercept()
        // 建立代理對象
        return enhancer.create();
    }
}      
public class CGProxyTest {
    public static void main(String[] args){
        Object proxyedObject = new UserServiceImpl();    // 被代理的對象
        CGProxy cgProxy = new CGProxy(proxyedObject);
        UserService proxyObject = (UserService) cgProxy.getProxyObject();
        proxyObject.getUser(1);
        proxyObject.addUser(new User());
    }
}      

觀察spring中AOP相關源碼可知,如果被代理對象實作了接口,那麼就使用JDK的動态代理技術,反之則使用CGLIB來實作AOP,是以 Spring預設是使用JDK的動态代理技術實作AOP的 。

AOP在spring中的使用

Spring中AOP的配置一般有兩種方法,一種是使用 <aop:config> 标簽在xml中進行配置,一種是使用注解以及@Aspect風格的配置。

1) 基于<aop:config>的AOP配置

<tx:advice id="transactionAdvice" transaction-manager="transactionManager"?>
    <tx:attributes >
        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="append*" propagation="REQUIRED" />
        <tx:method name="insert*" propagation="REQUIRED" />
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="get*" propagation="SUPPORTS" />
        <tx:method name="find*" propagation="SUPPORTS" />
        <tx:method name="load*" propagation="SUPPORTS" />
        <tx:method name="search*" propagation="SUPPORTS" />
        <tx:method name="*" propagation="SUPPORTS" />
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="transactionPointcut" expression="execution(* net.aazj.service..*Impl.*(..))" />
    <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>      
<bean id="aspectBean" class="net.aazj.aop.DataSourceInterceptor"/>
<aop:config>
    <aop:aspect id="dataSourceAspect" ref="aspectBean">
        <aop:pointcut id="dataSourcePoint" expression="execution(public * net.aazj.service..*.getUser(..))" />
        <aop:pointcut expression="" id=""/>
        <aop:before method="before" pointcut-ref="dataSourcePoint"/>
        <aop:after method=""/>
        <aop:around method=""/>
    </aop:aspect>
    <aop:aspect></aop:aspect>
</aop:config>      

<aop:aspect> 配置一個切面;

<aop:pointcut>配置一個切點,基于切點表達式;

<aop:before>,<aop:after>,<aop:around>是定義不同類型的advise. a