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

Spring Boot中AspectJ加载时织入遇NoSuchMethodException(aspectOf)求助

Spring Boot加载时织入(Load Time Weaving)配置问题与解决

配置详情

按照Spring LTW文档配置Spring Boot应用的加载时织入,具体配置如下:

依赖配置(pom.xml)

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-instrument</artifactId>
</dependency>

aop.xml配置(resources/META-INF/aop.xml)

<!DOCTYPE aspectj PUBLIC
    "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver options="-verbose -showWeaveInfo">
        <include within="com.tosan.http.server.starter.logger.ServiceLogger"/>
    </weaver>
<aspects>
    <aspect name="com.sample.demo.aspect.TestAspect"/>
</aspects>

Spring配置类

@Configuration
@EnableLoadTimeWeaving()
public class DemoConfig {

   @Bean
   public TestAspect testAspect() {
       TestAspect testAspect = Aspects.aspectOf(TestAspect.class);
       return testAspect;
   }
}

启动JavaAgent参数

-javaagent:\aspectjweaver.jar 
-javaagent:\spring-instrument-5.3.23.jar

启动报错

应用启动时抛出NoSuchMethodException异常:

[AppClassLoader@368239c8] weaveinfo Join point 'method-execution(java.lang.Object 
com.tosan.http.server.starter.logger.ServiceLogger.log(org.aspectj.lang.ProceedingJoinPoint))' 
in Type 'com.tosan.http.server.starter.logger.ServiceLogger' (ServiceLogger.java:17) advised 
by around advice from 'com.sample.demo.aspect.TestAspect' (TestAspect.java)
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'testAspect' defined in class path resource [com/sample/demo/DemoConfig.class]: Bean 
instantiation via factory method failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[com.sample.demo.aspect.TestAspect]: Factory method 'testAspect' threw exception; nested 
exception is org.aspectj.lang.NoAspectBoundException: Exception while initializing 
com.sample.demo.aspect.TestAspect: java.lang.NoSuchMethodException: 
com.sample.demo.aspect.TestAspect.aspectOf()
at 

其他尝试方案

  • 添加ajc编译器(使用com.nickwongdev插件):应用可正常启动,但切面会执行两次
  • 仅使用ajc编译器+aspectjWeaver Agent:出现找不到Spring特定织入类的错误
  • 仅使用ajc编译器+Spring织入Agent:无报错,但切面不执行

解决方法

  1. 在aop.xml的weaver节点中添加切面类的include规则:
<include within="com.sample.demo.aspect.TestAspect"/>
  1. 移除@EnableLoadTimeWeaving注解不影响加载时织入的生效,该注解仅用于强制运行时存在Spring织入Agent。

内容的提问来源于stack exchange,提问作者Mina Kh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 18:30:45