在運行時,動態的將代碼切入到類的指定方法、指定位置上的編程思想就是面向切面的編程。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
spectBean 是切面的處理bean
2) 基於注解和@Aspect風格的AOP配置