电竞比分网-中国电竞赛事及体育赛事平台

分享

動態(tài)代理方法互調(diào),靜態(tài)成員類

 頭號碼甲 2020-05-31

在日常使用,還有面試中,經(jīng)常會涉及到AOP的相關知識,AOP雖好,但是有一些小的細節(jié)注意不到,可能會被坑;

1.動態(tài)代理類的方法互調(diào),被調(diào)用的方法會不會生成代理?

2.靜態(tài)成員類,調(diào)用過程中會不會生成代理?

對以上這些問題進行測試:

首頁,這是AOP切面代碼

@Component
@Aspect
public class AopTest {

    @Pointcut("execution(public * com.wuzhiaite.javaweb.spring.serveice.AopServiceTest.*(..))")
    public void AopTest(){ }
@Before(
"AopTest()") public void doBefore(){ System.out.println("doBefore"); }
@After(
"AopTest()") public void doAfter(){ System.out.println("doAfter"); } }

 接下來:被代理類的代碼

@Service
public class AopServiceTest {

    public void testMethod(){
        new Inner().innerMethod();
        System.out.println("testMethod");
        this.testMethod2();
    }
    public void testMethod2(){
        System.out.println("testMethod22222222");
    }

    public static class Inner{
        public void innerMethod(){
            System.out.println("innerMethod");
        }
    }

}

再接下來,測試以下:

@RunWith(SpringJUnit4ClassRunner.class)
@Slf4j
@EnableAspectJAutoProxy
public class SpringBootJavawebBaseApplicationTests {

    @Autowired
    private AopServiceTest service;

    @Test
    public void aopTest1(){
        service.testMethod();
        new AopServiceTest.Inner().innerMethod();//靜態(tài)內(nèi)部類并不會生成代理類
    }
}

測試出來的結果是:

 

 很顯然,靜態(tài)成員類和方法內(nèi)部調(diào)用的方法并沒有生成代理,至于原因,后面康康源碼再補充

 

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多