我试图让AspectJ在一个现有项目中工作(实际上,我对该项目知之甚少,因为它似乎并不重要).
我们决定使用加载时间编织以避免使用ajc
因为我是AspectJ的新手,所以我首先创建了一个示例项目,其中包含一些类和日志记录方面:
@Aspect
public class LoggingAspect {
@Pointcut("call(public de.test.beans.IPerson+.*(..))")
public void logExecutions(JoinPoint jp) {}
@Before("logExecutions(jp)")
public void beforeExecutions(JoinPoint jp) {
BeforeExecutionLog log = new BeforeExecutionLog(jp);
System.out.println(log);
}
@AfterReturning(pointcut = "logExecutions(jp)", returning = "ret")
public void afterExecutions(JoinPoint jp, Object ret) {
AfterExecutionLog log = new AfterExecutionLog(jp, ret);
System.out.println(log);
}
}
一切正常,一切都很好.
下一步,我尝试同时使用AspectJ和Maven.我只是稍微更改了日志记录方面(只是软件包).
根据《 AspectJ in Action》一书中的代码,我修改了我们的maven pom.xml.
<dependencies>
...
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.12.M1</version>
</dependency>
</dependencies>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</execution>
</executions>
</plugin>
但是如果尝试调用mvn clean install我会遇到成千上万个错误,第一个错误是:
[ERROR] Syntax error on token "call(public xxx.yyy.zzz.api.Service+.*(..))", "name pattern" expected
[ERROR] Method annotated with @Pointcut() for abstract pointcut must be abstract
还有以下错误:
[ERROR] The method xyz() of type zyx must override a superclass method
我想这些都是受我方面影响的方法.
谁能解释我,怎么了?
先感谢您
UPD:
我更新了我的问题.
解决方法:
您的示例似乎不完整,显示的错误与代码不匹配,但是使用以下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>org.test</groupId>
<artifactId>test2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>