AspectJ:如何将方面库编织到Java项目中 [英] AspectJ: How to weave an aspect library into a Java project

查看:247
本文介绍了AspectJ:如何将方面库编织到Java项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

图书馆B - 我的图书馆(方面和注释)
项目A - 项目必须应用adivce的商业方法
是否有任何可能性这样做?

I build small library (Java and Maven) - using AspectJ. Library must be independent. Library deliver Aspects and Annotations. Function of library is - "call advice when executed a method with specific annotation". All is ok when I use everything in one module, but problem appers when i separate library and project with classes which advice must be applied. I create simple schema. Library B - my library (aspects and annotations) Project A - project with buisness methods which adivce must be applied Is any posibility to do this?

推荐答案

根据 AspectJ Maven文档,使用方面库,你需要


  • 将方面库添加为常规< dependency>

  • 还添加相同的依赖项(不含<$ c) $ c>< version> 因为它已在插件< configuration>< aspectLibraries> 部分中作为步骤1)分配< aspectLibrary>

  • add the aspect library as a regular <dependency>,
  • also add the same dependency (without <version> because it is already assigned in step 1) in the plugin <configuration><aspectLibraries> section as an <aspectLibrary>.

这是一个具体的例子:

Aspect库POM,标记a符号和示例方面:

<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仓库中。

Now run mvn clean install on the aspect library project so as to install the dependency into your local Maven repo.

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

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

By the way, in the POM I have also added two optional plugins:


  • One-JAR插件将您的应用程序与所有依赖项(例如方面库和AspectJ运行时)打包到一个工件中,这里是 target / java-app-0.0.1-SNAPSHOT.one-jar。罐。您可以通过 java -jar target / java-app-0.0.1-SNAPSHOT.one-jar.jar 运行您的应用程序。

  • 如果你想从命令行通过 mvn clean install exec:java 测试你的应用程序,那么Exec Maven插件很有用。

  • One-JAR plugin packages your application with all dependencies (e.g. aspect library and AspectJ runtime) into one artifact, here target/java-app-0.0.1-SNAPSHOT.one-jar.jar. You can just run your application via java -jar target/java-app-0.0.1-SNAPSHOT.one-jar.jar.
  • Exec Maven plugin is useful if you easily want to test your application from the command line via mvn clean install exec:java.

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

These two are only for convenience, you do not need them if you dislike them.

<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 <注释方法/ code>,在这种情况下只有 foo zot ,但不是 bar

This little driver application demonstrates that you can now annotate methods with @Marker, in this case only foo and zot, but not 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())

这篇关于AspectJ:如何将方面库编织到Java项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆