【问题标题】:Aspect in java not being weaved correctlyjava中的方面未正确编织
【发布时间】:2015-03-24 14:57:10
【问题描述】:

我有以下方法:

public class MonitorInterface {

    // this is the method you have to call to trigger the monitor
    public static void event(String eventName, HashMap params) { 
        System.out.println("Entering event method");
    }

}

以及以下方面:

package aspects;

import com.path.for.MonitorInterface;
import java.util.HashMap;
public aspect _asp_connector0 {
    private pointcut eventP():
        execution(public static void event(String, HashMap));

    before(): eventP(){
        System.out.println("Test pointcut weave");
    }
}

这基本上是在前面的方法中添加了一个 Sys.out.print

至于 pom.xml 我主要使用以下插件:

 <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <useIncrementalCompilation>false</useIncrementalCompilation>
    </configuration>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <complianceLevel>1.7</complianceLevel>
        <Xlint>ignore</Xlint>
        <encoding>UTF-8</encoding>
        <verbose>true</verbose>
        <showWeaveInfo>true</showWeaveInfo>
        <sources>
            <source>
            <basedir>src/main/resources</basedir>
            <includes>
                <include>**/_asp_connector0.aj</include>
            </includes>
            <excludes>
                <exclude>**/*.java</exclude>
                    <exclude>**/*.lrv</exclude>
                    <exclude>**/*.txt</exclude>
                </excludes>
            </source>
        </sources>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
         <mainClass>path.to.main.Example</mainClass>
    </configuration>
</plugin>
<plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
       <execution>
          <configuration>
            <onejarVersion>0.96</onejarVersion>
            <mainClass>path.to.main.Example</mainClass>
            <attachToBuild>true</attachToBuild>
          </configuration>
          <goals>
              <goal>one-jar</goal>
          </goals>
       </execution>
  </executions>
</plugin>

但是,当我编译(使用 mvn clean install)并运行生成的 jar 文件时,我永远不会以所需的方法获得编织代码。

或者,我尝试使用 ajc 编译器手动运行它们,如下所示:

ajc -outjar testMain.jar -target 1.5 -source 1.5 src\main\java\path\to\Example.java src\main\java\path\to\MonitorInterface.java
set CLASSPATH=%CLASSPATH%;.\testMain.jar
ajc -outjar testAsp.jar -target 1.5 -source 1.5 src\main\resources\aspects\_asp_connector0.aj
set CLASSPATH=%CLASSPATH%;.\testAsp.jar
aj path.to.Example

这会导致警告

_asp_connector0.aj:12 [warning] advice defined in aspects._asp_connector0 has not been applied [Xlint:adviceDidNotMatch]

但新的 println 仍然没有出现

我该如何解决这个问题,或者至少更有效地调试这个问题?

注意:使用 maven,正在生成切面的类文件,只是没有将代码编织到实际方法中

【问题讨论】:

    标签: java maven aspectj aspectj-maven-plugin


    【解决方案1】:

    由于您的方面在 Eclipse 中的普通 java AspectJ 项目中工作 - 至少对我而言 - 问题一定是您的编织配置。一件事立即突出:

    <basedir>src/main/resources</basedir>
    

    为什么你试图将你的方面应用到你的资源而不是你的代码(src/main/java 是 maven 项目中的默认值)?更改该行并删除包含/排除项,我从 aspectj-maven-plugin 获得以下信息:

    [INFO] Join point 'method-execution(void program.MonitorInterface.event(java.lang.String, java.util.HashMap))' in Type 'program.MonitorInterface' (MonitorInterface.java:7) advised by before advice from 'aspects._asp_connector0' (_asp_connector0.aj:11)
    

    所以建议被使用和应用。

    您是否有理由指定这些特定的包含/排除项?

    <exclude>**/*.java</exclude>
    

    仅此一项就几乎不可能为任何 Java 代码提供建议,因为您将所有 Java 代码排除在编译时编织之外。我建议删除所有包含/排除项并仅逐个添加它们,如果您有一个可以通过包含/排除项解决的特定问题。对于您在此处描述的用例,它们完全没有必要,而且在当前配置中实际上存在问题。

    【讨论】:

    • 资源是因为我的方面在资源文件夹中。我将它们移至 java 并将 basedir 设置为 /java。我也删除了排除,但我仍然没有接到任何电话
    • 你还有sources-标签吗?最好尝试完全删除它,看看它是否有效。否 sources-tag 意味着插件将获取项目中的每个 .aj 和 .java 文件。 empty sources-tag 意味着 no 文件将被拾取。
    猜你喜欢
    • 2018-07-22
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多