Spring AOP的幾種實現(xiàn)方式總結(jié)
AOP核心概念
1、橫切關(guān)注點
對哪些方法進(jìn)行攔截,攔截后怎么處理,這些關(guān)注點稱之為橫切關(guān)注點
2、切面(aspect)
類是對物體特征的抽象,切面就是對橫切關(guān)注點的抽象
3、連接點(joinpoint)
被攔截到的點,因為spring只支持方法類型的連接點,所以在Spring中連接點指的就是被攔截到的方法,實際上連接點還可以是字段或者構(gòu)造器
4、切入點(pointcut)
對連接點進(jìn)行攔截的定義
5、通知(advice)
所謂通知指的就是指攔截到連接點之后要執(zhí)行的代碼,通知分為前置、后置、異常、最終、環(huán)繞通知五類
6、目標(biāo)對象
代理的目標(biāo)對象
7、織入(weave)
將切面應(yīng)用到目標(biāo)對象并導(dǎo)致代理對象創(chuàng)建的過程
8、引入(introduction)
在不修改代碼的前提下,引入可以在運行期為類動態(tài)地添加一些方法或字段
Spring 實現(xiàn)AOP所需要的包:
1、Spring提供的jar包
2、aopalliance.jar
3、aspectjweaver.jar
Spring 實現(xiàn)AOP的方式:
1、Java動態(tài)代理
該方法針對接口的實例創(chuàng)建代理
applicationContext.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <bean id="concreteImplementor" class="com.marving.aop.ConcreteImplementor" /> <bean id="interceptorHandler" class="com.marving.aop.InterceptorHandler" /> <aop:config> <aop:aspect id="interceptor" ref="interceptorHandler"> <aop:pointcut id="addAllMethod" expression="execution(* com.marving.aop.Abstration.*(..))" /> <aop:before method="doSomething" pointcut-ref="addAllMethod" /> <aop:after method="doSomething" pointcut-ref="addAllMethod" /> </aop:aspect> </aop:config> </beans>
其中Abstration為接口,ConcreteImplementor為實現(xiàn)類,InterceptorHandler為代理攔截類。
public interface <span style="font-size:12px;">Abstration</span> { public void operation() }
//具體實現(xiàn)化角色 public class ConcreteImplementor implements Implementor{ @Override public void operation() { System.out.println("ConcreteImplementor"); } }
public class InterceptorHandler{ public void printTime(){ System.out.println("CurrentTime = " + System.currentTimeMillis()); } }
2、CGLIB生成代理
CGLIB針對代理對象為類的情況使用。
通過實現(xiàn)MethodInterceptor接口,并實現(xiàn) public Object intercept(Object obj, Method m, Object[] args,MethodProxy proxy) throws Throwable方法生成代理。
3、BeanNameAutoProxyCreator實現(xiàn)AOP
Spring為我們提供了自動代理機(jī)制,讓容器為我們自動生成代理,把我們從煩瑣的配置工作中解放出來,在內(nèi)部,Spring 使用BeanPostProcessor自動地完成這項工作。
具體配置如下:
<bean id="MyInterceptor" class="com.yesjpt.interceptor. MyInterceptor"></bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Service</value> </list> </property> <property name="interceptorNames"> <list> <value>MyInterceptor</value> </list> </property> </bean>
其中*Service 為需要攔截代理的bean,以Service結(jié)尾的都 被攔截,并使用MyInterceptor 進(jìn)行攔截,可配置多個攔截器,按順序執(zhí)行。
import java.lang.reflect.Method; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * @author * */ public class MyInterceptor implements MethodInterceptor{ @Override public Object invoke(MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod();//獲取被攔截的方法 Object[] arguments = invocation.getArguments();//獲取攔截方法的參數(shù) /* * 特殊,某些權(quán)限需要做特殊處理 * 比如用戶信息權(quán)限,在方法執(zhí)行完畢返回的時候,要將電話號碼與郵箱抹除 */ //環(huán)繞通知前置特殊處理 this.beforeReslove(); Object proceed = invocation.proceed();//調(diào)用目標(biāo)方法 //環(huán)繞通知后置特殊處理 proceed = this.afterReslove(); return proceed; } private Object afterReslove() { System.out.println("CurrentTime = " + System.currentTimeMillis()); return null; } private void beforeReslove() { System.out.println("CurrentTime = " + System.currentTimeMillis()); } }
4、使用注解AspectJ實現(xiàn)AOP
ApplicationContext.xml 加入
<aop:aspectj-autoproxy/>
創(chuàng)建切面處理類
package com.marving.aop; import java.util.Arrays; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect @Component public class AspectHandler { @Pointcut("execution(* com.marving.service.BaseServ+.*(..))") private void doMethod() { } /** * This is the method which I would like to execute before a selected method * execution. */ @Before("doMethod()") public void beforeAdvice() { System.out.println("before method invoked."); } /** * This is the method which I would like to execute after a selected method * execution. */ @After("doMethod()") public void afterAdvice() { System.out.println("after method invoked."); } // 配置controller環(huán)繞通知,使用在方法aspect()上注冊的切入點 @Around("doMethod()") public Object around(ProceedingJoinPoint pjp) throws Throwable{ Object result = null; String methodName = pjp.getSignature().getName(); try { System.out.println("The method [" + methodName + "] begins with " + Arrays.asList(pjp.getArgs())); result = pjp.proceed(); } catch (Throwable e) { System.out.println("The method [" + methodName + "] occurs expection : " + e); throw new RuntimeException(e); } System.out.println("The method [" + methodName + "] ends"); return result; } }
通過表達(dá)式execution(* com.marving.service.BaseServ+.*(..)) 匹配切入點函數(shù),并使用@Before@After@Around 對所攔截方法執(zhí)行前、中、后進(jìn)行攔截并執(zhí)行處理函數(shù)。
@Around @Before @After三個注解的區(qū)別@Before是在所攔截方法執(zhí)行之前執(zhí)行一段邏輯。@After 是在所攔截方法執(zhí)行之后執(zhí)行一段邏輯。@Around是可以同時在所攔截方法的前后執(zhí)行一段邏輯。
值得注意的是,Around在攔截方法后,需要返回一個方法執(zhí)行結(jié)果,否則,原方法不能正常執(zhí)行。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis使用resultMap如何解決列名和屬性名不一致
這篇文章主要介紹了MyBatis使用resultMap如何解決列名和屬性名不一致的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01解決Maven的pom.xml中設(shè)置repository不起作用問題
這篇文章主要介紹了解決Maven的pom.xml中設(shè)置repository不起作用問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03Spring boot + mybatis + Vue.js + ElementUI 實現(xiàn)數(shù)據(jù)的增刪改查實例代碼(一)
這篇文章主要介紹了Spring boot + mybatis + Vue.js + ElementUI 實現(xiàn)數(shù)據(jù)的增刪改查實例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-05-05Java購物系統(tǒng)設(shè)計與實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java購物系統(tǒng)設(shè)計與實現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01JAVA下單接口優(yōu)化實戰(zhàn)TPS性能提高10倍
今天小編就為大家分享一篇關(guān)于JAVA下單接口優(yōu)化實戰(zhàn)TPS性能提高10倍,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12Java中的字符流FileReader與FileWriter詳解
這篇文章主要介紹了Java中的字符流FileReader與FileWriter詳解,在Java中,使用Unicode約定存儲字符,字符流自動允許我們逐字符讀/寫數(shù)據(jù),有助于執(zhí)行16位Unicode的輸入和輸出,它是以reader和writer結(jié)尾的,需要的朋友可以參考下2023-10-10