99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

Hasor 攔截器

2018-10-09 10:34 更新

在 Hasor 中,一共有三種不同的方式實(shí)現(xiàn)請(qǐng)求攔截。

  1. 通過(guò) Aop 實(shí)現(xiàn)請(qǐng)求攔截器
  2. 通過(guò) InvokerFilter 接口攔截請(qǐng)求
  3. 通過(guò) javax.servlet.Filter 接口攔截請(qǐng)求

其中 InvokerFilter 接口和 Filter 接口的工作方式和原理是等價(jià)的,只是用了不同的接口結(jié)構(gòu)。

第一種 Aop 方式,這種方式是利用了 Hasor 原本的 Aop 功能充當(dāng)攔截器。好處是用起來(lái)最簡(jiǎn)單,例如下面這段例子:

@Aop(CountInterceptor.class)
@MappingTo("/helloAcrion.do")
public class HelloAcrion extends WebController {
    public void execute(RenderInvoker invoker, @Params() ParamsFormBean formBean){
        ...
    }
}
public class CountInterceptor implements MethodInterceptor {
    public Object invoke(MethodInvocation invocation) throws Throwable {
        try {
            System.out.println("before... " + invocation.getMethod().getName());
            Object returnData = invocation.proceed();
            System.out.println("after...");
            return returnData;
        } catch (Exception e) {
            System.out.println("throw...");
            throw e;
        }
    }
}

第二種,用前一章節(jié)介紹的 InvokerFilter 接口。這種方式的最大優(yōu)點(diǎn)是您可以在攔截器中直接拿到 Invoker 接口對(duì)象,例如:

public class MyInvokerFilter implements InvokerFilter {
    public void init(InvokerConfig config) throws Throwable {
        ...
    }
    public void doInvoke(Invoker invoker, InvokerChain chain) throws Throwable {
        try {
            // before
            chain.doNext(invoker);
            // after
        } catch (Throwable e) {
            // error
            throw e;
        }
    }
    public void destroy() {
        ...
    }
}


第三種,用傳統(tǒng)的 J2EE的 Filter 充當(dāng)攔截器。例如:

public class MyFilter implements Filter {
    ...
}


第二種和第三種,方式都需要您對(duì)攔截器進(jìn)行聲明注冊(cè):

public class StartModule extends WebModule {
    public void loadModule(WebApiBinder apiBinder) throws Throwable {
        ...
        apiBinder.filter("/*").through(MyInvokerFilter.class);
        apiBinder.jeeFilter("/*").through(MyFilter.class);
        ...
    }
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)