Spring?this調(diào)用當(dāng)前類方法無法攔截的示例代碼
先給出代碼示例
package com.example.demo.service;
import org.springframework.stereotype.Service;
@Service
public class ProxyService {
public void testA(){
System.out.println("進(jìn)入A");
this.testB();
}
public void testB() {
System.out.println("進(jìn)入b");
}
}package com.example.demo.annotation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AspectjTest {
@Around("execution(* com.example.demo.service.ProxyService.testB())")
public void recordProxy(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
joinPoint.proceed();
long end = System.currentTimeMillis();
System.out.println("花費(fèi)時(shí)間:"+(end-start));
}
}package com.example.demo.api;
import com.example.demo.service.ProxyService;
import com.example.demo.service.UserService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class ProxyApi {
// @Autowired
// ProxyService proxyService1;
@Autowired
private ApplicationContext applicationContext;
@PostMapping("/proxy")
public String test1() {
ProxyService proxyService1 = applicationContext.getBean(ProxyService.class);;
proxyService1.testA();
return "string";
}
}運(yùn)行上面的代碼會發(fā)現(xiàn) 配置aop 攔截方法不會被執(zhí)行

我們通過debug 查看這個(gè)proxyService1 和this的區(qū)別,看看他們的值是什么


發(fā)現(xiàn)不一樣,其實(shí)這就是問題的原因。
1、當(dāng)我們在aop配置攔截的時(shí)候會指定類下面的方法路徑,在spring啟動(dòng)的時(shí)候會先去加載這個(gè)ProxyService類,生成一個(gè)bean,但是因?yàn)槟阌胊op配置了,所以需要代理這個(gè)ProxyService類,所以最終存在spring容器中的bean對象就是被代理后的bean對象。所以,我們在用容器獲取bean或者用依賴注入獲取bean的地址路徑顯示的是被代理后的bean 。
2、this獲取的當(dāng)前對象方法的一個(gè)引用,所以在調(diào)用testB方法的時(shí)候用的不是被代理的對象,自熱不會經(jīng)過aop攔截,原理和我們使用普通動(dòng)態(tài)代理一樣,只能是代理對象才能走自定義的方法。
3、可以通過debug 查看當(dāng)ProxyService類被代理前和后的zhi值


發(fā)現(xiàn)是和之前的debug截圖里面的值相符合的哈。
解決方法,就是在調(diào)用testB方法的時(shí)候用spring容器里的bean對象
@Service
public class ProxyService {
@Autowired
private ProxyService proxyService;
public void testA(){
System.out.println("進(jìn)入A");
proxyService.testB();
}
public void testB() {
System.out.println("進(jìn)入b");
}或者
@Service
public class ProxyService {
public void testA(){
System.out.println("進(jìn)入A");
ProxyService currentProxy = (ProxyService) AopContext.currentProxy();
currentProxy.testB();
}
public void testB() {
System.out.println("進(jìn)入b");
}
}最終結(jié)果正確

到此這篇關(guān)于Spring this調(diào)用當(dāng)前類方法無法攔截的文章就介紹到這了,更多相關(guān)Spring this無法攔截內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis實(shí)現(xiàn)分頁查詢的詳細(xì)流程
這篇文章主要給大家介紹了關(guān)于Mybatis實(shí)現(xiàn)分頁查詢的詳細(xì)流程,MyBatis是支持普通SQL查詢,存儲過程和高級映射的優(yōu)秀持久層框架,需要的朋友可以參考下2023-08-08
關(guān)于HashMap相同key累加value的問題
這篇文章主要介紹了關(guān)于HashMap相同key累加value的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
java生成json實(shí)現(xiàn)隱藏掉關(guān)鍵屬性
這篇文章主要介紹了java生成json實(shí)現(xiàn)隱藏掉關(guān)鍵屬性,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot如何讀取mock數(shù)據(jù)(高效調(diào)試接口)
本文介紹如何在SpringBoot項(xiàng)目中讀取resources目錄下的mock數(shù)據(jù)文件,以便高效調(diào)試接口,在 Spring Boot 項(xiàng)目中,通常會將靜態(tài)資源或配置文件放在 src/main/resources 目錄下,下面通過實(shí)例給大家詳細(xì)介紹,感興趣的朋友一起看看吧2024-12-12
spring+Jpa多數(shù)據(jù)源配置的方法示例
這篇文章主要介紹了spring+Jpa多數(shù)據(jù)源配置的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08

