java - AspectJ 和 Maven 警告 : "Advice defined in ... has not been applied?"

标签 java maven web-applications jax-rs aspectj

我试图在编译时将某些方面编织到一个成为 WAR 的项目中。这些方面建议在同一项目中的类(尽管在不同的包中)。

我收到警告:

Advice not applied

我的方面没有被执行。这是我的设置:

注解 FooAnnotation.java:

package a;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface FooAnnotation {}

方面 FooAdvice.aj:

package a;
public aspect FooAdvice {
    private static final Log log = LogFactory.getLog(FooAdvice.class);

    Object around() : call( @FooAnnotation * *(..)) {   
        log.info(String.format("Testing around"));
        return proceed();
    }
}

我也试过注解:

@Around("call( @FooAnnotation * *(..))")
public Object checkFoo( ProceedingJoinPoint joinPoint) throws Throwable {

据我所知,我的切入点规范是正确的,但由于某种原因 ajc 编译器没有发挥作用。

类 FooClass.java:

package b;
@ApplicationPath("/service")
@Path("/{param}")
@Produces("application/json")
@Provider
public class FooClass {

    @POST
    @PUT
    @Path("/{context}/{resource}")
    @FooAnnotation
    public String updateResource(...) {}

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>zzz.group</groupId>
    <artifactId>yyy.artifact</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>yyy.name</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <javaVersion>1.6</javaVersion>
        <org.aspectj-version>1.7.2</org.aspectj-version>
    </properties>

    <dependencies>
        <dependency>
            ... stuff ...
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>${javaVersion}</source>
                    <target>${javaVersion}</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <!--
                    Have to use version 1.2 since version 1.3 does not appear to work
                    with ITDs
                -->
                <version>1.4</version>
                <dependencies>
                    <!--
                        You must use Maven 2.0.9 or above or these are ignored (see
                        MNG-2972)
                    -->
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${org.aspectj-version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${org.aspectj-version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <!-- <goal>test-compile</goal> -->
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <outxml>true</outxml>
                    <source>${javaVersion}</source>
                    <target>${javaVersion}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

最佳答案

啊哈!在这里找到答案:

aspectj: why advice cannot be applied?

与maven无关

原因是在我的示例中,该方法是通过间接加载从 jax-rs 框架内调用的。 call() 通知想要编织调用者,但 ajc 编译器无法知道调用者在哪里。解决方法是将建议更改为 execution() 因此:

Object around() : execution(@FooAnnotation * *(..)) {...}

这是不同的,因为它围绕执行(ajc 可以找到)而不是调用者编织。

完成。

关于java - AspectJ 和 Maven 警告 : "Advice defined in ... has not been applied?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15795630/

相关文章:

Python Pyramid gzip 渲染器

web-applications - 使用Grails读取PDF文件

iPhone "Bookmark to Homescreen"删除 cookie 和 session ?

Java 8 stream 的 toArray 和 size 参数

Maven 加载了错误版本的依赖项

maven - 如何从一个来源生成多个 Maven Artifact

scala - pom配置以通过scala maven插件强制使用jvm 7

java - 如何在 Java 中将 String 转换为 Boolean,但将 null 与 false 区别对待?

Java RegEx 反斜杠数字

java - Android 应用程序中 JSON 对象绑定(bind)的替代方案