Before


Before annotation is used to configure interceptors. This annotation can configure Class and Method level interceptors. The following is a code example:


1475634939249992.png

As shown in the above code, Before can configure the interceptor at Class level and Method level. The former will intercept all methods in this class, and the latter will only intercept this method. In addition, Before can configure multiple interceptors at the same time, just use it within curly brackets and separate multiple interceptors with commas.

In addition to Class and Method level interceptors, JFinal also supports global interceptor and Inject interceptor (Inject

interception will be introduced later), global interceptor It is divided into control layer global interceptor and business layer global interceptor. The former intercepts all Action methods in the control layer, and the latter intercepts all methods in the business layer.

Global interceptors need to be configured in YourJFinalConfig. The following is a configuration example:


public class AppConfig extends JFinalConfig {
	public void configInterceptor(Interceptors me) {
	// 添加控制层全局拦截器
	me.addGlobalActionInterceptor(new GlobalActionInterceptor());
 
	// 添加业务层全局拦截器
	me.addGlobalServiceInterceptor(new GlobalServiceInterceptor());
 
	// 为兼容老版本保留的方法,功能与addGlobalActionInterceptor完全一样
	me.add(new GlobalActionInterceptor());
	}
}


When a Method is Intercepted by multiple levels of interceptors, the execution order of each level of the interceptor is: Global, Inject, Class, Method. If there are multiple interceptors in the same level, the execution order in the same level is: configured in front Execute first.