JavaでもAOPしてみる(JBossAOP使ってみた)
http://labs.jboss.com/jbossaop/downloads/
から、最新stableらしい1.5.6.GAをダウンロード。
展開したフォルダの
jboss-aop_1.5.6.GA\docs\aspect-framework\examples\all
をコマンドラインで開き、antでコンパイル&実行までやってくれる。
all以外の他のサンプルも同じっぽい。こりゃ便利。
このサンプルを改造して、java.util.Date#getTimeの例をやってみようとする。
まず、Driver.javaに一行追加。
/* * JBoss, Home of Professional Open Source * Copyright 2005, JBoss Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ public class Driver { public static void main(String[] args) { System.out.println("--- new POJO(); ---"); POJO pojo = new POJO(); System.out.println("--- pojo.someMethod(); ---"); pojo.someMethod(); System.out.println("--- pojo.var++; ---"); pojo.var++; // 追加 System.out.println(new java.util.Date().getTime()); } }
<?xml version="1.0" encoding="UTF-8"?> <aop> <bind pointcut="all(POJO)"> <interceptor class="SimpleInterceptor"/> </bind> <!-- add --> <bind pointcut="all(java.util.Date)"> <interceptor class="SimpleInterceptor"/> </bind> </aop>
でantで実行してみる。が、結果むなしくgetTimeの結果が表示されただけ。
既存のjava.*クラスを書き換えられませんでした。
つか、なんでそれにこだわっているのか良く分からなくなってきたのですが、まあ結果がそうだということで。