Spring中AOP的切點、通知、切點表達(dá)式及知識要點整理
1.2.1、需要編寫的內(nèi)容
- 編寫核心業(yè)務(wù)代碼(目標(biāo)類的目標(biāo)方法)
- 編寫切面類,切面類中有通知(增強功能方法)
- 在配置文件中,配置織入關(guān)系,即將哪些通知與哪些連接點進(jìn)行結(jié)合
1.2.2、AOP 技術(shù)實現(xiàn)的內(nèi)容
Spring 框架監(jiān)控切入點方法的執(zhí)行。一旦監(jiān)控到切入點方法被運行,使用代理機制,動態(tài)創(chuàng)建目標(biāo)對象的代理對象,根據(jù)通知類別,在代理對象的對應(yīng)位置,將通知對應(yīng)的功能織入,完成完整的代碼邏輯運行。
1.2.3、AOP 底層使用哪種代理方式
在 spring 中,框架會根據(jù)目標(biāo)類是否實現(xiàn)了接口來決定采用哪種動態(tài)代理的方式。
1.2.4、知識要點
- aop:面向切面編程
- aop底層實現(xiàn):基于JDK的動態(tài)代理 和 基于Cglib的動態(tài)代理
- aop的重點概念:
Pointcut(切入點):被增強的方法 Advice(通知/ 增強):封裝增強業(yè)務(wù)邏輯的方法 Aspect(切面):切點+通知 Weaving(織入):將切點與通知結(jié)合的過程
開發(fā)明確事項:
誰是切點(切點表達(dá)式配置)
誰是通知(切面類中的增強方法)
將切點和通知進(jìn)行織入配置
1.2.5、 基于 XML 的 AOP 開發(fā)
1.2.5.1 快速入門
①導(dǎo)入 AOP 相關(guān)坐標(biāo)
②創(chuàng)建目標(biāo)接口和目標(biāo)類(內(nèi)部有切點)
③創(chuàng)建切面類(內(nèi)部有增強方法)
④將目標(biāo)類和切面類的對象創(chuàng)建權(quán)交給 spring
⑤在 applicationContext.xml 中配置織入關(guān)系
⑥測試代碼
①導(dǎo)入 AOP 相關(guān)坐標(biāo)
<!--導(dǎo)入spring的context坐標(biāo),context依賴aop--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.5.RELEASE</version> </dependency> <!-- aspectj的織入 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency>
②創(chuàng)建目標(biāo)接口和目標(biāo)類(內(nèi)部有切點)
//接口
public interface TargetInterface {
public void method();
}
//實現(xiàn)類
public class Target implements TargetInterface {
@Override
public void method() {
System.out.println("Target running....");
}
}③創(chuàng)建切面類(內(nèi)部有增強方法)
public class MyAspect {
//前置增強方法
public void before(){
System.out.println("前置代碼增強.....");
}
}④將目標(biāo)類和切面類的對象創(chuàng)建權(quán)交給 spring管理
<!--配置目標(biāo)類--> <bean id="target" class="com.itheima.aop.Target"></bean> <!--配置切面類--> <bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>
⑤在 applicationContext.xml 中配置織入關(guān)系
導(dǎo)入aop命名空間
<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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">⑤在 applicationContext.xml 中配置織入關(guān)系
配置切點表達(dá)式和前置增強的織入關(guān)系
<!--配置織入,告訴spring框架哪些方法(切點)要進(jìn)行增強(前置增強/后置增強)-->
<aop:config>
<!--聲明切面-->
<!--引用myAspect的Bean為切面對象-->
<aop:aspect ref="myAspect">
<!--配置切點-->
<!--配置Target的method方法執(zhí)行時要進(jìn)行myAspect的before方法前置增強-->
<!--切面:切點+通知, 要配置那個方法需要前置增強(要配置切入點,需要增強的方法) -->
<aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.method())"></aop:before>
</aop:aspect>
</aop:config>⑥測試代碼
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
@Autowired
private TargetInterface target;
@Test
public void test1(){
target.method();
}
}1.2.5.2 切點表達(dá)式的寫法
表達(dá)式語法:
execution([修飾符] 返回值類型 包名.類名.方法名(參數(shù)))
- 訪問修飾符可以省略
- 返回值類型、包名、類名、方法名可以使用星號* 代表任意
- 包名與類名之間一個點 . 代表當(dāng)前包下的類,兩個點 … 表示當(dāng)前包及其子包下的類
- 參數(shù)列表可以使用兩個點 … 表示任意個數(shù),任意類型的參數(shù)列表
例如:
execution(public void com.itheima.aop.Target.method()) //不常用,具體的方法,限制了 execution(void com.itheima.aop.Target.*(..)) //具體的類下的所有方法都會被增強,無返回值 execution(* com.itheima.aop.*.*(..)) //常用(推薦),該包下的任意類的任意方法(參數(shù)任意)都會被增強,返回值任意 execution(* com.itheima.aop..*.*(..)) //aop下及其子包下的任意類任意方法都會被增強 execution(* *..*.*(..)) //全部都任意
1.2.5.3 通知的類型
通知的配置語法:
<aop:通知類型 method=“切面類中方法名” pointcut=“切點表達(dá)式"></aop:通知類型>

Java類:
//增強對象
public class MyAspect {
//后置增強
public void before() {
System.out.println("前置增強................................");
}
//后置增強
public void afterReturning() {
System.out.println("后置增強................................");
}
//Proceeding JoinPoint 正在執(zhí)行的鏈接點----切點
//環(huán)繞增強
public Object Around(ProceedingJoinPoint point) throws Throwable {
System.out.println("環(huán)繞前增強................................");
Object proceed = point.proceed();//切點方法
System.out.println("環(huán)繞后增強................................");
return proceed;
}
//異常增強
public void afterThrowing() {
System.out.println("異常增強................................");
}
//最終增強
public void after() {
System.out.println("最終增強................................");
}
}
xml:
<!--切面:切點+通知, 要配置那個方法需要前置增強(要配置切入點,需要增強的方法) -->
<!-- <aop:before method="before" pointcut="execution(public void com.lfs.aop.Target.save())"/>-->
<aop:before method="before" pointcut="execution( * com.lfs.aop.*.*(..))"/>
<!--后置增強-->
<aop:after-returning method="afterReturning" pointcut="execution(* com.lfs.aop.*.*(..))"/>
<!--環(huán)繞增強-->
<aop:around method="Around" pointcut="execution(* com.lfs.aop.*.*(..))"/>
<!--異常增強-->
<aop:after-throwing method="afterThrowing" pointcut="execution(* com.lfs.aop.*.*(..))"/>
<!--最終增強-->
<aop:after method="after" pointcut="execution(* com.lfs.aop.*.*(..))"/>1.2.5.4 切點表達(dá)式的抽取
當(dāng)多個增強的切點表達(dá)式相同時,可以將切點表達(dá)式進(jìn)行抽取,在增強中使用 pointcut-ref 屬性代替 pointcut 屬性來引用抽取后的切點表達(dá)式。
//切點表達(dá)式 <aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
<aop:config>
<!--引用myAspect的Bean為切面對象-->
<aop:aspect ref="myAspect">
<aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
<aop:before method="before" pointcut-ref="myPointcut"></aop:before>
</aop:aspect>
</aop:config>1.2.5.5 知識要點
aop織入的配置
<aop:config>
<aop:aspect ref=“切面類”>
<aop:before method=“通知方法名稱” pointcut=“切點表達(dá)式"></aop:before>
</aop:aspect>
</aop:config>- 通知的類型:前置通知、后置通知、環(huán)繞通知、異常拋出通知、最終通知
- 切點表達(dá)式的寫法:
execution([修飾符] 返回值類型 包名.類名.方法名(參數(shù)))
到此這篇關(guān)于Spring中AOP的切點、通知、切點表達(dá)式以及知識要點的文章就介紹到這了,更多相關(guān)spring aop切點表達(dá)式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)冒泡排序算法及對其的簡單優(yōu)化示例
這篇文章主要介紹了Java實現(xiàn)冒泡排序算法及對其的簡單優(yōu)化示例,冒泡排序的最差時間復(fù)雜度為O(n^2),最優(yōu)時間復(fù)雜度為O(n),存在優(yōu)化的余地,需要的朋友可以參考下2016-05-05
Java使用正則表達(dá)式實現(xiàn)找出數(shù)字功能示例
這篇文章主要介紹了Java使用正則表達(dá)式實現(xiàn)找出數(shù)字功能,結(jié)合實例形式分析了Java針對數(shù)字的匹配查找及非數(shù)字替換操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-03-03
springboot 啟動如何修改application.properties的參數(shù)
這篇文章主要介紹了springboot 啟動如何修改application.properties的參數(shù)方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
詳解BeanUtils.copyProperties()方法如何使用
這篇文章主要為大家介紹了詳解BeanUtils.copyProperties()方法如何使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Spring Boot Maven 打包可執(zhí)行Jar文件的實現(xiàn)方法
這篇文章主要介紹了Spring Boot Maven 打包可執(zhí)行Jar文件的實現(xiàn)方法,需要的朋友可以參考下2018-02-02

