Spring?AOP配置文件方式入門教程詳解
一、什么是 AOP?
在學(xué)習(xí) Spring 時,我們常聽到 AOP(面向切面編程)。
簡單來說,AOP 就是:
在不改變原有業(yè)務(wù)代碼的情況下,給程序“加點料”。
例如,我們在保存用戶時,想額外加上:
打印日志;
記錄操作時間;
發(fā)送短信;
或者進(jìn)行事務(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;
/**
* 自定義切面類 = 切入點(表達(dá)式) + 通知(增強代碼)
*/
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 生效啦 ??
四、切入點表達(dá)式詳解
AOP 的核心是 切入點表達(dá)式,用來告訴 Spring 哪些方法要被增強。
基本格式:
execution([修飾符] 返回值類型 包名.類名.方法名(參數(shù)))
示例說明:
| 表達(dá)式 | 含義 |
|---|---|
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)文章
Spring注解驅(qū)動之ApplicationListener用法解讀
這篇文章主要介紹了Spring注解驅(qū)動之ApplicationListener用法解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
詳細(xì)理解JAVA面向?qū)ο蟮姆庋b,繼承,多態(tài),抽象
這篇文章主要介紹了Java基礎(chǔ)之面向?qū)ο髾C制(多態(tài)、繼承)底層實現(xiàn),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-07-07
SpringBoot多環(huán)境配置數(shù)據(jù)讀取方式
SpringBoot通過環(huán)境隔離機制,支持properties/yaml/yml多格式配置,結(jié)合@Value、Environment和@ConfigurationProperties實現(xiàn)靈活配置讀取,下面給大家介紹SpringBoot之多環(huán)境配置全解析,感興趣的朋友一起看看吧2025-07-07
springboot統(tǒng)一異常處理(返回json)并格式化異常
這篇文章主要介紹了springboot統(tǒng)一異常處理(返回json)并格式化異常,對spring boot的默認(rèn)異常處理方式進(jìn)行修改,要統(tǒng)一返回數(shù)據(jù)格式,優(yōu)雅的數(shù)據(jù)交互,優(yōu)雅的開發(fā)應(yīng)用,需要的朋友可以參考下2023-07-07
Spring Boot項目如何同時支持HTTP和HTTPS協(xié)議的實現(xiàn)
這篇文章主要介紹了Spring Boot項目如何同時支持HTTP和HTTPS協(xié)議的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
基于SpringBoot與Vue開發(fā)Web版SQL執(zhí)行工具
這篇文章主要為大家詳細(xì)介紹了一個基于Web的SQL執(zhí)行工具的設(shè)計與實現(xiàn),該工具允許用戶通過瀏覽器直接執(zhí)行SQL語句并查看結(jié)果,需要的小伙伴可以了解下2025-08-08
詳解Java數(shù)組的一維和二維講解和內(nèi)存顯示圖
這篇文章主要介紹了Java數(shù)組的一維和二維講解和內(nèi)存顯示圖,數(shù)組就相當(dāng)于一個容器,存放相同類型數(shù)據(jù)的容器。而數(shù)組的本質(zhì)上就是讓我們能 "批量" 創(chuàng)建相同類型的變量,需要的朋友可以參考下2023-05-05
詳解spring開發(fā)_JDBC操作MySQL數(shù)據(jù)庫
本篇文章主要介紹了spring開發(fā)_JDBC操作MySQL數(shù)據(jù)庫,具有一定的參考價值,有興趣的可以了解一下。2016-12-12

