網站首頁 編程語言 正文
對于AOP,我們明確的知道,是Spring的核心之一,這里,博主分享Spring實現AOP的三種方式。
準備環境如下:
- 導入對應依賴:
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.5version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
- 準備Spring的配置文件,applicationContext.xml,一定要引入AOP的約束
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
beans>
準備一個模擬業務層service,其中包括UserService接口和UserServiceImpl實現類:
UserService接口:
package com.ara.service;
//模擬增刪查改的業務層
public interface UserService {
void add();
void delete();
void update();
void query();
}
UserServiceImpl實現類:
package com.ara.service;
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加了一個用戶");
}
public void delete() {
System.out.println("刪除了一個用戶");
}
public void update() {
System.out.println("修改了一個用戶");
}
public void query() {
System.out.println("查詢了一個用戶");
}
}
將業務實現的bean添加到Spring配置中:
<bean id="userService" class="com.ara.service.UserServiceImpl"/>
測試代碼:
@Test
public void springTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
測試代碼一直保持不變,就更能體現出AOP的思想了。
這樣直接調用的結果為:
現在的需求是:在不改動上述代碼的前提下,我們調用其中的方法會打印出對應的切入信息。
1. 方式一:使用原生Spring API接口
編寫切入類:
BeforeLog:
package com.ara.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
//方法前置通知 實現Spring框架的方法前置通知接口
public class BeforeLog implements MethodBeforeAdvice {
/**
* @param method 要執行的目標方法的對象
* @param args 參數列表
* @param target 目標對象
* @throws Throwable
*/
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置切入");
System.out.println(target.getClass().getName() + "-" + method.getName());
}
}
AfterLog:
package com.ara.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
//方法前置通知 實現Spring框架的方法后置通知接口
public class AfterLog implements AfterReturningAdvice {
/**
* @param returnValue 返回值
* @param method 執行了什么方法
* @param args 執行方法的參數
* @param target 執行的目標
* @throws Throwable
*/
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("后置切入");
System.out.println(method.getName() + ":" + returnValue);
}
}
然后我們在Spring配置文件中配置切面:
<bean id="beforeLog" class="com.ara.log.BeforeLog"/>
<bean id="afterLog" class="com.ara.log.AfterLog"/>
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.ara.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointCut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointCut"/>
aop:config>
配置好后,我們再對測試代碼進行測試,發現結果如下:
發現得到了我們想要的結果。
2.方式二:自定義類
編寫切入代碼:
package com.ara.diy;
public class DiyPointCut {
public void before(){
System.out.println("========before()========");
}
public void after(){
System.out.println("========after()========");
}
}
在Spring配置文件中配置:
<bean id="diy" class="com.ara.diy.DiyPointCut"/>
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="point" expression="execution(* com.ara.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
aop:aspect>
aop:config>
測試結果如下:
同樣也得到了我們想要的結果
3.方式三:使用注解方式
編寫切入類:
package com.ara.diy;
//方式三:使用注解方式實現AOP
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect //標注這個類是一個切面
public class AnnotationPointCut {
//前置通知
@Before("execution(* com.ara.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("========before()========");
}
//后置通知
@After("execution(* com.ara.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("========after()========");
}
//在環繞增強中,我們可以給定一個參數 代表我們要獲取處理切入的點(相當于過濾器)
@Around("execution(* com.ara.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint point) throws Throwable {
System.out.println("========around-before()========");
System.out.println(point.getSignature());
//執行:相當于過濾器的放行
Object proceed = point.proceed();
System.out.println("========around-after()========");
System.out.println(proceed);
}
}
在Spring配置文件中添加:
<bean id="annotationPointCut" class="com.ara.diy.AnnotationPointCut" />
<aop:aspectj-autoproxy/>
然后再對代碼進行測試,結果如下:
這樣我們也能得到對應的效果。
至于我們需要選擇哪種方式實現,則根據實際需求選擇即可。
原文鏈接:https://blog.csdn.net/weixin_45935633/article/details/104830781
- 上一篇:SpringBoot配置文件簡介
- 下一篇:Kafka 認證登錄注意事項
相關推薦
- 2023-05-07 Python3中省略號(...)用法介紹_python
- 2023-02-15 Nginx如何根據前綴路徑轉發到不同的Flask服務_nginx
- 2022-04-22 wampserver You don‘t have permission to access / o
- 2024-04-07 Linux如何查看當前占用CPU和內存最多的進程
- 2022-07-13 Win系統服務器管理器打開方式
- 2023-06-20 Jupyter?Notebook中%time和%timeit的使用詳解_python
- 2022-04-19 python?tkinter實現彈窗的輸入輸出_python
- 2023-01-17 React?Hooks核心原理深入分析講解_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支