Spring Boot - 无法使用 aspectj 进行加载时间编织工作

标签 spring aop spring-boot load-time-weaving

谁能告诉我为什么使用 Spring Boot 时方面不会触发?我正在尝试使用 aspectj 设置加载时间编织,以便我可以建议私有(private)方法。

这是准系统项目的链接 - https://github.com/satb/spring_aop_test_project.git

使用“-javaagent:path/to/spring-instrument-4.1.0.RELEASE.jar”(或计算机上其他版本的 lib)运行“App”类并运行 curl 命令

curl -i http://localhost:8080/test-app/motd

MyAspect 类有一个在调用 MyService 的私有(private)方法时应该执行的通知。但在这种情况下,什么也没有发生。

但是,当应用程序启动时,我会看到如下消息:
[AppClassLoader@58644d46] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified.

我尝试按照这里的建议让它工作,但这没有帮助 - Using @Autowired with AspectJ and Springboot

最佳答案

好的,我快速浏览了您的 GitHub 项目。 POM 很奇怪,例如它不包含对 spring-instrument 的任何依赖。此外,您依赖于 aspectjweaver 1.8.2,但依赖于 aspectjrt 1.5.4。你真的应该为两者使用相同的版本。

无论如何,我在命令行上尝试了不同的 Java 代理,似乎仅使用 AspectJ weaver(结果:异常)或仅使用 Spring Instrument(结果:方面不起作用,正如您所描述的那样)是不够的。您需要同时使用两者:

java -javaagent:path/to/aspectjweaver-1.8.2.jar -javaagent:path/to/spring-instrument-4.1.0.RELEASE.jar ...

这适用于我的代码,并根据您的描述在使用 Curl 时在控制台上产生以下内容:

[AppClassLoader@58644d46] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.1.6.RELEASE)

2014-09-08 13:09:54.489  INFO 1464 --- [           main] App                                      : Starting App on Xander-PC with PID 1464 (C:\Users\Alexander\Documents\java-src\SO_AJ_SpringBootPrivilegedAspect\target\classes started by Alexander in C:\Users\Alexander\Documents\java-src\SO_AJ_SpringBootPrivilegedAspect)
2014-09-08 13:09:54.513  INFO 1464 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6771beb3: startup date [Mon Sep 08 13:09:54 CEST 2014]; root of context hierarchy
(...)
2014-09-08 13:09:56.257  INFO 1464 --- [           main] o.s.c.w.DefaultContextLoadTimeWeaver     : Found Spring's JVM agent for instrumentation
2014-09-08 13:09:56.259  INFO 1464 --- [           main] o.s.c.w.DefaultContextLoadTimeWeaver     : Found Spring's JVM agent for instrumentation
Aspect of called
(...)
2014-09-08 13:09:56.779  INFO 1464 --- [           main] App                                      : Started App in 2.531 seconds (JVM running for 3.067)
2014-09-08 13:09:59.115  INFO 1464 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2014-09-08 13:09:59.115  INFO 1464 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2014-09-08 13:09:59.122  INFO 1464 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 7 ms
Aspect of called
Advising getter

关于Spring Boot - 无法使用 aspectj 进行加载时间编织工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25714668/

相关文章:

java - Spring 中的服务器发送事件客户端示例

c# - 在 .net 中使用 AOP 登录

java - Spring AOP 和注释在方法执行之前检查参数

rest - Spring Boot RestClient post for object without request body 导致错误请求

java - OneToMany 关系上的 Spring-data-rest NullPointerException

java - 运行Spring项目时出现空指针异常

dependency-injection - 依赖注入(inject) - 谁拥有接口(interface)?

java - 如何使用 JUnit 5 在 Spring Boot 中捕获 Hibernate ConstraintViolationException(或 Spring DataIntegrityViolationException?)

java - 如何在 Spring Boot 应用程序中实现安全性?

spring - 为什么在Spring MVC应用程序中创建两个Spring bean Controller 实例?