1. 程式人生 > >Jfinal 源碼分析之攔截器的使用

Jfinal 源碼分析之攔截器的使用

一次 stat ret 目前 核心 不同 手動 tex 包括

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);

Jfinal 源碼分析之攔截器的使用