溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

spring使用<context:load-time-weaver/>實(shí)現(xiàn)靜態(tài)代理所遇到的問題

發(fā)布時間:2021-09-29 16:55:00 來源:億速云 閱讀:324 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“spring使用<context:load-time-weaver/>實(shí)現(xiàn)靜態(tài)代理所遇到的問題”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“spring使用<context:load-time-weaver/>實(shí)現(xiàn)靜態(tài)代理所遇到的問題”吧!

第一步:

創(chuàng)建要實(shí)現(xiàn)靜態(tài)的類,以及Advice增強(qiáng)類實(shí)現(xiàn),內(nèi)容如下:

需要靜態(tài)代理的類:

public interface IITestBean {
    void test();
}
public class TestBean implements IITestBean {
    @Override
    public void test() {
        System.out.println("test");
    }
}

Advice增強(qiáng)類:

@Aspect
public class AspectTest {

    @Pointcut("execution(* *.test(..))")
    public void test() {
        System.out.println("我切入了");
    }

    @Before("test()")
    public void beforeTest() {
        System.out.println("beforeTest()");
    }
    
    @After("test()")
    public void afterTest() {
        System.out.println("afterTest()");
    }

    @Around("test()")
    public Object aroundTest(ProceedingJoinPoint p) {
        System.out.println("before1");
        Object o = null;
        try {
            o = p.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("after1");
        return o;
    }
}

第二步:

在class目錄下的META-INF(沒有則創(chuàng)建)文件夾下建立aop.xml,內(nèi)容如下

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver>
        <include within="com.zzx.study.aspect.*"/>
    </weaver>

    <aspects>
        <aspect name="com.zzx.study.aspect.AspectTest"/>
    </aspects>
</aspectj>

第三步:

編寫spring的配置spring-aspect.xml,內(nèi)容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="test" class="com.zzx.study.aspect.TestBean"/>
    <context:load-time-weaver/>
</beans>

第四步:

編寫測試類,內(nèi)容如下:

public class AspectTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-aspect.xml");
        TestBean bean = (TestBean)context.getBean("test");
        bean.test();
    }
}

第五步:

測試時,需下載并引入org.springframework.instrument.jar文件,在idea中配置如下:

spring使用<context:load-time-weaver/>實(shí)現(xiàn)靜態(tài)代理所遇到的問題

第六步:

運(yùn)行中遇到的問題

問題1:出現(xiàn)了一個java.lang.VerifyError: Expecting a stackmap frame at branch target 7錯誤

解決方法:idea中VM option,需加入-XX:-UseSplitVerifier

spring使用<context:load-time-weaver/>實(shí)現(xiàn)靜態(tài)代理所遇到的問題

問題2:circular advice precedence錯誤

解決方法:

原因Advice增強(qiáng)器AspectTest,必須要按照@Before->@Around->@After編寫代碼,上面代碼調(diào)整順利即可。但是在spring動態(tài)代理沒有該順序不對,不會拋異常。

第七步:

我們可以看到正常的靜態(tài)類代理結(jié)果如下:

spring使用<context:load-time-weaver/>實(shí)現(xiàn)靜態(tài)代理所遇到的問題

到此,相信大家對“spring使用<context:load-time-weaver/>實(shí)現(xiàn)靜態(tài)代理所遇到的問題”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI