站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题翻译-面向方面编程和JBoss之二

翻译-面向方面编程和JBoss之二

摘要: 翻译-面向方面编程和JBoss之二
内容: 访问Metadata
为了用元数据,它在运行时间必须是可达的。类的元数据是通过Invocation对象可达的。为了在我们的例子使用它,TracingInterceptor必须要修改一点点。

public class TracingInterceptor implements Interceptor
{
public String getName() { return TracingInterceptor; }
public InvocationResponse invoke(Invocation invocation)
throws Throwable
{
String filter = (String)invocation.getMetaData(tracing, filter);
if (filter != null && filter.equals(true))
return invocation.invokeNext();

String message = null;

if (invocation.getType() == InvocationType.METHOD)
{
Method method = MethodInvocation.getMethod(invocation);
message = method: + method.getName();
}
else if (invocation.getType() == InvocationType.CONSTRUCTOR)
{
Constructor c = ConstructorInvocation.getConstructor(invocation);
message = constructor: + c.toString();
}
else
{
// Do nothing for fields. Just too verbose.
return invocation.invokeNext();
}

System.out.println(Entering + message);

// Continue on. Invoke the real method or constructor.
InvocationResponse rsp = invocation.invokeNext();
System.out.println(Leaving + message);
return rsp;
}
}

运行例子2


POJO类将扩展一点,增加get()和set()方法。
public class POJO
{
public POJO() {}
public void helloWorld() { System.out.println(Hello World!); }

private int counter = 0;

public int getCounter() { return counter; }
public void setCounter(int val) { counter = val; }
public static void main(String[] args)
{
POJO pojo = new POJO();
pojo.helloWorld();
pojo.setCounter(32);
System.out.println(counter is: + pojo.getCounter());
}
}
TracingInterceptor将拦截对main(),POJO()和helloWorld()调用。输出应该看起来如下:
Entering constructor: public POJO()
Leaving constructor: public POJO()
Entering method: helloWorld
Hello World!
Leaving method: helloWorld


你能够在这里下载JBoss AOP和离子代码。编译和执行:
$ cd oreilly-aop/example2
$ export CLASSPATH=.;jboss-common.jar;jboss-aop.jar;javassist.jar
$ javac *.java
$ java -Djava.system.class.loader=org.jboss.aop.standalone.SystemClassLoader POJO

例子3.使用导言
如果我们能够为特定的实例关闭和打开,那将很酷。JBoss AOP有一个API,他绑定元数据到一个对象实例,但是让我们伪装一个实际的跟踪API是一个更好的方案。在这例子中,我们通过用一个导言,将改变POJO类的本身的定义。我们将强制POJO类去实现一个跟踪借口和提供混合类,这个混合类处理新的跟踪API。这将是跟踪借口:

public interface Tracing
{
public void enableTracing();
public void disableTracing();
}

定义一个混合的类
Tracing接口将在混合类中实现。当一个POJO是实例时,一个混合对象混合类将绑定到POJO类。下面是实现:

import org.jboss.aop.Advised;

public class TracingMixin implements Tracing
{
Advised advised;

Public TracingMixin(Object obj)
{
this.advised = (Advised)obj;
}

public void enableTracing()
{
advised._getInstanceAdvisor().getMetaData().addMetaData(
"tracing", "filter", true);
}

public void disableTracing()
{
advised._getInstanceAdvisor().getMetaData().addMetaData(
"tracing", "filter", false);
}
}

enableTracing()方法绑定filter属性到对象实例。在disableTracing()方法作同样的事,但是制定filter属性为false。这两个方法是元数据能够怎么样用于超过一个类级别。元数据也能够实例级的应用。元数据应用在实例级别。

绑定一个导言
好了,所以我们定义跟踪接口,并且实现这个混合类。下一步是应用导言到POJO类。像拦截器,我们必须在XML中定义一个ponitcut。让我们看一下这项什么。

<?xml version="1.0" encoding="UTF-8">



Tracing
TracingMixin
new TracingMixin(this)




上面的pointcuts将强制POJO类实现Tracing接口。现在,当一个POJO实例被初始化,一个TracingMixin也将被实例化。TracingMixin被初始化的途径被定义在标签中。你能够把想要的任一行Java代码放入在标签中。

运行例子3
POJO类为了显示TracingAPI怎么被访问,它已经被扩展了一点。TracingInterceptor仍然和例子2一样。

public class POJO
{
public POJO() {}
public void helloWorld() { System.out.println(Hello World!); }

public static void main(String[] args)
{
POJO pojo = new POJO();
Tracing trace = (Tracing)this;
pojo.helloWorld();

System.out.println("Turn off tracing.");

trace.disableTracing();
pojo.helloWorld();

System.out.println("Turn on tracing.");

trace.enableTracing();
pojo.helloWorld();
}
}

注意我们转换POJO到Tracing接口。输出应该看起来这样:

Entering constructor: POJO()
Leaving constructor: POJO()
Entering method: helloWorld
Hello World!
Leaving method: helloWorld
Turn off tracing.
Entering method: disableTracing
Leaving method: disableTracing
Hello World!
Turn on tracing.
Entering method: helloWorld
Hello World!
Leaving method: helloWorld


注意被增加到TracingInterceptor 中的interceptor-pointcut也应用到那些通过Tracing 导言导入的方法中。
为了编译和运行这个例子:

$ cd oreilly-aop/example3
$ export CLASSPATH=.;jboss-common.jar;jboss-aop.jar;javassist.jar
$ javac *.java
$ java -Djava.system.class.loader=org.jboss.aop.standalone.SystemClassLoader POJO


结论
面向方面编程对于软件开发是一个强有力的新工具。为了使你的软件开发过程更加动态和流畅,用JBoss4.0,你能够实现你自己的拦截器,元数据和导言。更详细的文档参见我们的站点www.jboss.org。那会有一些惊奇等着你,象我们已经在我们新的框架上实现了一套服务。拥有它并恰当的使用。
Bill Burke JBOssGroup首席架构师,领导JBoss4.0
Adrian Brock是JBossGroup的技术主管

matrix开源技术经onjava授权翻译并发布.
如果你对此文章有任何看法或建议,请到Matrix论坛发表您的意见.
注明: 如果对matrix的翻译文章系列感兴趣,请点击oreilly和javaworld文章翻译计划查看详细情况
您也可以点击-neo 查看翻译作者的详细信息.


Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd
↑返回目录
前一篇: Rational为由设计直接生成代码努力
后一篇: 面向方面编程和JBoss之一