方法和注释
要把你的java代码和aop结合起来,可以使用一些标签,就像新的jdk5.0版本中的元数据以及xdoclet
。对于你的java方法,就有点像使用synchronized一样。当你把你的方法标注为synchronized,就表明
你高数jvm你的这个方法在执行的使用是需要同步的。注释标签允许你定义新的关键词用来处理你自己
的特殊行为。aop就使你有了把这些织入到你的应用中,以使应用可以执行的能力。
让我们看看我们使用自己定义的标签@oneway来使一个方法在后台的另外一个线程执行。
import org.jboss.aspects.oneway;
public class foo
{
@oneway public static void somemethod() {…}
public static void main(string[] args)
{
somemethod(); // executes in background
}
}
当我们的main方法调用 msomeethod方法的时候,起始它的执行是和main方法平行的另外一个同步方法中
执行。
要执行这个类,我们必须首先定义自己的标签到java语法中。
package org.jboss.aspects;
import java.lang.annotation.elementtype;
import java.lang.annotation.target;
@target({elementtype.method})
public @interface oneway {}
这就够了。@target可以限制你的标签的应用范围。在这里你的oneway就只能被用于方法。上面这些
是纯粹的j2se5.0里面应用。
下面的是我们的方面,用来执行@oneway的行为。
package org.jboss.aspects;
public onewayaspect
{
private static class task implements runnable
{
private methodinvocation invocation;
public task(methodinvocation invocation)
{
this.invocation = invocation;
}
public void run()
{
try { invocation.invokenext(); }
catch (throwable ignore) { }
}
}
public object oneway(methodinvocation invocation) throws throwable
{
methodinvocation copy = invocation.copy();
thread t = new thread(new task(copy));
t.setdaemon(false);
t.start();
return null;
}
}
我们的方面就是这么简单。
最后就是我们要定义切入点
<aop>
<aspect class="org.jboss.aspects.onewayaspect"/>
<bind pointcut="execution(void *->@org.jboss.oneway(..))">
<advice name="oneway"
aspect="org.jboss.aspects.onewayaspect"/>
</bind>
</aop>
这个定义表明了只有是void的被标注为@oneway的方法才被我们onewayaspect执行。这样就可以把你
其它应用加上去,这就是简单,清晰,方便的java预言。
字段和注释
通过aop我们也可以定义我们自己的字段意思,例如我们可以定义一个threadlocal的字段,如果
使用以前的方法,我们就必须使用很多get()和set()才能实现。而现在我们可以直接定义一个
@threadlocal的类型。
要使用这个新的类型可以像这样:
import org.jboss.aspects.threadbased;
public class foo
{
@threadbased private int counter;
}
首先我们要定义这个类型
package org.jboss.aspects;
import java.lang.annotation.elementtype;
import java.lang.annotation.target;
@target({elementtype.field})
public @interface threadbased {}
这样就可以了,现在所有被标注为@threadbased的字段都将被应用到这里来,但这里限制了此类型
只能用户字段。
下面就是实现我们的方面来做具体的工作。
package org.jboss.aspects;
import org.jboss.aop.joinpoint.*;
import java.lang.reflect.field;
public class threadbasedaspect
{
private threadlocal threadbased = new threadlocal();
public object access(fieldreadinvocation invocation)
throws throwable
{
// just in case we have a primitive,
// we cant return null
if (threadbased.get() == null)
return invocation.invokenext();
return threadbased.get();
}
public object access(fieldwriteinvocation invocation)
throws throwable
{
threadbased.set(invocation.getvalue());
return null;
}
}
最后定义切入点
<aop>
<aspect class="org.jboss.aspects.threadbasedaspect"
scope="per_joinpoint"/>
<bind pointcut="field(* *->@org.jboss.aspects.threadbased)">
<advice name="access"
aspect="org.jboss.aspects.threadbasedaspect"/>
</bind>
</aop>
我们的希望是为每个字段都进行实例化的时候使之成为一个threadlocal的静态变量,所以在
定义切入点的时候我们使用了per_joinpoint,让jboss aop知道为我们每个字段都要实例化
一个新的threadlocal,否则jboss aop只会我们实例化一个threadbasedaspect,如果这样就
会让所有的字段共享一个threadlocal,这明显不是我们所期望的。
和上面一样,在这里我们看到了干净的,容易集成的方法实现了定义新的java类型。
