我是aspectJ的新手。我在Aspect类中遇到编译错误。当我运行项目时,我收到编译错误。请帮我。放置代码段和下面获得的错误:
项目结构:
MannersAspect.java
package main.java.testaop;
public aspect MannersAspect {
pointcut callSayMessage() :
call(public static void HelloWorld.say*(..));
before() : callSayMessage() {
System.out.println("Good day!");
}
after() : callSayMessage() {
System.out.println("Thank you!");
}
}
HelloWorld.java
package main.java.testaop;
public class HelloWorld {
public static void say(String message) {
System.out.println(message);
}
public static void sayToPerson(String message, String name) {
System.out.println(name + ", " + message);
}
}
的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>testaop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testaop</name>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.4</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<versionRange>[2.5.1,)</versionRange>
<goals><goal>compile</goal></goals>
</pluginExecutionFilter>
<action><ignore></ignore></action>
</pluginExecution>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<versionRange>[1.7,)</versionRange>
<goals><goal>compile</goal></goals>
</pluginExecutionFilter>
<action><ignore></ignore></action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<executions>
<execution>
<goals><goal>compile</goal></goals>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution><goals><goal>java</goal></goals></execution>
</executions>
<configuration>
<mainClass>main.java.testaop.HelloWorld</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
错误:
[ERROR] COMPILATION ERROR :
[ERROR] D:\workspacedummy\testaop\src\main\java\testaop\MannersAspect.java:[3,7] error: class, interface, or enum expected
[ERROR] D:\workspacedummy\testaop\src\main\java\testaop\MannersAspect.java:[4,51] error: class, interface, or enum expected
[ERROR] D:\workspacedummy\testaop\src\main\java\testaop\MannersAspect.java:[4,72] error: malformed floating point literal
[ERROR] D:\workspacedummy\testaop\src\main\java\testaop\MannersAspect.java:[5,4] error: class, interface, or enum expected
[ERROR] D:\workspacedummy\testaop\src\main\java\testaop\MannersAspect.java:[7,4] error: class, interface, or enum expected
[ERROR] D:\workspacedummy\testaop\src\main\java\testaop\MannersAspect.java:[10,4] error: class, interface, or enum expected
[INFO] 6 errors
[INFO] BUILD FAILURE
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project testaop: Compilation failure: Compilation failure:
答案 0 :(得分:1)
您已经通过&#34; New&#34;在Eclipse中创建了您的方面。 - &GT; &#34;级&#34; (因此&#34; .java&#34;扩展名),但你应该通过&#34; New&#34; - &GT; &#34;方面&#34; (然后它会得到一个&#34; .aj&#34;扩展名。)
P.S。:您在Eclipse中安装了AJDT(AspectJ开发工具),不是吗?的; - )强>
P.P.S。:您的Maven POM还包含一些问题,例如:对 AspectJ Runtime 1.8.4的运行时依赖性,但旧版本1.5中的 AspectJ Maven插件使用 AspectJ Tools 1.7.3。当前插件版本1.7使用 AspectJ Tools 1.8.2,如有必要,可以通过插件依赖关系配置升级到1.8.4。