SpringBoot項(xiàng)目使用aop案例詳解
前言
IOC和AOP是Spring中的兩個(gè)核心的概念,簡單介紹一下我的理解:
IOC:控制反轉(zhuǎn),就是將以前由我們自己手動(dòng)創(chuàng)建對(duì)象的過程交給了Spring,Spring幫助我們生產(chǎn)對(duì)象、管理對(duì)象、管理對(duì)象和對(duì)象之間的依賴關(guān)系。降低了代碼的耦合度,方便我們后期對(duì)項(xiàng)目做維護(hù)。舉個(gè)通俗一點(diǎn)的例子:
正常情況下,我們?cè)诩?,餓了,自己做飯。
使用IOC情況下,我們?cè)诩遥I了,打電話給商家,飯送過來。
IOC就相當(dāng)于商家,做飯就相當(dāng)于創(chuàng)建對(duì)象。
也就是說正常情況下,當(dāng)一個(gè)類需要調(diào)用其他類的方法時(shí),我們手動(dòng)通過new、工廠或者其他方式創(chuàng)建對(duì)象。
使用IOC情況下,我們只需要注入對(duì)象即可。
AOP:面向切面(方便)編程,可以對(duì)某一類對(duì)象進(jìn)行監(jiān)督和控制,在調(diào)用這類對(duì)象方法的前后去調(diào)用指定的代碼,從而對(duì)一個(gè)方法進(jìn)行擴(kuò)展,從而達(dá)到增強(qiáng)模塊功能的效果。舉個(gè)通俗一點(diǎn)的例子:
正常情況下,直接吃飯。
使用AOP情況下,有個(gè)保姆關(guān)注著,吃飯前幫忙洗手,吃飯后幫忙收拾碗筷。
AOP就相當(dāng)于保姆,吃飯就相當(dāng)于帶調(diào)用具體的方法。
也就是說,當(dāng)我們想對(duì)方法進(jìn)行補(bǔ)充時(shí),并不去直接修改方法,而是通過AOP去補(bǔ)充。當(dāng)我們不想補(bǔ)充或者需要更換補(bǔ)充的時(shí)候,直接操作AOP即可。
1、Pointcut: 切點(diǎn),用于定義哪個(gè)方法會(huì)被攔截,例如 execution(* cn.springcamp.springaop.service..(…))
2、Advice: 攔截到方法后要執(zhí)行的動(dòng)作
3、Aspect: 切面,把Pointcut和Advice組合在一起形成一個(gè)切面
4、Join Point: 在執(zhí)行時(shí)Pointcut的一個(gè)實(shí)例
4、Weaver: 實(shí)現(xiàn)AOP的框架,例如 AspectJ 或 Spring AOP
一、SpringBoot項(xiàng)目引入AOP依賴
<!--aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>啟動(dòng)類加上@EnableAspectJAutoProxy注解,可以省略。因?yàn)樵贏OP的默認(rèn)配置屬性中,spring.aop.auto屬性默認(rèn)是開啟的。
也不需要再引入AspectJ依賴了。
二、普通方式
切面類代碼:
package com.example.myblog.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AOPTest {
//定義切入點(diǎn)
@Pointcut("execution(public * com.example.myblog.test.AOPTestClient.*(..))")
public void aspectTest(){}
//前置通知,切入點(diǎn)執(zhí)行之前執(zhí)行
@Before("aspectTest()")
public void doBefore(JoinPoint joinPoint){
System.out.println("前置通知");
}
//后置通知,切入點(diǎn)執(zhí)行之后執(zhí)行
@After("aspectTest()")
public void doAfter(JoinPoint joinPoint){
System.out.println("后置通知");
}
//最終通知,,切入點(diǎn)執(zhí)行之后執(zhí)行
@AfterReturning("aspectTest()")
public void doAfterReturning(JoinPoint joinPoint){
System.out.println("最終通知");
}
//異常通知,切入點(diǎn)拋出異常執(zhí)行
@AfterThrowing("aspectTest()")
public void deAfterThrowing(JoinPoint joinPoint){
System.out.println("異常通知");
}
//環(huán)繞通知,切入點(diǎn)執(zhí)行前、后執(zhí)行
@Around("aspectTest()")
public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("未執(zhí)行");
Object result = joinPoint.proceed();
System.out.println("已執(zhí)行");
//返回結(jié)果
return result;
}
}
切點(diǎn)類代碼:
package com.example.myblog.test;
import org.springframework.stereotype.Component;
@Component
public class AOPTestClient {
public void test(){
System.out.println("正在測試AOP");
}
}測試類代碼:
package com.example.myblog;
import com.example.myblog.test.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class MyblogApplicationTests {
@Autowired
private AOPTestClient aopTestClient;
@Test
public void testAOP(){
aopTestClient.test();
}
}測試結(jié)果:

三、注解方式
自定義注解代碼:
package com.example.myblog.test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//表示次注解可以標(biāo)注在類和方法上
@Target({ElementType.METHOD, ElementType.TYPE})
//運(yùn)行時(shí)生效
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
//定義一個(gè)變量,可以接受參數(shù)
String desc() default " ";
}切面類代碼:
package com.example.myblog.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AOPAnnotationTest {
//定義切入點(diǎn)
@Pointcut("@annotation(com.example.myblog.test.MyAnnotation)")
public void aspectTest(){}
//前置通知,切入點(diǎn)執(zhí)行之前執(zhí)行
@Before("aspectTest()")
public void doBefore(JoinPoint joinPoint){
System.out.println("前置通知");
}
//后置通知,切入點(diǎn)執(zhí)行之后執(zhí)行
@After("aspectTest()")
public void doAfter(JoinPoint joinPoint){
System.out.println("后置通知");
}
//最終通知,,切入點(diǎn)執(zhí)行之后執(zhí)行
@AfterReturning("aspectTest()")
public void doAfterReturning(JoinPoint joinPoint){
System.out.println("最終通知");
}
//異常通知,切入點(diǎn)拋出異常執(zhí)行
@AfterThrowing("aspectTest()")
public void deAfterThrowing(JoinPoint joinPoint){
System.out.println("異常通知");
}
//環(huán)繞通知,切入點(diǎn)執(zhí)行前、后執(zhí)行
@Around("aspectTest()")
public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("未執(zhí)行");
Object result = joinPoint.proceed();
System.out.println("已執(zhí)行");
//返回結(jié)果
return result;
}
}
切點(diǎn)類代碼:
package com.example.myblog.test;
import org.springframework.stereotype.Component;
@Component
public class AOPAnnotationTestClient {
@MyAnnotation
public void test(){
System.out.println("正在測試AOP");
}
}測試類代碼:
@Test
public void testAOPAnnotation(){
aopAnnotationTestClient.test();
}測試結(jié)果:

到此這篇關(guān)于SpringBoot項(xiàng)目使用aop的文章就介紹到這了,更多相關(guān)SpringBoot使用aop內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring?@Conditional通過條件控制bean注冊(cè)過程
- 向Spring IOC 容器動(dòng)態(tài)注冊(cè)bean實(shí)現(xiàn)方式
- 解決Springboot全局異常處理與AOP日志處理中@AfterThrowing失效問題
- SpringBean和Controller實(shí)現(xiàn)動(dòng)態(tài)注冊(cè)與注銷過程詳細(xì)講解
- BeanDefinitionRegistryPostProcessor如何動(dòng)態(tài)注冊(cè)Bean到Spring
- Spring運(yùn)行時(shí)動(dòng)態(tài)注冊(cè)bean的方法
- spring動(dòng)態(tài)注冊(cè)bean?AOP失效原理解析
相關(guān)文章
SpringMVC異常全局捕獲與錯(cuò)誤響應(yīng)的處理方法
編程式異常處理是通過在代碼中?顯式編寫異常捕獲邏輯(如?try-catch?塊)來管理異常的方式,開發(fā)者需要手動(dòng)處理每一個(gè)可能拋出異常的代碼段,本文給大家介紹SpringMVC異常全局捕獲與錯(cuò)誤響應(yīng)的處理方法,感興趣的朋友一起看看吧2025-03-03
SpringBoot采用Dynamic-Datasource方式實(shí)現(xiàn)多JDBC數(shù)據(jù)源
在某些情況下,如果我們需要配置多個(gè)數(shù)據(jù)源,本文主要介紹了SpringBoot采用Dynamic-Datasource方式實(shí)現(xiàn)多JDBC數(shù)據(jù)源,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
關(guān)于JwtToken使用-重點(diǎn)看一下過期時(shí)間
這篇文章主要介紹了關(guān)于JwtToken使用-重點(diǎn)看一下過期時(shí)間,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Maven+SSM框架實(shí)現(xiàn)簡單的增刪改查
這篇文章主要介紹了Maven+SSM框架實(shí)現(xiàn)簡單的增刪改查,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
關(guān)于elcipse 安裝lombok插件解決 @Slf4j 等找不到log變量問題
這篇文章主要介紹了關(guān)于elcipse 安裝lombok插件解決 @Slf4j 等找不到log變量問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Java中ExecutorService和ThreadPoolExecutor運(yùn)行原理
本文主要介紹了Java中ExecutorService和ThreadPoolExecutor運(yùn)行原理,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
解決IDEA?JDK9沒有module-info.java的問題
這篇文章主要介紹了解決IDEA?JDK9沒有module-info.java的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Nginx+SpringCloud Gateway搭建項(xiàng)目訪問環(huán)境
本文主要介紹了Nginx+SpringCloud Gateway搭建項(xiàng)目訪問環(huán)境,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

