You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring AOP加载时织入(LTW)失效,请求排查问题原因

Spring AOP加载时织入(LTW)未生效排查

我正在开发一个实验性程序,尝试使用Spring AOP的加载时织入(LTW)将切面织入目标类,但织入操作未生效。当前使用Java 17和IntelliJ IDE开发,相关代码可查看GitHub仓库。

切面类代码

@Aspect
@Component
public class MonitoringAspect {

    @Pointcut("execution(* com.sj.utilities.Watch.*(..))")
    public void monitoringOperation(){}

    @Around("monitoringOperation()")
    public Object aroundAdvice(ProceedingJoinPoint pjp)
            throws Throwable {
        System.out.println("Before " + pjp.getSignature() + " execution!");

        long startTime = System.nanoTime();
        Object retVal = pjp.proceed();
        long endTime = System.nanoTime();
        long executingTime = endTime - startTime;
        System.out.println("Method's execution time:\n"
                + "\t" + executingTime + " ns\n"
                + "\t" + (executingTime / 1000) + " μs\n"
                + "\t" + (executingTime / 1000000) + " ms\n"
                + "\t" + (executingTime / 1000000000) + " s");
        System.out.println("After " + pjp.getSignature() + " execution!");

        return retVal;
    }

}

曾尝试移除@Component注解(因为切面Bean并非必需),但问题依旧。切面要增强的是实现了FictiveWatch接口的Watch类中的sleeping方法(该接口仅用于Spring使用JDK动态代理)。

Java注解配置类

@Configuration
//@EnableAspectJAutoProxy
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
@ComponentScan("com.sj")
public class AOPConfig implements LoadTimeWeavingConfigurer {

    @Override
    public LoadTimeWeaver getLoadTimeWeaver() {
        return new InstrumentationLoadTimeWeaver();
    }

    @Bean
    public InstrumentationLoadTimeWeaver loadTimeWeaver()  throws Throwable {
        return new InstrumentationLoadTimeWeaver();
    }

}

XML配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans <!-- namespaces ...-->

    <!-- this switches on the load-time weaving -->
    <context:load-time-weaver
        weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>

    <!-- this switches on the spring aop autoproxy -->
    <!--<aop:aspectj-autoproxy/>-->

    <!-- a service object; we will be profiling its methods -->
    <bean id="watch"
          class="com.sj.utilities.Watch"/>
    <bean id="monitoringAspect"
          class="com.sj.aspects.MonitoringAspect"/>

</beans>

META-INF/aop.xml文件

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "https://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver
            options="-verbose -showWeaveInfo">
        <!-- only weave classes in our application-specific packages -->
        <include within="com.sj..*"/>
    </weaver>
    <aspects>
        <!-- weave in just this aspect -->
        <aspect name="com.sj.aspects.MonitoringAspect"/>
    </aspects>
</aspectj>

驱动类代码

public class Main {
    public static void main(String[] args) throws InterruptedException {
        //Check used JVM arguments
        System.out.println(ManagementFactory.getRuntimeMXBean().getInputArguments());

        //Java class LTW config
        //ApplicationContext context = new AnnotationConfigApplicationContext(AOPConfig.class);

        //XML LTW config
        ApplicationContext context = new ClassPathXmlApplicationContext("config/aop-xml-conf.xml");
        Watch watch = context.getBean(Watch.class);
        watch.sleeping(3000);
    }
}

当前问题现象

  • 无论使用注解配置还是XML配置启用LTW,织入均未生效,横切逻辑没有执行
  • 打印的当前JVM参数:
[--add-opens=java.base/java.lang=ALL-UNNAMED, -Dspring.aop.proxy-target-class=false, -Dorg.aspectj.weaver.loadtime.configuration=trace, -Dorg.aspectj.weaver.loadtime.verbose=true, -javaagent:E:\intelliJIdea_dir\IntelliJ IDEA Community Edition 2022.2.1\lib\idea_rt.jar=55606:E:\intelliJIdea_dir\IntelliJ IDEA Community Edition 2022.2.1\bin, -Dfile.encoding=UTF-8]
  • 程序运行后仅输出Watch类sleeping方法的原生结果:
Slept 3000 ms

已参照Spring官方LTW文档及相关问题解答核对配置,未发现明显错误,但织入仍未生效,请问问题出在哪部分?


内容的提问来源于stack exchange,提问作者Sergejs Jevstigņejevs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 01:01:10