Jfinal 源码分析之拦截器的使用

Jfinal 源码分析之拦截器的使用

官方文档初始拦截器:

Interceptor可以对方法进行拦截,并提供机会再方法的前后添加切面代码,实现AOP的核心目标。

拦截器相关的类的分析:

Interceptor接口

定义了一个interceptor方法,参数为Invocation 类型,为拦截器中传递拦截器、目标方法。

InterceptorManager方法:

1、 管理控制层、业务层全局拦截器
2、 缓存业务层Class级拦截器数组。在业务层仅有injectInters、methodInters 数组未被整体缓存
3、 用于创建Inteceptor、组装Interceptor
4、 除手动new出来的inject拦截器以外,其他所有拦截器均为单例
5、 重点关注业务层拦截器组装性能,控制层所有拦截器一杯整体缓存。

注意:无法使用Method或Before对象缓存业务层Method级拦截器:
1、 不同对象或相同对象获取同一个class中同一个Method得到的对象id值不同
2、 不同对象获取同一个method之上的Before得到的对象id值不同

具体的拦截器(包括自定义的拦截器),需要继承Interceptor实现interceptor方法。

注意:必须调用 inv.invoke() 方法,才能将当前调用传递到后续的 Interceptor 与 Action
常见错误:目前为止仍有很多同学忘了调用 inv.invoke() 方法,造成 controller 中的 action 不会被执行。在此再次强调一次,一定要调用一次 inv.invoke(),除非是刻意不去调用剩下的拦截器与 action,这种情况仍然需要使用 inv.getController().render()/renderJson() 调用一下相关的 render() 方法为客户端响应数据。
Interceptor官方参考

Interceptors

Interceptors用于配置全局的动作拦截器和全局的服务拦截器

final public class Interceptors {
    
    /**
     * 与addGlobalActionInterceptor.类似,是用于兼容早期的Jfinal版本   */
    public Interceptors add(Interceptor globalActionInterceptor) {
        if (globalActionInterceptor == null) {
            throw new IllegalArgumentException("globalActionInterceptor can not be null.");
        }
        InterceptorManager.me().addGlobalActionInterceptor(globalActionInterceptor);
        return this;
    }
    
    /**
     * 添加全局的动作拦截器去拦截所有的动作    */
    public Interceptors addGlobalActionInterceptor(Interceptor globalActionInterceptor) {
        if (globalActionInterceptor == null) {
            throw new IllegalArgumentException("globalActionInterceptor can not be null.");
        }
        InterceptorManager.me().addGlobalActionInterceptor(globalActionInterceptor);
        return this;
    }
    
    /**
     *添加全局的服务拦截器用以拦截所有被aop增强器拦截的的方法 
     */
    public Interceptors addGlobalServiceInterceptor(Interceptor globalServiceInterceptor) {
        if (globalServiceInterceptor == null) {
            throw new IllegalArgumentException("globalServiceInterceptor can not be null.");
        }
        InterceptorManager.me().addGlobalServiceInterceptor(globalServiceInterceptor);
        return this;
    }
}

拦截器的配置


    public void configInterceptor(Interceptors me) { 
        //me.add(new DemoInterceptor());
    }

根据配置中的拦截器执行


    private static final Interceptors interceptors = new Interceptors();
    
    // prevent new Config();
    private Config() {
    }
    
    /*
     * Config order: constant, plugin, route, engine, interceptor, handler
     */
    static void configJFinal(JFinalConfig jfinalConfig) {
        jfinalConfig.configConstant(constants);         initLogFactory();   initEngine();
        
        configPluginWithOrder(1, jfinalConfig);
        jfinalConfig.configRoute(routes);
        
        configPluginWithOrder(2, jfinalConfig);
        jfinalConfig.configEngine(engine);
        
        configPluginWithOrder(3, jfinalConfig);
        jfinalConfig.configInterceptor(interceptors);
        
        configPluginWithOrder(4, jfinalConfig);
        jfinalConfig.configHandler(handlers);
        
        configPluginWithOrder(5, jfinalConfig);
    }

这是拦截器的执行顺序:

        configPluginWithOrder(3, jfinalConfig);
        jfinalConfig.configInterceptor(interceptors);

猜你喜欢

转载自www.cnblogs.com/Erma/p/10391469.html