王朝网络
分享
 
 
 

面向方面编程AOP和JBoss(二)

王朝other·作者佚名  2008-05-31
宽屏版  字体: |||超大  

访问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

[code]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

[/code]

你能够在这里下载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"

<aop

<introduction-pointcut class="POJO"

<mixin

<interfacesTracing</interfaces

<classTracingMixin</class

<constructionnew TracingMixin(this)</construction

</mixin

</introduction-pointcut

</aop

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

运行例子3

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

[code]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();

}

}

[/code]

注重我们转换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。那会有一些惊异等着你,象我们已经在我们新的框架上实现了一套服务。拥有它并恰当的使用。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有