在 AspectJ 示例项目中出错
Getting error in AspectJ sample project
我是 aspectJ 的新手。我在 Aspect class 中遇到编译错误。当我 运行 项目时,出现编译错误。请帮我。放置以下代码片段和错误:
项目结构:
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:
您已经通过 "New" -> "Class"(因此是“.java”扩展名)在 Eclipse 中创建了方面,但您应该通过 "New" 创建它-> "Aspect"(然后它将获得“.aj”扩展名)。
P.S.: 你确实在 Eclipse 中安装了 AJDT(AspectJ 开发工具),不是吗? ;-)
P.P.S.: 您的 Maven POM 也包含一些问题,例如对 AspectJ Runtime 1.8.4 的运行时依赖,但旧版本 1.5 中的 AspectJ Maven Plugin 使用 AspectJ Tools 1.7.3。当前插件版本 1.7 使用 AspectJ Tools 1.8.2,如果需要,可以通过插件依赖配置升级到 1.8.4。
我是 aspectJ 的新手。我在 Aspect class 中遇到编译错误。当我 运行 项目时,出现编译错误。请帮我。放置以下代码片段和错误:
项目结构:
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:
您已经通过 "New" -> "Class"(因此是“.java”扩展名)在 Eclipse 中创建了方面,但您应该通过 "New" 创建它-> "Aspect"(然后它将获得“.aj”扩展名)。
P.S.: 你确实在 Eclipse 中安装了 AJDT(AspectJ 开发工具),不是吗? ;-)
P.P.S.: 您的 Maven POM 也包含一些问题,例如对 AspectJ Runtime 1.8.4 的运行时依赖,但旧版本 1.5 中的 AspectJ Maven Plugin 使用 AspectJ Tools 1.7.3。当前插件版本 1.7 使用 AspectJ Tools 1.8.2,如果需要,可以通过插件依赖配置升级到 1.8.4。