Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

4.2 Interceptor


May 14, 2021 JFinal manual



Interceptor intercepts methods and provides an opportunity to add faceted code before and after methods to achieve the core objectives of AOP. T he Interceptor interface has only one method, void intercept (Invocation inv). Here's a simple example:

public class DemoInterceptor implements Interceptor {
	public void intercept(Invocation inv) { System.out.println("Before method invoking"); inv.invoke();
	System.out.println("After method invoking");
	}
}

DemoInterceptor in the above code intercepts the target method and outputs text to the console before and after the target method call. The line of inv.invoke() is a call to the target method, and inserting faceted code before and after this line of code makes it easy to implement AOP.


nvocation, as the only parameter in the Interceptor interface intercept method, provides a number of convenient methods to use in interceptors. Here's how to do it in Invocation:

Method

Describe

void invoke()

Pass this call, calling the remaining interceptor with the target method

Controller getController()

Get Controller objects called by Action (for control layer blocking only)

String getActionKey()

Get action key value for Action call (control layer blocking only)

String getControllerKey()

Get the controller key value of action call (for control layer blocking only)

String getViewPath()

Get the view path for Action calls (for control layer blocking only)

<T> T getTarget()

Gets the object to which the intercepted method belongs

Method getMethod()

Gets the Method object of the intercepted method

String getMethodName()

Gets the method name of the intercepted method

Object[] getArgs()

Gets all the parameter values of the intercepted method

Object getArg(int)

Gets the parameter value of the serial number specified by the intercepted method

<T> T getReturnValue()

Gets the return value of the intercepted method

void setArg(int)

Sets the parameter value of the serial number specified by the intercepted method

void setReturnValue(Object)

Sets the return value of the intercepted method

boolean isActionInvocation()

Determines whether it is an Action call, or whether it is a control layer intercept