温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring使用<context:load-time-weaver/>实现静态代理所遇到的问题

发布时间:2021-09-29 16:55:00 来源:亿速云 阅读:233 作者:iii 栏目:大数据

本篇内容主要讲解“spring使用<context:load-time-weaver/>实现静态代理所遇到的问题”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“spring使用<context:load-time-weaver/>实现静态代理所遇到的问题”吧!

第一步:

创建要实现静态的类,以及Advice增强类实现,内容如下:

需要静态代理的类:

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

Advice增强类:

@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(没有则创建)文件夹下建立aop.xml,内容如下

<!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,内容如下:

<?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>

第四步:

编写测试类,内容如下:

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/>实现静态代理所遇到的问题

第六步:

运行中遇到的问题

问题1:出现了一个java.lang.VerifyError: Expecting a stackmap frame at branch target 7错误

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

spring使用<context:load-time-weaver/>实现静态代理所遇到的问题

问题2:circular advice precedence错误

解决方法:

原因Advice增强器AspectTest,必须要按照@Before->@Around->@After编写代码,上面代码调整顺利即可。但是在spring动态代理没有该顺序不对,不会抛异常。

第七步:

我们可以看到正常的静态类代理结果如下:

spring使用<context:load-time-weaver/>实现静态代理所遇到的问题

到此,相信大家对“spring使用<context:load-time-weaver/>实现静态代理所遇到的问题”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI