maven aspectj插件可以通过注释编写方面吗?

时间:2013-06-15 00:52:00

标签: annotations aspectj aspectj-maven-plugin

我有一个项目,我想使用方面但不使用spring。所以我使用的是AspectJ。在我的特定情况下,LTW不适合我,所以我必须使用CTW,这导致我使用maven aspectj插件。我目前正在使用Java 7.我正在将一个maven模块中编写的方面编织到另一个模块中,并将其作为依赖项。

当我编织一个没有注释的方面时,插件似乎正确编织,但是当我将它切换到基于注释的方面时,看起来插件并没有写入hasaspect()和aspectof()方法因为在运行时我得到异常,说方法不存在。

我尝试过的一件事是让基于注释的方面扩展Aspects类,希望它能够找到方法,但这也无效。

我的插件配置如下:

   <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <showWeaveInfo>false</showWeaveInfo>
                <Xlint>ignore</Xlint>
                <source>1.7</source>
                <target>1.7</target>
                <complianceLevel>1.7</complianceLevel>
                <encoding>UTF-8</encoding>
                <verbose>false</verbose>
                <weaveDirectories>
                    <weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
                </weaveDirectories>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>my.group</groupId>
                        <artifactId>dependencywithaspecttoweave</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>1.7.2</version>
                </dependency>
            </dependencies>

我的注释方面如下:

public class TraceLog extends Aspects {
private ComponentLogger logger;

@Pointcut(value = "within(my.package..*) && "
        + "execution(* *(..))")
public void scope() {
}

/**
 * Before method.
 *
 * @param thisJoinPoint The joinpoint.
 * @throws IllegalAccessException When it tries to get the logger and can't.
 */
@Before(value = "scope()")
public void logBefore(final JoinPoint thisJoinPoint) throws IllegalAccessException {
    logMethod(thisJoinPoint, "Entering " + createLogMessage(thisJoinPoint));
}
...

我必须做的是恢复原始的非注释方面风格。但这是一个拖累,因为我不能再从方法中抛出异常(或至少到目前为止,我不知道如何)。抛出异常对于安全性等特别有用,如果用户没有访问权限,则异常将是提醒调用者的适当方式。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我几乎完全相同的设置工作(我离开了Spring,Google App Engine只支持编译时编织),并部署了一个多模块EAR。

从您的问题中听起来您没有找到AspectJ Multi-Module documentation并在您的设置中使用它。通过更仔细地查看POM,我发现在执行部分中,您没有任何测试 - 编译阶段的任何内容。此外,您应该获得尽可能多的信息:将verbose和showWeaveInfo设置为true。这应该会给你一个更好的错误(可能会指向StackOverflow)或寻求帮助。

我的执行部分如下

      <!-- Executions for compiler -->
      <executions>
        <execution>
          <id>aspectj-compile</id>
          <goals>
            <goal>compile</goal>
          </goals>
        </execution>
        <execution>
          <id>aspectj-test-compile</id>
          <goals>
            <goal>test-compile</goal>
          </goals>
        </execution>
      </executions>

在获取更多信息,测试编译和按照建议设置项目之间,你应该能够开始。