img
Creating Advice in Spring
Spring supports six different flavors of advice, described in Table 6-2.
Table 6-2. Advice Types in Spring
Advice Name
Interface
Description
org.springframework.aop
Before
Using before advice, you can perform custom processing
.MethodBeforeAdvice
before a joinpoint executes. Because a joinpoint in
Spring is always a Method Invocation, this essentially
allows you to perform preprocessing before the method
executes. A before advice has full access to the target of
the Method Invocation as well as the arguments passed
to the method, but it has no control over the execution
of the method itself. In case a before advice throws an
exception, further execution of the interceptor chain (as
well as the target method) will be aborted, and the
exception will propagate back up the interceptor chain.
org.springframework.aop
After returning
After-returning advice is executed after the Method
.AfterReturningAdvice
Invocation at the joinpoint has finished executing and
has returned a value. The after-returning advice has
access to the target of the Method Invocation, the
arguments passed to the method, and the return value as
well. Because the method has already executed when the
after-returning advice is invoked, it has no control over
the Method Invocation at all. In case the target method
throws an exception, the after-returning advice will not
be run, and the exception will be propagated up to the
call stack as usual.
org.springframework.aop
After (finally)
After-returning advice is executed only when the advised
.AfterAdvice
method completes normally. However, the after (finally)
advice will be executed no matter the result of the
advised method. The advice is executed even when the
advised method fails and an exception is thrown.
org.aopalliance.intercept
Around
In Spring, around advice is modeled using the AOP
.MethodInterceptor
Alliance standard of a method interceptor. Your advice is
allowed to execute before and after the Method
Invocation, and you can control the point at which the
Method Invocation is allowed to proceed. You can
choose to bypass the method altogether if you want,
providing your own implementation of the logic.
org.springframework.aop
Throws
Throws advice is executed after a Method Invocation
.ThrowsAdvice
returns, but only if that invocation threw an exception. It
is possible for a throws advice to catch only specific
exceptions, and if you choose to do so, you can access the
method that threw the exception, the arguments passed
into the invocation, and the target of the invocation.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home