網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
對(duì)于AOP,我們明確的知道,是Spring的核心之一,這里,博主分享Spring實(shí)現(xiàn)AOP的三種方式。
準(zhǔn)備環(huán)境如下:
- 導(dǎo)入對(duì)應(yīng)依賴:
<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>
- 準(zhǔn)備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>
準(zhǔn)備一個(gè)模擬業(yè)務(wù)層service,其中包括UserService接口和UserServiceImpl實(shí)現(xiàn)類:
UserService接口:
package com.ara.service;
//模擬增刪查改的業(yè)務(wù)層
public interface UserService {
void add();
void delete();
void update();
void query();
}
UserServiceImpl實(shí)現(xiàn)類:
package com.ara.service;
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加了一個(gè)用戶");
}
public void delete() {
System.out.println("刪除了一個(gè)用戶");
}
public void update() {
System.out.println("修改了一個(gè)用戶");
}
public void query() {
System.out.println("查詢了一個(gè)用戶");
}
}
將業(yè)務(wù)實(shí)現(xiàn)的bean添加到Spring配置中:
<bean id="userService" class="com.ara.service.UserServiceImpl"/>
測(cè)試代碼:
@Test
public void springTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
測(cè)試代碼一直保持不變,就更能體現(xiàn)出AOP的思想了。
這樣直接調(diào)用的結(jié)果為:
現(xiàn)在的需求是:在不改動(dòng)上述代碼的前提下,我們調(diào)用其中的方法會(huì)打印出對(duì)應(yīng)的切入信息。
1. 方式一:使用原生Spring API接口
編寫切入類:
BeforeLog:
package com.ara.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
//方法前置通知 實(shí)現(xiàn)Spring框架的方法前置通知接口
public class BeforeLog implements MethodBeforeAdvice {
/**
* @param method 要執(zhí)行的目標(biāo)方法的對(duì)象
* @param args 參數(shù)列表
* @param target 目標(biāo)對(duì)象
* @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;
//方法前置通知 實(shí)現(xiàn)Spring框架的方法后置通知接口
public class AfterLog implements AfterReturningAdvice {
/**
* @param returnValue 返回值
* @param method 執(zhí)行了什么方法
* @param args 執(zhí)行方法的參數(shù)
* @param target 執(zhí)行的目標(biāo)
* @throws Throwable
*/
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("后置切入");
System.out.println(method.getName() + ":" + returnValue);
}
}
然后我們?cè)赟pring配置文件中配置切面:
<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>
配置好后,我們?cè)賹?duì)測(cè)試代碼進(jìn)行測(cè)試,發(fā)現(xiàn)結(jié)果如下:
發(fā)現(xiàn)得到了我們想要的結(jié)果。
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>
測(cè)試結(jié)果如下:
同樣也得到了我們想要的結(jié)果
3.方式三:使用注解方式
編寫切入類:
package com.ara.diy;
//方式三:使用注解方式實(shí)現(xiàn)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 //標(biāo)注這個(gè)類是一個(gè)切面
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()========");
}
//在環(huán)繞增強(qiáng)中,我們可以給定一個(gè)參數(shù) 代表我們要獲取處理切入的點(diǎn)(相當(dāng)于過(guò)濾器)
@Around("execution(* com.ara.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint point) throws Throwable {
System.out.println("========around-before()========");
System.out.println(point.getSignature());
//執(zhí)行:相當(dāng)于過(guò)濾器的放行
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/>
然后再對(duì)代碼進(jìn)行測(cè)試,結(jié)果如下:
這樣我們也能得到對(duì)應(yīng)的效果。
至于我們需要選擇哪種方式實(shí)現(xiàn),則根據(jù)實(shí)際需求選擇即可。
原文鏈接:https://blog.csdn.net/weixin_45935633/article/details/104830781
相關(guān)推薦
- 2022-02-02 怎么樣判斷頁(yè)面是否在iframe框架里
- 2022-04-14 ASP.NET?Core基礎(chǔ)之Main方法講解_基礎(chǔ)應(yīng)用
- 2022-11-27 深入了解Linux的文件權(quán)限_linux shell
- 2023-06-02 Hadoop部署的基礎(chǔ)設(shè)施操作詳解_服務(wù)器其它
- 2022-12-27 C++實(shí)現(xiàn)STL迭代器萃取的示例代碼_C 語(yǔ)言
- 2022-08-03 python中的三種注釋方法_python
- 2022-07-16 Spring Boot之處理各類請(qǐng)求和響應(yīng)
- 2022-11-13 ASP.NET?MVC使用Session會(huì)話保持表單狀態(tài)_實(shí)用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支