Spring?AOP配置文件方式入門教程詳解
一、什么是 AOP?
在學(xué)習(xí) Spring 時,我們常聽到 AOP(面向切面編程)。
簡單來說,AOP 就是:
在不改變原有業(yè)務(wù)代碼的情況下,給程序“加點料”。
例如,我們在保存用戶時,想額外加上:
打印日志;
記錄操作時間;
發(fā)送短信;
或者進行事務(wù)控制。
這些跟業(yè)務(wù)無關(guān)的功能,統(tǒng)一抽取到一個“切面”里,通過 AOP 技術(shù)“織入”到業(yè)務(wù)方法中,就不用每次都手寫一遍啦!
二、AOP 的相關(guān)術(shù)語(一定要理解這幾個!)
| 術(shù)語 | 含義 | 舉例 |
|---|---|---|
| Joinpoint(連接點) | 被攔截的方法 | save() 方法 |
| Pointcut(切入點) | 定義要攔截哪些方法 | 只攔截 save() 開頭的方法 |
| Advice(通知/增強) | 要做的事情(增強邏輯) | 打印日志、事務(wù)控制 |
| Target(目標(biāo)對象) | 被代理的業(yè)務(wù)類 | UserServiceImpl |
| Weaving(織入) | 把增強邏輯應(yīng)用到目標(biāo)對象的過程 | 運行時動態(tài)生成代理 |
| Proxy(代理對象) | 織入增強后產(chǎn)生的新對象 | 由 Spring 生成 |
| Aspect(切面) | 切入點 + 通知 的組合 | 我們自定義的切面類 |
三、Spring AOP 配置文件方式入門
下面通過一個簡單的例子演示:
在執(zhí)行 UserServiceImpl.save() 方法之前,打印一句“增強的方法執(zhí)行了…”
創(chuàng)建 Maven 項目并添加依賴
<dependencies>
<!-- Spring 核心容器 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!-- 日志相關(guān) -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<!-- 測試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- AOP 相關(guān)依賴 -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.3</version>
</dependency>
</dependencies>
創(chuàng)建業(yè)務(wù)類
UserService.java
package com.qcbyjy.demo2;
public interface UserService {
void save();
}
UserServiceImpl.java
package com.qcbyjy.demo2;
public class UserServiceImpl implements UserService {
@Override
public void save() {
System.out.println("業(yè)務(wù)層:保存用戶...");
}
}
創(chuàng)建切面類(增強邏輯)
MyXmlAspect.java
package com.qcbyjy.demo2;
/**
* 自定義切面類 = 切入點(表達式) + 通知(增強代碼)
*/
public class MyXmlAspect {
// 通知邏輯:增強方法
public void log() {
System.out.println("增強的方法執(zhí)行了...");
}
// 環(huán)繞通知示例
public void aroundLog(org.aspectj.lang.ProceedingJoinPoint pjp) throws Throwable {
System.out.println("環(huán)繞前...");
pjp.proceed(); // 手動執(zhí)行目標(biāo)方法
System.out.println("環(huán)繞后...");
}
}
Spring 配置文件(applicationContext_demo2.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/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">
<!-- 配置業(yè)務(wù)類 -->
<bean id="userService" class="com.qcbyjy.demo2.UserServiceImpl"/>
<!-- 配置切面類 -->
<bean id="myXmlAspect" class="com.qcbyjy.demo2.MyXmlAspect"/>
<!-- 配置 AOP 增強 -->
<aop:config>
<aop:aspect ref="myXmlAspect">
<!-- 前置通知:目標(biāo)方法執(zhí)行前增強 -->
<aop:before method="log"
pointcut="execution(* com.qcbyjy.demo2.UserServiceImpl.save(..))"/>
</aop:aspect>
</aop:config>
</beans>
測試類
Demo2.java
package com.qcbyjy.test;
import com.qcbyjy.demo2.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext_demo2.xml")
public class Demo2 {
@Autowired
private UserService userService;
@Test
public void run1() {
userService.save();
}
}
輸出結(jié)果:
增強的方法執(zhí)行了...
業(yè)務(wù)層:保存用戶...
說明 AOP 生效啦 ??
四、切入點表達式詳解
AOP 的核心是 切入點表達式,用來告訴 Spring 哪些方法要被增強。
基本格式:
execution([修飾符] 返回值類型 包名.類名.方法名(參數(shù)))
示例說明:
| 表達式 | 含義 |
|---|---|
execution(public void com.qcbyjy.demo2.UserServiceImpl.save()) | 精確匹配 |
execution(* com.qcbyjy.*.*ServiceImpl.*(..)) | 模糊匹配所有 ServiceImpl 的任意方法 |
execution(* com.qcbyjy.demo2.*.save*(..)) | 匹配 demo2 包下所有以 save 開頭的方法 |
通配符規(guī)則:
*代表任意(一個)內(nèi)容..代表任意包或任意參數(shù)
例如:
<aop:before method="log"
pointcut="execution(* com.qcbyjy.*.*ServiceImpl.save*(..))"/>
表示:增強所有以 save 開頭的方法。
五、AOP 的五種通知類型
| 通知類型 | 執(zhí)行時機 | XML 配置標(biāo)簽 |
|---|---|---|
| 前置通知 | 目標(biāo)方法執(zhí)行前 | <aop:before> |
| 后置通知 | 方法成功執(zhí)行后 | <aop:after-returning> |
| 異常通知 | 方法拋出異常后 | <aop:after-throwing> |
| 最終通知 | 不管成功或失敗都會執(zhí)行 | <aop:after> |
| 環(huán)繞通知 | 方法執(zhí)行前后都能增強(最靈活) | <aop:around> |
六、環(huán)繞通知示例
<aop:around method="aroundLog"
pointcut="execution(* com.qcbyjy.*.*ServiceImpl.save*(..))" />
執(zhí)行順序:
環(huán)繞前...
增強的方法執(zhí)行了...
業(yè)務(wù)層:保存用戶...
環(huán)繞后...
總結(jié)
| 概念 | 功能 |
|---|---|
| AOP | 把公共功能(如日志、事務(wù))從業(yè)務(wù)邏輯中分離出來 |
| 切面類(Aspect) | 定義要增強的邏輯 |
| 切入點(Pointcut) | 定義哪些方法會被增強 |
| 通知(Advice) | 什么時候增強(前置、后置、環(huán)繞等) |
| 代理對象(Proxy) | Spring 自動幫我們生成的“帶增強”的對象 |
通過這套機制,Spring AOP 讓你的代碼更清爽、可維護性更高。
到此這篇關(guān)于Spring AOP配置文件方式入門教程的文章就介紹到這了,更多相關(guān)Spring AOP配置文件方式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spingboot?JPA?CriteriaBuilder?如何獲取指定字段
這篇文章?主要介紹了Spingboot?JPA?CriteriaBuilder?如何獲取指定字段,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Springboot Redis?哨兵模式的實現(xiàn)示例
本文主要介紹了Springboot Redis?哨兵模式的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
springboot druid mybatis多數(shù)據(jù)源配置方式
這篇文章主要介紹了springboot druid mybatis多數(shù)據(jù)源配置方式,具有很好的參考價值,希望對大家有所幫助,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
SpingMvc復(fù)雜參數(shù)傳收總結(jié)
這篇文章主要為大家介紹了SpingMvc復(fù)雜參數(shù)傳收總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
Java使用Maven BOM統(tǒng)一管理版本號的實現(xiàn)
這篇文章主要介紹了Java使用Maven BOM統(tǒng)一管理版本號的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

