@EnableLoadTimeWeaving在Spring Boot中引发异常

时间:2018-12-03 00:10:46

标签: maven spring-boot aop

我在Spring Boot中使用@EnableLoadTimeWeaving,但在服务器启动时却遇到Maven异常

 °ERROR§ Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.3.RELEASE:run (default-cli) on project api-server: Could not exec java: Application finished with exit code: 1 -> °Help 1§
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.3.RELEASE:run (default-cli) on project api-server: Could not exec java
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)

我的Spring Boot配置如下

@EnableAsync(proxyTargetClass = true)
@EnableSpringConfigured
@EnableLoadTimeWeaving
@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@ComponentScan(
  basePackageClasses = {io.skyledge.incontrol.Application.class},
  lazyInit = true
)

和Maven依赖关系如下

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.8.7</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.7</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-instrument</artifactId>
  <version>5.1.3.RELEASE</version>
</dependency>

1 个答案:

答案 0 :(得分:0)

您的主要问题是您应该使用 AspectJ weaver作为Java代理来运行应用程序。如果还将Spring-Instrument添加为第二个Java代理,则可以避免将weaver添加到应用程序中类作为显式的bean,Spring会处理它。由于必须修改命令行,因此只需添加两个代理即可:

-javaagent:/path/to/spring-instrument-5.0.9.RELEASE.jar -javaagent:/path/to/aspectjweaver-1.8.13.jar

实际上,您也不需要@EnableAspectJAutoProxy(尽管名称仅用于Spring AOP),因为您已经使用@EnableLoadTimeWeaving(对于AspectJ LTW)。

为了避免以后出现有关“圆形视图路径”的错误消息,您还应该在问候方法中添加一个@ResponseBody批注:

public @ResponseBody String greeting(...)

我还建议添加资源文件 META-INF / aop.xml ,以避免由于将方面应用于各种Spring和3rd party代码而导致大量错误消息能够更准确地告诉AspectJ编织什么,记录什么等等。在您的项目中使用此配置...

<?xml version="1.0"?>

<!-- AspectJ load-time weaving config file with Spring aspects -->
<aspectj>

  <!--<weaver options="-verbose -showWeaveInfo -Xlint:ignore">-->
  <weaver options="-showWeaveInfo -Xlint:ignore">
    <include within="example..*"/>
  </weaver>

  <!-- It also works if you omit this section -->
  <aspects>
    <aspect name="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"/>
    <!--
    <aspect name="org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect"/>
    <aspect name="org.springframework.transaction.aspectj.AnnotationTransactionAspect"/>
    <aspect name="org.springframework.cache.aspectj.AnnotationCacheAspect"/>
    -->
  </aspects>

</aspectj>

...我正在得到这种日志输出:

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

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

2018-12-16 09:54:23.804  INFO 5784 --- [           main] example.Application                      : Starting Application on Xander-Ultrabook with PID 5784 (C:\Users\alexa\Documents\java-src\SO_AJ_SpringConfigurableBeans\target\classes started by alexa in C:\Users\alexa\Documents\java-src\SO_AJ_SpringConfigurableBeans)
2018-12-16 09:54:23.804  INFO 5784 --- [           main] example.Application                      : No active profile set, falling back to default profiles: default
2018-12-16 09:54:23.867  INFO 5784 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7c83dc97: startup date [Sun Dec 16 09:54:23 ICT 2018]; root of context hierarchy
2018-12-16 09:54:24.659  INFO 5784 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-12-16 09:54:24.674  INFO 5784 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-12-16 09:54:24.674  INFO 5784 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
(...)
2018-12-16 09:54:25.049  INFO 5784 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/greeting],methods=[GET]}" onto public java.lang.String example.GreetingController.greeting(java.lang.String,org.springframework.ui.Model)
(...)
2018-12-16 09:54:25.315  INFO 5784 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-12-16 09:54:25.315  INFO 5784 --- [           main] example.Application                      : Started Application in 1.848 seconds (JVM running for 3.272)
2018-12-16 10:10:39.050  INFO 5784 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-12-16 10:10:39.051  INFO 5784 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-12-16 10:10:39.066  INFO 5784 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms
[AppClassLoader@18b4aac2] weaveinfo Extending interface set for type 'example.PrintDelegator' (PrintDelegator.java) to include 'org.springframework.beans.factory.aspectj.ConfigurableObject' (AnnotationBeanConfigurerAspect.aj)
[AppClassLoader@18b4aac2] weaveinfo Join point 'initialization(void org.springframework.beans.factory.aspectj.ConfigurableObject.<init>())' in Type 'example.PrintDelegator' (PrintDelegator.java:7) advised by before advice from 'org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect' (AbstractDependencyInjectionAspect.aj:77) [with runtime test]
[AppClassLoader@18b4aac2] weaveinfo Join point 'initialization(void org.springframework.beans.factory.aspectj.ConfigurableObject.<init>())' in Type 'example.PrintDelegator' (PrintDelegator.java:7) advised by afterReturning advice from 'org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect' (AbstractDependencyInjectionAspect.aj:86) [with runtime test]
[AppClassLoader@18b4aac2] weaveinfo Join point 'initialization(void example.PrintDelegator.<init>())' in Type 'example.PrintDelegator' (PrintDelegator.java:7) advised by afterReturning advice from 'org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect' (AbstractDependencyInjectionAspect.aj:86) [with runtime test]
test