AspectJ:如何将方面库编织到Java项目中

时间:2015-11-13 21:06:24

标签: java maven aop aspectj

我使用AspectJ构建小型库(Java和Maven)。图书馆必须是独立的。图书馆提供方面和注释。库的功能是 - "在执行具有特定注释的方法时调用建议"。当我在一个模块中使用所有内容时,一切正常,但是当我将库和项目与必须应用建议的类分开时,问题就会出现。我创建简单的架构。 Schema 图书馆B - 我的图书馆(方面和注释) 项目A - 必须应用adisce的商业方法项目 有没有可能做到这一点?

2 个答案:

答案 0 :(得分:17)

根据AspectJ Maven documentation, chapter "using aspect libraries",你需要

  • 将方面库添加为常规<dependency>
  • 还会在插件<version>部分中添加相同的依赖项(不包含<configuration><aspectLibraries>,因为它已在步骤1中分配)作为<aspectLibrary>

这是一个具体的例子:

Aspect库POM,标记注释和样本方面:

<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>de.scrum-master.stackoverflow</groupId>
    <artifactId>aspectj-lib</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>AspectJ Library</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.source-target.version>1.8</java.source-target.version>
        <aspectj.version>1.8.7</aspectj.version>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>${java.source-target.version}</source>
                        <target>${java.source-target.version}</target>
                        <!-- IMPORTANT -->
                        <useIncrementalCompilation>false</useIncrementalCompilation>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.7</version>
                    <configuration>
                        <!--<showWeaveInfo>true</showWeaveInfo> -->
                        <source>${java.source-target.version}</source>
                        <target>${java.source-target.version}</target>
                        <Xlint>ignore</Xlint>
                        <complianceLevel>${java.source-target.version}</complianceLevel>
                        <encoding>${project.build.sourceEncoding}</encoding>
                        <!--<verbose>true</verbose> -->
                        <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn> -->
                    </configuration>
                    <executions>
                        <execution>
                            <!-- IMPORTANT -->
                            <phase>process-sources</phase>
                            <goals>
                                <goal>compile</goal>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjtools</artifactId>
                            <version>${aspectj.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
            </plugin>
            <!--
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            -->
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

    <organization>
        <name>Scrum-Master.de - Agile Project Management</name>
        <url>http://scrum-master.de</url>
    </organization>

</project>
package de.scrum_master.aspect;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Marker {}
package de.scrum_master.aspect;

public aspect MyAspect {
    before() : execution(* *(..)) && @annotation(Marker) {
        System.out.println(thisJoinPoint);
    }
}

现在在方面库项目上运行mvn clean install,以便将依赖项安装到本地Maven仓库中。

Java应用程序POM和示例应用程序:

顺便说一句,在POM中我还添加了两个可选插件:

  • One-JAR插件将您的应用程序与所有依赖项(例如,方面库和AspectJ运行时)打包到一个工件中,此处为target/java-app-0.0.1-SNAPSHOT.one-jar.jar。您可以通过java -jar target/java-app-0.0.1-SNAPSHOT.one-jar.jar
  • 运行您的应用程序
  • 如果您想通过mvn clean install exec:java从命令行轻松测试应用程序,那么Exec Maven插件非常有用。

这两个只是为了方便起见,如果您不喜欢它们,则不需要它们。

<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>de.scrum-master.stackoverflow</groupId>
    <artifactId>java-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>Java Application</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.source-target.version>1.8</java.source-target.version>
        <aspectj.version>1.8.7</aspectj.version>
        <main-class>de.scrum_master.app.Application</main-class>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>${java.source-target.version}</source>
                        <target>${java.source-target.version}</target>
                        <!-- IMPORTANT -->
                        <useIncrementalCompilation>false</useIncrementalCompilation>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.7</version>
                    <configuration>
                        <!--<showWeaveInfo>true</showWeaveInfo> -->
                        <source>${java.source-target.version}</source>
                        <target>${java.source-target.version}</target>
                        <Xlint>ignore</Xlint>
                        <complianceLevel>${java.source-target.version}</complianceLevel>
                        <encoding>${project.build.sourceEncoding}</encoding>
                        <!--<verbose>true</verbose> -->
                        <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn> -->
                        <aspectLibraries>
                            <aspectLibrary>
                                <groupId>de.scrum-master.stackoverflow</groupId>
                                <artifactId>aspectj-lib</artifactId>
                            </aspectLibrary>
                        </aspectLibraries>
                    </configuration>
                    <executions>
                        <execution>
                            <!-- IMPORTANT -->
                            <phase>process-sources</phase>
                            <goals>
                                <goal>compile</goal>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjtools</artifactId>
                            <version>${aspectj.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.4.0</version>
                    <configuration>
                        <mainClass>${main-class}</mainClass>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.dstovall</groupId>
                    <artifactId>onejar-maven-plugin</artifactId>
                    <version>1.4.4</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>one-jar</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <onejarVersion>0.96</onejarVersion>
                        <mainClass>${main-class}</mainClass>
                        <attachToBuild>true</attachToBuild>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
            </plugin>
            <!--
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <configuration>
                    <mainClass>${main-class}</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.dstovall</groupId>
                <artifactId>onejar-maven-plugin</artifactId>
                <configuration>
                    <mainClass>${main-class}</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <pluginRepositories>
        <pluginRepository>
            <id>OneJAR googlecode.com</id>
            <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>de.scrum-master.stackoverflow</groupId>
                <artifactId>aspectj-lib</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>
            <dependency>
                <groupId>de.scrum-master.stackoverflow</groupId>
                <artifactId>aspectj-lib</artifactId>
            </dependency>
    </dependencies>

    <organization>
        <name>Scrum-Master.de - Agile Project Management</name>
        <url>http://scrum-master.de</url>
    </organization>

</project>

这个小小的驱动程序应用程序演示了您现在可以使用@Marker注释方法,在这种情况下只有foozot,而不是bar

package de.scrum_master.app;

import de.scrum_master.aspect.Marker;

public class Application {
    @Marker
    public static void foo() {}

    public static void bar() {}

    @Marker
    public static void zot() {}

    public static void main(String[] args) {
        foo();
        bar();
        zot();
    }
}

控制台日志:

java -jar target/java-app-0.0.1-SNAPSHOT.one-jar.jar

execution(void de.scrum_master.app.Application.foo())
execution(void de.scrum_master.app.Application.zot())

答案 1 :(得分:0)

这是krigaex 回答的简短摘要。将您的库作为普通依赖项和 AspectJ 插件依赖项添加到 pom.xml。如果您使用 com.nickwongdev:aspectj-maven-plugin - 使用 <weaveDependencies> & <weaveDependency> 标签而不是 <aspectLibraries> & <aspectLibrary>

<project>
  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>my-library</artifactId>
      <version>1.2.0-SNAPSHOT</version>
    </dependency>
  <build>
    <plugins>
    <plugin>
      <groupId>com.nickwongdev</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.12.6</version>
      <configuration>
        <weaveDependencies>
          <weaveDependency>
            <groupId>com.example</groupId>
            <artifactId>my-library</artifactId>
          </weaveDependency>
        </weaveDependencies>
      </configuration>
    </plugin>
    </plugins>
  </build>
</project>