アスペクト・ライブラリを作る 其ノ参 -- アスペクト・ライブラリを使用する

アスペクト・ライブラリを作る 其ノ参 -- アスペクト・ライブラリを使用する

今回は、前回作成したアスペクト・ライブラリを使用してみましょう。

アスペクト・ライブラリを使用するプロジェクトは「greeting-app」とします。

pom.xml

とりあえず、サンプルを。

<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 !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值