JBoss.org Community Documentation
To create a binding to an advice method from an aspect class, you annotate
the advice method with
@Bind
. To create a binding to an
Interceptor or AspectFactory, you annotate the class itself with
@Bind
since Interceptors only contain one advice (the
invoke()
method).
The @Bind annotation will only be recognised in the situations just mentioned.
The declaration of
org.jboss.aop.Bind
is:
package org.jboss.aop; @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Bind { AdviceType type() default AdviceType.AROUND; String pointcut(); String cflow() default ""; }
The
@Bind
annotation takes three parameters:
type
, valid values are AdviceType.AROUND
,
AdviceType.BEFORE
, AdviceType.AFTER
,
AdviceType.THROWING
and AdviceType.FINALLY
.
See Chapter 4, Advices
for a description of the different advice types.
If omitted, the default is an around advice.
pointcut
, which is a pointcut expression resolving to the
joinpoints you want to bind an aspect/interceptor to
cflow
, which is optional. If defined it must resolve to
the name of a defined cflow.)
In the case of a binding to an advice in an aspect class, the internal name of the binding becomes:
<name of the aspect class>.<the name of the advice method>
In the case of a binding to an
Interceptor
or
AspectFactory
implementation, the internal name of the binding becomes:
<name of the Interceptor/AspectFactory implementation class>
An example of a binding using an advice method in an aspect class:
package com.mypackage; import org.jboss.aop.Bind; @Aspect (scope = Scope.PER_VM) public class MyAspect { @PointcutDef ("(execution(* org.blah.Foo->someMethod()) \ OR execution(* org.blah.Foo->otherMethod()))") public static Pointcut fooMethods; @Bind (pointcut="com.mypackage.MyAspect.fooMethods") public Object myAdvice(Invocation invocation) { return invocation.invokeNext(); } @Bind (pointcut="execution("* org.blah.Bar->someMethod())") public Object myAdvice(Invocation invocation) { return invocation.invokeNext(); } }
The equivalent using XML configuration would be:
<aop> <aspect class="com.mypackage.MyAspect" scope="PER_VM"/> <pointcut name="com.mypackage.MyAspect.fooMethods" expr="(execution("* org.blah.Foo->someMethod()) OR \ execution("* org.blah.Foo->otherMethod()))" /> <bind pointcut="com.mypackage.MyAspect.fooMethods"> <advice name="myAdvice" aspect="com.mypackage.MyAspect"> </bind> <bind pointcut="execution("* org.blah.Bar->someMethod())"> <advice name="otherAdvice" aspect="com.mypackage.MyAspect"> </bind> </aop>
Revisiting the examples above in the @InterceptorDef section, now that we know what @Bind means, the equivalent using XML configuration would be:
<aop> <interceptor class="com.mypackage.MyInterceptor" scope="PER_VM"/> <interceptor factory="com.mypackage.MyInterceptorFactory" scope="PER_VM"/> <bind pointcut="execution("* com.blah.Test->test2(..)"> <interceptor-ref name="com.mypackage.MyInterceptor"/> </bind> <bind pointcut="execution("* com.blah.Test->test2(..)"> <interceptor-ref name="com.mypackage.MyInterceptorFactory"/> </bind> </aop>