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

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

