前面我們看過javaassit是如何破解java應用,核心都是AOP相關的知識,今天我們看下Spring AOP是怎么回事!
Spring-AOP
spring 5.x版本
AOP面向切面編程,通過預編譯方式和運行期間動態代理實現程序功能的統一維護的一種技術。AOP是OOP的延續,從另一視角擴展了對面向對象編程的形式。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發的效率。
Spring AOP與IOC作為整個Spring框架最為核心的兩個部分,其意義不言而喻。在Spring中,我們都是面向Bean的裝配與管理,一個Bean是一個對象,也可以是一個代理。
概念
Aspect(切面):Aspect聲明類似于Java中的類聲明,在Aspect中會包含著一些Pointcut以及相應的 Advice。
JointPoint(連接點):表示在程序中明確定義的點,典型的包括方法調用,對類成員的訪問以及異常處理程序塊的執行等等,它自身還可以嵌套其它joint point。
Pointcut(切點):按規則匹配的JointPoint,這些JointPoint或是通過邏輯關系組合起來,或是通過通配、正則表達式等方式集中起來,它定義了相應的Advice執行的具體地方。
Advice(通知):Advice定義了在Pointcut里面定義的程序點具體要做的操作,它通過before、after和around來區別是在每個JointPoint之前、之后還是代替執行的代碼。
Target(目標對象):將Advice織入到目標對象.。
Weaving(織入):將Aspect和其他對象連接起來, 并創建增強(代理)對象的過程
下面從幾個方面了解Spring中如何使用AOP,你會發現,所有的配置都是圍繞著這些概念。Spring提供了AOP代理的上下文環境,而對目標對象的加強(Aspect、Advisor)都是由開發者自己完成。
Spring-aop.xml
- 基于aspect配置AOP,一個Aspect包含Pointcut與Advice:
< beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
< !-- 業務類 -- >
< bean id="queryService" class="com.sucl.blog.springaop.service.QueryService"/ >
< !-- 定義Aspect,aop:method 的來源 -- >
< bean id="logAspect" class="com.sucl.blog.springaop.aspect.LogAspect"/ >
< !-- 基于Aspect-- >
< aop:config >
< aop:aspect ref="logAspect" >
< aop:pointcut id="pointcut" expression="execution(* com.sucl.blog.springaop.service..*(..))"/ >
< aop:before method="before" pointcut-ref="pointcut"/ >
< aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/ >
< aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/ >
< aop:after method="after" pointcut-ref="pointcut"/ >
< aop:around method="around" pointcut-ref="pointcut"/ >
< /aop:aspect >
< /aop:config >
< /beans >
- 基于advice配置AOP,這種方式更方便Advice的復用
< beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
< bean id="queryService" class="com.sucl.blog.springaop.service.QueryService"/ >
< bean id="logAdvice" class="com.sucl.blog.springaop.advice.LogAdvice"/ >
< !-- 基于Advisor -- >
< aop:config >
< aop:pointcut id="pointcut" expression="execution(* com.sucl.blog.springaop.service..*(..))"/ >
< aop:advisor advice-ref="logAdvice" pointcut-ref="pointcut"/ >
< /aop:config >
< /beans >
@Aspect注解
基于注解@Aspect定義切面來增強目標對象,與上面的XML第一種配置對應,記得使用@EnableAspectJAutoProxy注解開啟
@Slf4j
@Aspect
public class LogAspect {
@Pointcut("execution(* com.sucl.blog.springaop.service..*(..))")
public void pointcut(){}
/**
* 方法執行前執行
* @param joinPoint
* @param arg1
*/
@Before(value = "pointcut()")
public void before(JoinPoint joinPoint){
log.info(" >> > 執行 before");
}
/**
* 方法出現異常時執行
* @param joinPoint
*/
@AfterThrowing(value = "pointcut()", throwing = "e")
public void afterThrowing(JoinPoint joinPoint, Exception e){
log.info(" >> > 執行 afterThrowing: {}", e.getMessage());
}
/**
* 方法返回后執行,異常時不執行
* @param joinPoint
*/
@AfterReturning(value = "pointcut()", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result){
log.info(" >> > 執行 afterReturning: {}" ,result);
}
/**
* 方法執行完執行,不管是否發生異常
* @param joinPoint
*/
@After(value = "pointcut()")
public void after(JoinPoint joinPoint){
log.info(" >> > 執行 after");
}
/**
* 在after與 around 前后執行
* @param pjp
* @param jp
*/
@Around(value = "pointcut()")
public void around(ProceedingJoinPoint pjp){
try {
log.info(" >> > 執行 around starting");
pjp.proceed();
log.info(" >> > 執行 around finished");
} catch (Throwable e) {
log.info(" >> > 執行 around 異常:{}", e.getMessage());
}
}
}
AOP代理執行順序
spring 5.2.7之后的執行順序:
Pointcut表達式(AspectJ)
execution:用于匹配方法執行的連接點
within:用于匹配指定類型內的方法執行
this:用于匹配當前AOP代理對象類型的執行方法;注意是AOP代理對象的類型匹配,這樣就可能包括引入接口也類型匹配
target:用于匹配當前目標對象類型的執行方法;注意是目標對象的類型匹配,這樣就不包括引入接口也類型匹配
args:用于匹配當前執行的方法傳入的參數為指定類型的執行方法
@within:用于匹配所以持有指定注解類型內的方法
@target:用于匹配當前目標對象類型的執行方法,其中目標對象持有指定的注解
@args:用于匹配當前執行的方法傳入的參數持有指定注解的執行
@annotation:用于匹配當前執行方法持有指定注解的方法
bean:Spring AOP擴展的,AspectJ沒有對于指示符,用于匹配特定名稱的Bean對象的執行方法
基于@Configuration
通過ProxyFactoryBean我們可以生成基于目標對象的代理。通過下面幾行代碼,加上自定義的切面實現(MethodInterceptor、Advice等接口的實現),就可以實現上面的before、after、around等通知切面。
@Configuration
public class ProxyConfiguration {
@Bean
public ProxyFactoryBean printServiceProxy() {
ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
proxyFactoryBean.setTarget(new PrintService());
proxyFactoryBean.setProxyTargetClass(true);
addInterceptors(proxyFactoryBean);
return proxyFactoryBean;
}
/**
* 定義方法攔截器:(支持通配符)
* org.springframework.aop.Advisor
* org.aopalliance.intercept.Interceptor
*
* MethodBeforeAdvice
* AfterReturningAdvice
* ThrowsAdvice
*
* org.aopalliance.intercept.MethodInterceptor
* org.aopalliance.aop.Advice
*
* @param proxyFactoryBean
*/
private void addInterceptors(ProxyFactoryBean proxyFactoryBean) {
proxyFactoryBean.setInterceptorNames("logAdvice");
}
@Bean
public LogAdvice logAdvice() {
return new LogAdvice();
}
}
@Slf4j
public class LogAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
try {
log.info(" >> > before");
Object result = invocation.proceed();
log.info(" >> > afterReturning : {}", result);
return result;
} catch (Throwable e) {
log.info(" >> > afterThrowing : {}", e.getMessage());
throw e;
} finally {
log.info(" >> > after");
}
}
}
實現原理
這里說到的是上面第二種通過@Aspect的形式實現AOP功能,這種模式更適合業務模塊中對特定模塊內的方法進行業務代理。我們需要依賴注解 EnableAspectJAutoProxy , 下面來看看具體如何實現?
- 在EnableAspectJAutoProxy中可以看到@Import(AspectJAutoProxyRegistrar.class),如果你之前看過之前的spring boot start你就知道,這里是基于ImportBeanDefinitionRegistrar在Spring容器中注冊BeanDefinition。
- 可以看到AspectJAutoProxyRegistrar#registerBeanDefinitions第一行,AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry), 主要是注冊了BeanPostProcessor(AnnotationAwareAspectJAutoProxyCreator)。
- 我們知道在Spring Bean生命中期中,Bean執行初始化前后會執行容器中的nPostProcessor,Spring AOP即通過AnnotationAwareAspectJAutoProxyCreator這個BeanPostProcessor完成Bean的代理工作。
- 其父類AbstractAutoProxyCreator中,postProcessAfterInitialization -> wrapIfNecessary -> createProxy -> ProxyFactory -> getProxy 到這里,Bean的代理對象也就生成了, 當然省略了各種判斷以及加工過程。
代理方式
- Cglib和JDK Proxy
Spring AOP主要是通過這兩個代理框架來實現代理的。一般情況下,基于接口代理時使用JDK動態代理,否則使用Cglib.- Java動態代理只能夠對接口進行代理,不能對普通的類進行代理(因為所有生成的代理類的父類為Proxy,Java類繼承機制不允許多重繼承)。而CGLIB能夠代理普通類
- Java動態代理使用Java原生的反射API進行操作,在生成類上比較高效;CGLIB使用ASM框架直接對字節碼進行操作,在類的執行過程中比較高效
- AspectJ
AspectJ是一個功能強大的面相切面編程框架,是對Java面向對象的擴展,支持編譯時、編譯后、加載時為目標對象(不僅僅是類方法)織入代理。在Spring AOP中引入了aspectjweaver.jar,僅僅使用了其注解。
下面是網上Spring AOP與AspectJ的比對
Spring AOP | AspectJ |
---|---|
用純Java實現 | 使用Java編程語言的擴展實現 |
無需單獨的編譯過程 | 除非設置了LTW,否則需要AspectJ編譯器(ajc) |
僅需運行時編織 | 運行時編織不可用。支持編譯時,后編譯和加載時編織 |
不足–僅支持方法級編織 | 更強大–可以編織字段,方法,構造函數,靜態初始值設定項,最終類/方法等 |
只能在Spring容器管理的bean上實現 | 可以在所有領域對象上實施 |
僅支持方法執行切入點 | 支持所有切入點 |
代理是針對目標對象創建的,并且方面已應用于這些代理 | 在應用程序執行之前(運行時之前)將方面直接編織到代碼中 |
比AspectJ慢得多 | 更好的性能 |
易于學習和應用 | 比Spring AOP復雜得多 |
結束語
Spring AOP是整個Spring框架的核心,里面涉及到的內容不是很多,但是都比較有深度,在spring諸多模塊中,比如事務、緩存、鑒權等等方面都有使用到。由于springboot的出現,xml的配置形式使用 得比較少了,但是這種配置的形式更直白地體現了AOP需要的配置以及各個組件的依賴關系。而基于@Aspect的形式代理業務模塊中的方法更簡單直觀,而基于ProxyFactoryBean、ProxyFactory的方式,在編寫類似 cache的模塊會更加的靈活。
-
JAVA
+關注
關注
19文章
2976瀏覽量
105211 -
開發
+關注
關注
0文章
370瀏覽量
40937 -
程序
+關注
關注
117文章
3797瀏覽量
81456 -
spring
+關注
關注
0文章
340瀏覽量
14398 -
AOP
+關注
關注
0文章
40瀏覽量
11124
發布評論請先 登錄
相關推薦
什么是java spring
Spring工作原理
Spring筆記分享
「Spring認證」Spring Hello World 項目示例
java的動態代理機制和作用
java動態代理機制詳解的類和接口描述
Spring認證_什么是Spring GraphQL
![<b class='flag-5'>Spring</b>認證_什么是<b class='flag-5'>Spring</b> GraphQL](https://file.elecfans.com//web2/M00/0E/C4/pYYBAGEM1vmAACSgAAG9KGVFbn4357.jpg)
「Spring認證」Spring IoC 容器
![「<b class='flag-5'>Spring</b>認證」<b class='flag-5'>Spring</b> IoC 容器](https://file.elecfans.com//web2/M00/10/66/pYYBAGEcmgaAaV0lAAG9KGVFbn4087.jpg)
如何獲得Spring認證?學習JAVA如何獲得Spring Professional認證?
![如何獲得<b class='flag-5'>Spring</b>認證?學習<b class='flag-5'>JAVA</b>如何獲得<b class='flag-5'>Spring</b> Professional認證?](https://file.elecfans.com//web2/M00/4E/CF/poYBAGLCTmeAO7LwAAAlboje75E409.png)
聊聊在使用Spring AOP時一個非常常見的概念AspectJ
![聊聊在使用<b class='flag-5'>Spring</b> <b class='flag-5'>AOP</b>時一個非常常見的概念AspectJ](https://file1.elecfans.com/web2/M00/A0/C9/wKgZomTunqGAXv4VAAAmjXyX8rg513.png)
AOP要怎么使用
![<b class='flag-5'>AOP</b>要怎么使用](https://file1.elecfans.com/web2/M00/A9/2F/wKgZomUjthuAC-q2AAKbegXXPrU488.jpg)
評論