AspectJ你好世界,需要一些建议

时间:2015-05-15 14:53:50

标签: java maven aspectj

我需要一些关于AspectJ和maven的帮助。我用AspectJ的方面编写了简单的应用程序。

src/main/java - 默认包

public class Main 
{
    public int a = 10; 
    public static void main( String[] args )
    {
        Main instance = new Main();
        System.out.println(instance.test());
    }

    public int test(){
        return a;
    }
}

src/main/aspects - default-package

public aspect TestAspect {
    pointcut publicMethodExecuted(): execution(public !static * *(..));

    int around() : publicMethodExecuted() {    
         int original_return_value = proceed();
         return original_return_value * 100;
    }
}

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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>group.com</groupId>
    <artifactId>test</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>test</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <configuration>
                            <sources>
                                <source>
                                    <basedir>src/main/aspects</basedir>
                                    <includes>
                                        <include>**/TestAspect.aj</include>
                                    </includes>
                                </source>
                            </sources>
                        </configuration>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

予。命令行方式

现在我执行以下操作:

mvn install
java -jar test-1.0-SNAPSHOT.jar

我得到了输出10。但我希望它是1000

II。 Eclipse AJDT方式

如果我在Eclipse AJDT内运行该项目,我得到1000正如我预期的那样。

命令行的方式是什么?

当我运行mvn install -X

我发现了一行

[WARNING] advice defined in TestAspect has not been applied [Xlint:adviceDidNotMatch]
        D:\aspectJ\aspectj-lib\src\main\aspect\TestAspect.aj:4

为什么会这样?

2 个答案:

答案 0 :(得分:1)

  

[INFO]没有发现跳过aspectJ compile的修改

是。这解释了。这通常通过比较文件的日期来完成,例如源文件和类文件。这可能是由您的IDE引起的。有一个名为forceAjcCompile的插件的标志,但是,我看到插件编写者忘了为命令行公开它。简单(不是最佳)解决方案是:将<forceAjcCompile>true</forceAjcCompile>添加到插件的配置中,或运行mvn clean package

答案 1 :(得分:1)

您的配置似乎有缺陷

试试这个:

        <configuration>
          <aspectDirectory>src/main/aspects</aspectDirectory>
        </configuration>

如果使用默认的src/main/aspect目录,则可以完全省略。