一篇文章帶你了解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)對(duì)象獲得具體方法
<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>⑤、測(cè)試
@Test
public void testAop(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService useService = (UserService) context.getBean("userService");
useService.addUser();
useService.deleteUser();
}⑥、控制臺(tái)打印結(jié)果

上面的例子很簡(jiǎn)單,就是在 UserService 的 addUser()方法和 deleteUser()方法增加前置通知和后置通知,這在實(shí)際操作中很好理解。比如這是和數(shù)據(jù)庫(kù)打交道的話,那么我們?cè)赼ddUser() 或者deleteUser() 時(shí),必須要在前面開始事務(wù),操作完畢后提交事務(wù)。下面我們就用注解的方式來(lái)配置。
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)類:

切面類:

③、配置掃描注解識(shí)別
這個(gè)我們?cè)谇懊嬉仓v過(guò),上面配置的注解,Spring 如何才能識(shí)別這些類上添加了注解呢?我們必須告訴他。
在applicationContext.xml 文件中添加如下配置:
<!-- 配置掃描注解類
base-package:表示含有注解類的包名。
如果掃描多個(gè)包,則下面的代碼書寫多行,改變 base-package 里面的內(nèi)容即可!
-->
<context:component-scan base-package="com.ys.aop"></context:component-scan>④、注解配置 AOP
一、我們用xml配置過(guò)如下:

這是告訴 Spring 哪個(gè)是切面類。下面我們用注解配置
我們?cè)谇忻骖惿咸砑?@Aspect 注解,如下:

二、如何讓 Spring 認(rèn)識(shí)我們所配置的 AOP 注解?
光有前面的類注解掃描是不夠的,這里我們要額外配置 AOP 注解識(shí)別。
我們?cè)?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" 配置,這是用來(lái)獲得目標(biāo)方法的返回值的。
注解配置如下:

五、測(cè)試
@Test
public void testAopAnnotation(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_Annotation.xml");
UserService useService = (UserService) context.getBean("userService");
useService.addUser();
useService.deleteUser();
}六、控制臺(tái)打印結(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(){} 之后通過(guò)“方法名”獲得切入點(diǎn)引用
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java人機(jī)猜拳實(shí)現(xiàn)的思路及方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Java人機(jī)猜拳實(shí)現(xiàn)的思路及方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
如何通過(guò)properties文件配置web.xml中的參數(shù)
這篇文章主要介紹了如何通過(guò)properties文件配置web.xml中的參數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot實(shí)用小技巧之如何動(dòng)態(tài)設(shè)置日志級(jí)別
這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)用小技巧之如何動(dòng)態(tài)設(shè)置日志級(jí)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用SpringBoot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
spring batch使用reader讀數(shù)據(jù)的內(nèi)存容量問(wèn)題詳解
這篇文章主要介紹了spring batch使用reader讀數(shù)據(jù)的內(nèi)存容量問(wèn)題詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Java給JFrame窗口設(shè)置熱鍵的方法實(shí)現(xiàn)
這篇文章主要介紹了Java給JFrame窗口設(shè)置熱鍵的方法實(shí)現(xiàn),文中通過(guò)示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

