gpt4 book ai didi

java - AspectJ - 解析定义失败 - NullPointerException

转载 作者:行者123 更新时间:2023-12-02 13:37:34 36 4
gpt4 key购买 nike

这是 MVCE https://github.com/yami12376/AspectJ

  1. 添加VM argumentsRun/Debug configuration of Junit test ,就我而言:

    -javaagent:C:\aspectjWeaver\spring-instrument-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

如果删除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. 显然您的 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 inside 标记中,您应该使用 .. 语法以便包含/排除子包。
  6. 你的方面类一团糟:

    • 您无缘无故地使用嵌套类。
    • 您在建议签名中使用 @Around,但使用 JoinPoint 而不是 ProceedingJoinPoint
    • 您使用@Around,但从不调用proceed()

那么这个怎么样?

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();
}
}

控制台日志:

-javaagent:/path/to/aspectjweaver.jar 添加到 IntelliJ IDEA 中的 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 项目是一个明智而审慎的决定。我可以很容易地发现问题。 :-) 如果每个人都这样做,我会在这里节省很多时间。

关于java - AspectJ - 解析定义失败 - NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42906522/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com