访问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;
}
}
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
public interface tracing
{
public void enabletracing();
public void disabletracing();
}
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);
}
}
<?xml version="1.0" encoding="utf-8">
<aop>
<introduction-pointcut class="pojo">
<mixin>
<interfaces>tracing</interfaces>
<class>tracingmixin</class>
<construction>new tracingmixin(this)</construction>
</mixin>
</introduction-pointcut>
</aop>
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();
}
}
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
$ 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
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器