首页 文章

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

提问于
浏览
0

我有一个项目,我想使用方面但不使用 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

    我几乎完全相同的设置工作(我离开了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>
    

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

相关问题