本文介绍了AspectJ& Maven警告:“尚未应用...中定义的建议?"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在编译时将某些方面编织到成为WAR的项目中.这些方面建议同一项目中的类(尽管使用不同的程序包).

I'm trying to weave some aspects at compile time into a project that becomes a WAR. The aspects advise classes that are within the same project (though in different packages).

我得到警告:

Advice not applied

我的方面未执行.这是我的设置:

My aspect is not being executed. Here's my setup:

注释FooAnnotation.java:

annotation FooAnnotation.java:

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

尊重FooAdvice.aj:

aspect 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编译器无法发挥作用.

As far as I can tell, my pointcut specification is correct, but for some reason the ajc compiler isn't playing ball.

FooClass.java类:

Class 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:为什么不能应用建议?

它与行家无关.

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

The reason is that in my example, the method is being called from within the jax-rs framework by indirect loading. The call() advice wants to weave the caller, but the ajc compiler can't know where the caller is. The fix is to change the advice to execution() thusly:

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

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

This differs because it weaves around the execution (which ajc can find), rather than the caller.

完成.

这篇关于AspectJ&amp; Maven警告:“尚未应用...中定义的建议?"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 14:48