一篇文章帶你了解Spring?AOP?的注解
1、xml 的方式實(shí)現(xiàn) AOP
①、接口 UserService
package com.ys.aop; public interface UserService { //添加 user public void addUser(); //刪除 user public void deleteUser(); }
②、實(shí)現(xiàn)類 UserServiceImpl
package com.ys.aop; public class UserServiceImpl implements UserService{ @Override public void addUser() { System.out.println("增加 User"); } @Override public void deleteUser() { System.out.println("刪除 User"); } }
③、切面類,也就是通知類 MyAspect
package com.ys.aop; import org.aspectj.lang.JoinPoint; public class MyAspect { /** * JoinPoint 能獲取目標(biāo)方法的一些基本信息 * @param joinPoint */ public void myBefore(JoinPoint joinPoint){ System.out.println("前置通知 : " + joinPoint.getSignature().getName()); } public void myAfterReturning(JoinPoint joinPoint,Object ret){ System.out.println("后置通知 : " + joinPoint.getSignature().getName() + " , -->" + ret); } public void myAfterThrowing(JoinPoint joinPoint,Throwable e){ System.out.println("拋出異常通知 : " + e.getMessage()); } public void myAfter(){ System.out.println("最終通知"); } }
④、AOP配置文件 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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--1、創(chuàng)建目標(biāo)類 --> <bean id="userService" class="com.ys.aop.UserServiceImpl"></bean> <!--2、創(chuàng)建切面類(通知) --> <bean id="myAspect" class="com.ys.aop.MyAspect"></bean> <!--3、aop編程 3.1 導(dǎo)入命名空間 3.2 使用 <aop:config>進(jìn)行配置 proxy-target-class="true" 聲明時(shí)使用cglib代理 如果不聲明,Spring 會(huì)自動(dòng)選擇cglib代理還是JDK動(dòng)態(tài)代理 <aop:pointcut> 切入點(diǎn) ,從目標(biāo)對象獲得具體方法 <aop:advisor> 特殊的切面,只有一個(gè)通知 和 一個(gè)切入點(diǎn) advice-ref 通知引用 pointcut-ref 切入點(diǎn)引用 3.3 切入點(diǎn)表達(dá)式 execution(* com.ys.aop.*.*(..)) 選擇方法 返回值任意 包 類名任意 方法名任意 參數(shù)任意 --> <aop:config> <aop:aspect ref="myAspect"> <!-- 切入點(diǎn)表達(dá)式 --> <aop:pointcut expression="execution(* com.ys.aop.*.*(..))" id="myPointCut"/> <!-- 3.1 前置通知 <aop:before method="" pointcut="" pointcut-ref=""/> method : 通知,及方法名 pointcut :切入點(diǎn)表達(dá)式,此表達(dá)式只能當(dāng)前通知使用。 pointcut-ref : 切入點(diǎn)引用,可以與其他通知共享切入點(diǎn)。 通知方法格式:public void myBefore(JoinPoint joinPoint){ 參數(shù)1:org.aspectj.lang.JoinPoint 用于描述連接點(diǎn)(目標(biāo)方法),獲得目標(biāo)方法名等 --> <aop:before method="myBefore" pointcut-ref="myPointCut"/> <!-- 3.2后置通知 ,目標(biāo)方法后執(zhí)行,獲得返回值 <aop:after-returning method="" pointcut-ref="" returning=""/> returning 通知方法第二個(gè)參數(shù)的名稱 通知方法格式:public void myAfterReturning(JoinPoint joinPoint,Object ret){ 參數(shù)1:連接點(diǎn)描述 參數(shù)2:類型Object,參數(shù)名 returning="ret" 配置的 --> <aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="ret" /> <!-- 3.3 最終通知 --> <aop:after method="myAfter" pointcut-ref="myPointCut"/> </aop:aspect> </aop:config> </beans>
⑤、測試
@Test public void testAop(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService useService = (UserService) context.getBean("userService"); useService.addUser(); useService.deleteUser(); }
⑥、控制臺打印結(jié)果
上面的例子很簡單,就是在 UserService 的 addUser()方法和 deleteUser()方法增加前置通知和后置通知,這在實(shí)際操作中很好理解。比如這是和數(shù)據(jù)庫打交道的話,那么我們在addUser() 或者deleteUser() 時(shí),必須要在前面開始事務(wù),操作完畢后提交事務(wù)。下面我們就用注解的方式來配置。
2、注解實(shí)現(xiàn) AOP
①、導(dǎo)入相應(yīng)的 jar 包,以及在 applicationContext.xml 文件中導(dǎo)入相應(yīng)的命名空間。
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
②、注解配置 bean
xml配置:
<!--1、創(chuàng)建目標(biāo)類 --> <bean id="userService" class="com.ys.aop.UserServiceImpl"></bean> <!--2、創(chuàng)建切面類(通知) --> <bean id="myAspect" class="com.ys.aop.MyAspect"></bean>
注解配置:
目標(biāo)類:
切面類:
③、配置掃描注解識別
這個(gè)我們在前面也講過,上面配置的注解,Spring 如何才能識別這些類上添加了注解呢?我們必須告訴他。
在applicationContext.xml
文件中添加如下配置:
<!-- 配置掃描注解類 base-package:表示含有注解類的包名。 如果掃描多個(gè)包,則下面的代碼書寫多行,改變 base-package 里面的內(nèi)容即可! --> <context:component-scan base-package="com.ys.aop"></context:component-scan>
④、注解配置 AOP
一、我們用xml配置過如下:
這是告訴 Spring 哪個(gè)是切面類。下面我們用注解配置
我們在切面類上添加 @Aspect 注解,如下:
二、如何讓 Spring 認(rèn)識我們所配置的 AOP 注解?
光有前面的類注解掃描是不夠的,這里我們要額外配置 AOP 注解識別。
我們在 applicationContext.xml
文件中增加如下配置:
<!--2、確定 aop 注解生效 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
三、注解配置前置通知
我們先看 xml 配置前置通知如下:
<!-- 切入點(diǎn)表達(dá)式 --> <aop:pointcut expression="execution(* com.ys.aop.*.*(..))" id="myPointCut"/> <!-- 3.1 前置通知 <aop:before method="" pointcut="" pointcut-ref=""/> method : 通知,及方法名 pointcut :切入點(diǎn)表達(dá)式,此表達(dá)式只能當(dāng)前通知使用。 pointcut-ref : 切入點(diǎn)引用,可以與其他通知共享切入點(diǎn)。 通知方法格式:public void myBefore(JoinPoint joinPoint){ 參數(shù)1:org.aspectj.lang.JoinPoint 用于描述連接點(diǎn)(目標(biāo)方法),獲得目標(biāo)方法名等 --> <aop:before method="myBefore" pointcut-ref="myPointCut"/>
那么注解的方式如下:
四、注解配置后置通知
xml 配置后置通知:
<!-- 3.2后置通知 ,目標(biāo)方法后執(zhí)行,獲得返回值 <aop:after-returning method="" pointcut-ref="" returning=""/> returning 通知方法第二個(gè)參數(shù)的名稱 通知方法格式:public void myAfterReturning(JoinPoint joinPoint,Object ret){ 參數(shù)1:連接點(diǎn)描述 參數(shù)2:類型Object,參數(shù)名 returning="ret" 配置的 --> <aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="ret" />
注意看,后置通知有個(gè) returning="ret" 配置,這是用來獲得目標(biāo)方法的返回值的。
注解配置如下:
五、測試
@Test public void testAopAnnotation(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_Annotation.xml"); UserService useService = (UserService) context.getBean("userService"); useService.addUser(); useService.deleteUser(); }
六、控制臺打印結(jié)果
3、注解改進(jìn)
我們可以看前置通知和后置通知的注解配置:
注意看紅色框住的部分,很顯然這里是重復(fù)的,而且如果我們有多個(gè)通知方法,那就得在每個(gè)方法名都寫上該注解,而且如果包名夠復(fù)雜,也很容易寫錯(cuò)。那么怎么辦呢?
解決辦法就是聲明公共切入點(diǎn):
①、在 切面類 MyAspect.java 中新增一個(gè)切入點(diǎn)方法 myPointCut(),然后在這個(gè)方法上添加@Pointcut 注解
②、那么前置通知和后置通知,我們可以進(jìn)行如下改寫配置:
4、總結(jié)
上面我們只進(jìn)行了前置通知和后置通知的講解,還有比如最終通知、環(huán)繞通知、拋出異常通知等,配置方式都差不多,這里就不進(jìn)行一一講解了。然后我們看一下這些通知的注解:
@Aspect 聲明切面,修飾切面類,從而獲得 通知。
通知
- @Before 前置
- @AfterReturning 后置
- @Around 環(huán)繞
- @AfterThrowing 拋出異常
- @After 最終
切入點(diǎn)
- @PointCut ,修飾方法 private void xxx(){} 之后通過“方法名”獲得切入點(diǎn)引用
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
mybatis-plus 表名添加前綴的實(shí)現(xiàn)方法
這篇文章主要介紹了mybatis-plus 表名添加前綴的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Maven打包所有依賴到一個(gè)可執(zhí)行jar中遇到的問題
這篇文章主要給大家介紹了關(guān)于Maven打包所有依賴到一個(gè)可執(zhí)行jar中遇到的問題,將依賴打入jar包,由于maven管理了所有的依賴,所以將項(xiàng)目的代碼和依賴打成一個(gè)包對它來說是順理成章的功能,需要的朋友可以參考下2023-10-10Java數(shù)組,去掉重復(fù)值、增加、刪除數(shù)組元素的方法
下面小編就為大家?guī)硪黄狫ava數(shù)組,去掉重復(fù)值、增加、刪除數(shù)組元素的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10使用Java編寫一個(gè)圖片word互轉(zhuǎn)工具
這篇文章主要介紹了使用Java編寫一個(gè)PDF?Word文件轉(zhuǎn)換工具的相關(guān)資料,需要的朋友可以參考下2023-01-01Java線程實(shí)現(xiàn)的三種方式詳細(xì)解析
這篇文章主要介紹了Java線程實(shí)現(xiàn)的三種方式詳細(xì)解析,Java多線程實(shí)現(xiàn)方式主要有三種,繼承Thread類、實(shí)現(xiàn)Runnable接口、使用ExecutorService、Callable、Future實(shí)現(xiàn)有返回結(jié)果的多線程,需要的朋友可以參考下2023-12-12RocketMQ之NameServer架構(gòu)設(shè)計(jì)及啟動(dòng)關(guān)閉流程源碼分析
這篇文章主要為大家介紹了RocketMQ之NameServer架構(gòu)設(shè)計(jì)及啟動(dòng)關(guān)閉流程源碼分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11SpringCloud容器化服務(wù)發(fā)現(xiàn)及注冊實(shí)現(xiàn)方法解析
這篇文章主要介紹了SpringCloud容器化服務(wù)發(fā)現(xiàn)及注冊實(shí)現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08