AspectJ - 解析定义失败 - NullPointerException

时间:2017-03-20 14:41:32

标签: java eclipse junit aspectj aspect

此处MVCE https://github.com/yami12376/AspectJ

  1. 在我的案例中将VM arguments添加到Run/Debug configuration of Junit test

    -javaagent:C:\ aspectjWeaver \弹簧仪器3.0.4.jar

    -javaagent:C:\ aspectjWeaver \ aspectjweaver-1.6.11.jar

  2. 在我的情况下,我在JAVA build path \target\classes\META-INF中添加了src/main/resources

  3. 运行Junit test

  4. 如果您删除aop.xml,它仍在编织type org.*

    但另一方面aop.xml不应该编织它,因为它具有: <include within="com.*"/> 为什么它会编织com.*以外的其他内容?

    最后,我希望在根据MyAspect

    运行Junit test时调用@Around("execution(* *(..))")

    正如你现在所看到的,它没有被调用。

    我的榜样基于 https://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-aj-ltw-first-example

    运行JUnit test时,我看到此错误:

    http://wklej.org/id/3066582/

     [AppClassLoader@18b4aac2] warning parse definitions failed -- (NullPointerException) null
    null
    java.lang.NullPointerException
        at org.aspectj.weaver.loadtime.definition.DocumentParser.resolveEntity(DocumentParser.java:177)
        at org.apache.xerces.util.EntityResolverWrapper.resolveEntity(Unknown Source)
        at org.apache.xerces.impl.XMLEntityManager.resolveEntity(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at org.aspectj.weaver.loadtime.definition.DocumentParser.saxParsing(DocumentParser.java:158)
        at org.aspectj.weaver.loadtime.definition.DocumentParser.parse(DocumentParser.java:123)
        at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.parseDefinitions(ClassLoaderWeavingAdaptor.java:272)
        at org.aspectj.weaver.loadtime.DefaultWeavingContext.getDefinitions(DefaultWeavingContext.java:130)
        at org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptor.initialize(ClassLoaderWeavingAdaptor.java:156)
        at org.aspectj.weaver.loadtime.Aj$ExplicitlyInitializedClassLoaderWeavingAdaptor.initialize(Aj.java:340)
        at org.aspectj.weaver.loadtime.Aj$ExplicitlyInitializedClassLoaderWeavingAdaptor.getWeavingAdaptor(Aj.java:345)
        at org.aspectj.weaver.loadtime.Aj$WeaverContainer.getWeaver(Aj.java:319)
        at org.aspectj.weaver.loadtime.Aj.preProcess(Aj.java:113)
        at org.aspectj.weaver.loadtime.ClassPreProcessorAgentAdapter.transform(ClassPreProcessorAgentAdapter.java:54)
        at sun.instrument.TransformerManager.transform(Unknown Source)
        at sun.instrument.InstrumentationImpl.transform(Unknown Source)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
    

    我想解决此错误

1 个答案:

答案 0 :(得分:1)

嗯,你的项目中有几个奇怪的问题和错误:

  1. 显然,您的POJO应用程序不使用Spring。因此,您可以在命令行中删除Spring依赖项以及spring-instrument.jar。您也不需要core-context.xml
  2. 就我所见,也不需要Xerces。
  3. 如果使用aspectjweaver.jar作为依赖项,aspectjrt.jar是多余的,因为前者是超集,因此包含后者。
  4. aop.xml中有一个包名称拼写错误:请在方面名称中使用com.mkyong,而不是com.mykong。否则永远不会找到方面。
  5. include within标记中,您应使用..语法,以便也可以包含/排除子包。
  6. 您的方面类总是一团糟:

    • 您没有明显的理由使用嵌套类。
    • 您在建议签名中使用@Around,但JoinPoint代替ProceedingJoinPoint
    • 您使用@Around,但从不致电proceed()
  7. 那怎么样?

    <强>的pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.mkyong</groupId>
      <artifactId>NumberGenerator</artifactId>
      <version>1.0-SNAPSHOT</version>
    
      <name>NumberGenerator</name>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.8.10</version>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
              <source>1.7</source>
              <target>1.7</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    
    </project>
    

    <强> aop.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <aspectj>
      <aspects>
        <aspect name="com.mkyong.MyAspect"/>
      </aspects>
      <weaver options="-verbose -showWeaveInfo">
        <include within="com.mkyong..*"/>
        <exclude within="org.jibx*..*"/>
      </weaver>
    </aspectj>
    

    <强>方面:

    package com.mkyong;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    
    @Aspect
    public class MyAspect {
      @Around("execution(!static * *(..))")
      public Object dontLogDuplicates(ProceedingJoinPoint thisJoinPoint) throws Throwable {
        System.out.println(thisJoinPoint);
        return thisJoinPoint.proceed();
      }
    }
    

    控制台日志:

    在IntelliJ IDEA中将-javaagent:/path/to/aspectjweaver.jar添加到我的JUnit运行配置时,我看到以下内容:

    [AppClassLoader@18b4aac2] info AspectJ Weaver Version 1.8.10 built on Monday Dec 12, 2016 at 19:07:48 GMT
    [AppClassLoader@18b4aac2] info register classloader sun.misc.Launcher$AppClassLoader@18b4aac2
    [AppClassLoader@18b4aac2] info using configuration /C:/Users/Alexander/Documents/java-src/yami12376-AspectJ/target/classes/META-INF/aop.xml
    [AppClassLoader@18b4aac2] info register aspect com.mkyong.MyAspect
    [AppClassLoader@18b4aac2] weaveinfo Join point 'method-execution(void com.mkyong.AppTest.testLengthOfTheUniqueKey())' in Type 'com.mkyong.AppTest' (AppTest.java:9) advised by around advice from 'com.mkyong.MyAspect' (MyAspect.java)
    execution(void com.mkyong.AppTest.testLengthOfTheUniqueKey())
    [AppClassLoader@18b4aac2] weaveinfo Join point 'method-execution(java.lang.String com.mkyong.App.generateUniqueKey())' in Type 'com.mkyong.App' (App.java:12) advised by around advice from 'com.mkyong.MyAspect' (MyAspect.java)
    execution(String com.mkyong.App.generateUniqueKey())
    

    但实际上,你应该修复你的POM,并确保从那里完成方面编织,而不是手动完成。这真的很难看。

    更新:在经过所有批评后,我还要说一个积极的事情:你提供了一个MCVE,谢谢你。使用代码片段,问题和答案,我们永远不会在您的文件中发现许多错误。因此,创建GitHub项目是一个聪明而谨慎的决定。我很容易发现问题。 :-)如果每个人都这样做,我会在这里节省很多时间。