|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT | |||||||||
@Retention(value=RUNTIME) @Target(value=METHOD) public @interface AroundInvoke
Designates a method to intercept invocations on a beanlet. A class that
declares a method marked with the ArroundInvoke annotation is said to
be an interceptor class. Interceptor classes are instantiated either by their
(typically implicit) sole constructor, or by contructor injection.
Example (A) shows how to implement an interceptor using this annotation.
ArroundInvoke annotation can be applied on methods of any class,
including classes implementing a beanlet. Such interceptors are so-called
inner interceptors. An inner interceptor is automatically
installed for all business methods of a beanlet. It is placed at the tail of
the interceptor chain. It cannot be excluded by the
ExcludeClassInterceptors annotation. An example of an inner
interceptor can be found at example (B).
AroundInvoke annotation is applied MUST
fulfill all of the following criteria:
InterceptorContext object as parameter.
void
AroundInvoke is applied MAY be
public, protected, package private or private.
static.
final.
ArroundInvoke
annotation:
public class BaseInterceptor {
@AroundInvoke
public Object wrap(InvocationContext ctx) throws Exception {
try {
// Run code before the invocation...
return ctx.proceed();
} finally {
// Run code after the invocation...
}
}
}
(B) Example of a beanlet with an inner interceptor:
public class ExampleBeanlet {
@Factory
public Object getInstance() {
return new Object();
}
@ArroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
try {
// Run code before the invocation...
return ctx.proceed();
} finally {
// Run code after the invocation...
}
}
}
(C) Example of an interceptor class that extends the interceptor class
listed at example (A). The BaseInterceptor.wrap() method preceeds
the ExtendedInterceptor.wrapToo() method in the interceptor chain:
public class ExtendedInterceptor extends BaseInterceptor {
@AroundInvoke
public Object wrapToo(InvocationContext ctx) throws Exception {
try {
// Run code before the invocation...
return ctx.proceed();
} finally {
// Run code after the invocation...
}
}
}
(D) Example of an extended interceptor class of which the interceptor
method overrides the interceptor method of its superclass (listed at (A)).
The BaseInterceptor.wrap() method is excluded from the interceptor
chain, it is not invoked. The ExtendedInterceptor.wrap() method
however, is included in the interceptor chain.
public class ExtendedInterceptor extends BaseInterceptor {
@AroundInvoke
public Object wrap(InvocationContext ctx) throws Exception {
try {
// Run code before the invocation...
return ctx.proceed();
} finally {
// Run code after the invocation...
}
}
}
More information on how to apply interceptors can be found at the
javadoc of the Interceptor interface.
<beanlets xmlns="http://beanlet.org/schema/beanlet"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://beanlet.org/schema/beanlet http://beanlet.org/schema/beanlet/beanlet_1_0.xsd">
<beanlet name="foo" type="com.acme.Foo">
<around-invoke method="bar"/>
</beanlet>
</beanlets>
Interceptor,
Interceptors
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT | |||||||||