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

时间:2014-09-07 20:50:04

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

有人可以告诉我为什么使用弹簧靴时方面不会闪光?我正在尝试使用aspectj设置加载时间编织,以便我可以建议私有方法。

以下是准系统项目的链接 - 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的私有方法时执行。但在这种情况下,没有任何反应。

当应用程序启动时,我会看到如下消息:

[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

2 个答案:

答案 0 :(得分:4)

好的,我快速浏览了一下你的GitHub项目。 POM非常奇怪,例如它不包含任何对弹簧乐器的依赖。此外,您依赖于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

答案 1 :(得分:4)

尝试声明一个InstrumentationLoadTimeWeaver bean而不是显式使用-javaagent:/path/to/org.springframework.instrument- {version} .jar。根据文件

要使用它,您必须通过提供以下JVM选项来启动具有Spring代理的虚拟机:

-javaagent:/path/to/org.springframework.instrument- {版本}的.jar

请注意,这需要修改VM启动脚本,这可能会阻止您在应用程序服务器环境中使用它(具体取决于您的操作策略)。此外,JDK代理将检测整个VM,这可能证明是昂贵的。

我希望以下方式做得更好。

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

xml配置中也可以这样做。

找到一个新的library,它可以解决动态设置spring InstrumentationLoadTimeWeaver以支持方面而无需使用显式java代理启动JVM

<dependency>
    <groupId>de.invesdwin</groupId>
    <artifactId>invesdwin-instrument</artifactId>
    <version>1.0.2</version>
</dependency>

Spring boot config

@SpringBootApplication
/** 
 * Make @Configurable work via @EnableLoadTimeWeaving.
 * If it does not work, alternatively you can try: 
 * @ImportResource(locations = "classpath:/META-INF/ctx.spring.weaving.xml") 
 */
@EnableLoadTimeWeaving
public class MySpringBootApplication {
    public static void main(final String[] args) {
        DynamicInstrumentationLoader.waitForInitialized(); //dynamically attach java agent to jvm if not already present
        DynamicInstrumentationLoader.initLoadTimeWeavingContext(); //weave all classes before they are loaded as beans
        SpringApplication.run(MySpringBootApplication.class, args); //start application, load some classes
    }
}