今回は、前回作成したアスペクト・ライブラリを使用してみましょう。
アスペクト・ライブラリを使用するプロジェクトは「greeting-app」とします。
とりあえず、サンプルを。
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>greeting-app</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<dependencies>
<dependency>
<groupId>org.sample</groupId>
<artifactId>greeting</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.sample</groupId>
<artifactId>greeting-lib</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.2a</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<weaveDependencies>
<waveDependency>
<groupId>org.sample</groupId>
<artifactId>greeting</artifactId>
</waveDependency>
</weaveDependencies>
<aspectLibraries>
<aspectLibrary>
<groupId>org.sample</groupId>
<artifactId>greeting-lib</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
幾つか注意を:
- 「既存のプロジェクト」と「アスペクト・ライブラリのプロジェクト」への依存性を付加する。
- aspectjrt への依存性を付加する(AspectJ プロジェクトに必須)。
- aspectj-maven-plugin の <executions> 要素を設定する(AspectJ プロジェクトに必須)。
- aspectj-maven-plugin の <weaveDependency>, <aspectLibrary> を設定する。
<weaveDependency> 要素には既存のライブラリを、また、<aspectLibrary> 要素には作成したアスペクト・ライブラリのプロジェクトを設定します*1。
実行クラス ExampleMain と実行結果
プロジェクトに以下のような実行クラス
package greeting;
public class ExampleMain {
public static void main(String[] args) {
Person waman = new Person("Waman");
waman.greet();
}
}
を作成し、これを実行すると、以下のメッセージが表示されます:
Waman says : Hello, world !