spring-framework / org.aopalliance.intercept / MethodInterceptor

MethodInterceptor

@FunctionalInterface interface MethodInterceptor : Interceptor

Intercepts calls on an interface on its way to the target. These are nested "on top" of the target.

The user should implement the #invoke(MethodInvocation) method to modify the original behavior. E.g. the following class implements a tracing interceptor (traces all the calls on the intercepted method(s)):

 class TracingInterceptor implements MethodInterceptor { Object invoke(MethodInvocation i) throws Throwable { System.out.println("method "+i.getMethod()+" is called on "+ i.getThis()+" with args "+i.getArguments()); Object ret=i.proceed(); System.out.println("method "+i.getMethod()+" returns "+ret); return ret; } } 

Author
Rod Johnson

Functions

invoke

abstract fun invoke(invocation: MethodInvocation): Any

Implement this method to perform extra treatments before and after the invocation. Polite implementations would certainly like to invoke Joinpoint#proceed().

Inheritors

AbstractTraceInterceptor

abstract class AbstractTraceInterceptor : MethodInterceptor, Serializable

Base MethodInterceptor implementation for tracing.

By default, log messages are written to the log for the interceptor class, not the class which is being intercepted. Setting the useDynamicLogger bean property to true causes all log messages to be written to the Log for the target class being intercepted.

Subclasses must implement the invokeUnderTrace method, which is invoked by this class ONLY when a particular invocation SHOULD be traced. Subclasses should write to the Log instance provided.

AfterReturningAdviceInterceptor

open class AfterReturningAdviceInterceptor : MethodInterceptor, AfterAdvice, Serializable

Interceptor to wrap an org.springframework.aop.AfterReturningAdvice. Used internally by the AOP framework; application developers should not need to use this class directly.

AsyncExecutionInterceptor

open class AsyncExecutionInterceptor : AsyncExecutionAspectSupport, MethodInterceptor, Ordered

AOP Alliance MethodInterceptor that processes method invocations asynchronously, using a given org.springframework.core.task.AsyncTaskExecutor. Typically used with the org.springframework.scheduling.annotation.Async annotation.

In terms of target method signatures, any parameter types are supported. However, the return type is constrained to either void or java.util.concurrent.Future. In the latter case, the Future handle returned from the proxy will be an actual asynchronous Future that can be used to track the result of the asynchronous method execution. However, since the target method needs to implement the same signature, it will have to return a temporary Future handle that just passes the return value through (like Spring's org.springframework.scheduling.annotation.AsyncResult or EJB 3.1's javax.ejb.AsyncResult).

When the return type is java.util.concurrent.Future, any exception thrown during the execution can be accessed and managed by the caller. With void return type however, such exceptions cannot be transmitted back. In that case an AsyncUncaughtExceptionHandler can be registered to process such exceptions.

As of Spring 3.1.2 the AnnotationAsyncExecutionInterceptor subclass is preferred for use due to its support for executor qualification in conjunction with Spring's @Async annotation.

ConcurrencyThrottleInterceptor

open class ConcurrencyThrottleInterceptor : ConcurrencyThrottleSupport, MethodInterceptor, Serializable

Interceptor that throttles concurrent access, blocking invocations if a specified concurrency limit is reached.

Can be applied to methods of local services that involve heavy use of system resources, in a scenario where it is more efficient to throttle concurrency for a specific service rather than restricting the entire thread pool (e.g. the web container's thread pool).

The default concurrency limit of this interceptor is 1. Specify the "concurrencyLimit" bean property to change this value.

EventPublicationInterceptor

open class EventPublicationInterceptor : MethodInterceptor, ApplicationEventPublisherAware, InitializingBean

MethodInterceptor that publishes an ApplicationEvent to all ApplicationListeners registered with an ApplicationEventPublisher after each successful method invocation.

Note that this interceptor is only capable of publishing stateless events configured via the "applicationEventClass" property.

ExposeInvocationInterceptor

open class ExposeInvocationInterceptor : MethodInterceptor, PriorityOrdered, Serializable

Interceptor that exposes the current org.aopalliance.intercept.MethodInvocation as a thread-local object. We occasionally need to do this; for example, when a pointcut (e.g. an AspectJ expression pointcut) needs to know the full invocation context.

Don't use this interceptor unless this is really necessary. Target objects should not normally know about Spring AOP, as this creates a dependency on Spring API. Target objects should be plain POJOs as far as possible.

If used, this interceptor will normally be the first in the interceptor chain.

HessianClientInterceptor

open class HessianClientInterceptor : UrlBasedRemoteAccessor, MethodInterceptor

org.aopalliance.intercept.MethodInterceptor for accessing a Hessian service. Supports authentication via username and password. The service URL must be an HTTP URL exposing a Hessian service.

Hessian is a slim, binary RPC protocol. For information on Hessian, see the Hessian website Note: As of Spring 4.0, this client requires Hessian 4.0 or above.

Note: There is no requirement for services accessed with this proxy factory to have been exported using Spring's HessianServiceExporter, as there is no special handling involved. As a consequence, you can also access services that have been exported using Caucho's com.caucho.hessian.server.HessianServlet.

JmsInvokerClientInterceptor

open class JmsInvokerClientInterceptor : MethodInterceptor, InitializingBean

org.aopalliance.intercept.MethodInterceptor for accessing a JMS-based remote service.

Serializes remote invocation objects and deserializes remote invocation result objects. Uses Java serialization just like RMI, but with the JMS provider as communication infrastructure.

To be configured with a javax.jms.QueueConnectionFactory and a target queue (either as javax.jms.Queue reference or as queue name).

Thanks to James Strachan for the original prototype that this JMS invoker mechanism was inspired by!

JndiRmiClientInterceptor

open class JndiRmiClientInterceptor : JndiObjectLocator, MethodInterceptor, InitializingBean

org.aopalliance.intercept.MethodInterceptor for accessing RMI services from JNDI. Typically used for RMI-IIOP but can also be used for EJB home objects (for example, a Stateful Session Bean home). In contrast to a plain JNDI lookup, this accessor also performs narrowing through PortableRemoteObject.

With conventional RMI services, this invoker is typically used with the RMI service interface. Alternatively, this invoker can also proxy a remote RMI service with a matching non-RMI business interface, i.e. an interface that mirrors the RMI service methods but does not declare RemoteExceptions. In the latter case, RemoteExceptions thrown by the RMI stub will automatically get converted to Spring's unchecked RemoteAccessException.

The JNDI environment can be specified as "jndiEnvironment" property, or be configured in a jndi.properties file or as system properties. For example:

<property name="jndiEnvironment"> <props> <prop key="java.naming.factory.initial">com.sun.jndi.cosnaming.CNCtxFactory</prop> <prop key="java.naming.provider.url">iiop://localhost:1050</prop> </props> </property>

MethodBeforeAdviceInterceptor

open class MethodBeforeAdviceInterceptor : MethodInterceptor, Serializable

Interceptor to wrap am org.springframework.aop.MethodBeforeAdvice. Used internally by the AOP framework; application developers should not need to use this class directly.

MethodValidationInterceptor

open class MethodValidationInterceptor : MethodInterceptor

An AOP Alliance MethodInterceptor implementation that delegates to a JSR-303 provider for performing method-level validation on annotated methods.

Applicable methods have JSR-303 constraint annotations on their parameters and/or on their return value (in the latter case specified at the method level, typically as inline annotation).

E.g.: public @NotNull Object myValidMethod(@NotNull String arg1, @Max(10) int arg2)

Validation groups can be specified through Spring's Validated annotation at the type level of the containing target class, applying to all public service methods of that class. By default, JSR-303 will validate against its default group only.

As of Spring 5.0, this functionality requires a Bean Validation 1.1 provider.

OpenSessionInterceptor

open class OpenSessionInterceptor : MethodInterceptor, InitializingBean

Simple AOP Alliance MethodInterceptor implementation that binds a new Hibernate Session for each method invocation, if none bound before.

This is a simple Hibernate Session scoping interceptor along the lines of OpenSessionInViewInterceptor, just for use with AOP setup instead of MVC setup. It opens a new Session with flush mode "MANUAL" since the Session is only meant for reading, except when participating in a transaction.

PersistenceExceptionTranslationInterceptor

open class PersistenceExceptionTranslationInterceptor : MethodInterceptor, BeanFactoryAware, InitializingBean

AOP Alliance MethodInterceptor that provides persistence exception translation based on a given PersistenceExceptionTranslator.

Delegates to the given PersistenceExceptionTranslator to translate a RuntimeException thrown into Spring's DataAccessException hierarchy (if appropriate). If the RuntimeException in question is declared on the target method, it is always propagated as-is (with no translation applied).

RemoteInvocationTraceInterceptor

open class RemoteInvocationTraceInterceptor : MethodInterceptor

AOP Alliance MethodInterceptor for tracing remote invocations. Automatically applied by RemoteExporter and its subclasses.

Logs an incoming remote call as well as the finished processing of a remote call at DEBUG level. If the processing of a remote call results in a checked exception, the exception will get logged at INFO level; if it results in an unchecked exception (or error), the exception will get logged at WARN level.

The logging of exceptions is particularly useful to save the stacktrace information on the server-side rather than just propagating the exception to the client (who might or might not log it properly).

ThrowsAdviceInterceptor

open class ThrowsAdviceInterceptor : MethodInterceptor, AfterAdvice

Interceptor to wrap an after-throwing advice.

The signatures on handler methods on the ThrowsAdvice implementation method argument must be of the form: void afterThrowing([Method, args, target], ThrowableSubclass);

Only the last argument is required.

Some examples of valid methods would be:

public void afterThrowing(Exception ex)
public void afterThrowing(RemoteException)
public void afterThrowing(Method method, Object[] args, Object target, Exception ex)
public void afterThrowing(Method method, Object[] args, Object target, ServletException ex)

This is a framework class that need not be used directly by Spring users.