在講spring boot之前,我們先了解一下過濾器和攔截器。這兩者在功能方面很類似,但是在具體技術(shù)實(shí)現(xiàn)方面,差距還是比較大的。在分析兩者的區(qū)別之前,我們先理解一下aop的概念,aop不是一種具體的技術(shù),而是一種編程思想。在面向?qū)ο缶幊痰倪^程中,我們很容易通過繼承、多態(tài)來解決縱向擴(kuò)展。 但是對于橫向的功能,比如,在所有的service方法中開啟事務(wù),或者統(tǒng)一記錄日志等功能,面向?qū)ο蟮氖菬o法解決的。所以aop——面向切面編程其實(shí)是面向?qū)ο缶幊趟枷氲囊粋€補(bǔ)充。而我們今天講的過濾器和攔截器都屬于面向切面編程的具體實(shí)現(xiàn)。而兩者的主要區(qū)別包括以下幾個方面:
1、filter是依賴于servlet容器,屬于servlet規(guī)范的一部分,而攔截器則是獨(dú)立存在的,可以在任何情況下使用。
2、filter的執(zhí)行由servlet容器回調(diào)完成,而攔截器通常通過動態(tài)代理的方式來執(zhí)行。
3、filter的生命周期由servlet容器管理,而攔截器則可以通過ioc容器來管理,因此可以通過注入等方式來獲取其他bean的實(shí)例,因此使用會更方便。
二、過濾器的配置
現(xiàn)在我們通過過濾器來實(shí)現(xiàn)記錄請求執(zhí)行時間的功能,其實(shí)現(xiàn)如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class logcostfilter implements filter { @override public void init(filterconfig filterconfig) throws servletexception { } @override public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception { long start = system.currenttimemillis(); filterchain.dofilter(servletrequest,servletresponse); system.out.println( "execute cost=" +(system.currenttimemillis()-start)); } @override public void destroy() { } } |
這段代碼的邏輯比較簡單,就是在方法執(zhí)行前先記錄時間戳,然后通過過濾器鏈完成請求的執(zhí)行,在返回結(jié)果之間計(jì)算執(zhí)行的時間。這里需要主要,這個類必須繼承filter類,這個是servlet的規(guī)范,這個跟以前的web項(xiàng)目沒區(qū)別。但是,有了過濾器類以后,以前的web項(xiàng)目可以在web.xml中進(jìn)行配置,但是spring boot項(xiàng)目并沒有web.xml這個文件,那怎么配置?在spring boot中,我們需要filterregistrationbean來完成配置。其實(shí)現(xiàn)過程如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
@configuration public class filterconfig { @bean public filterregistrationbean registfilter() { filterregistrationbean registration = new filterregistrationbean(); registration.setfilter( new logcostfilter()); registration.addurlpatterns( "/*" ); registration.setname( "logcostfilter" ); registration.setorder( 1 ); return registration; } } |
這樣配置就完成了,需要配置的選項(xiàng)主要包括實(shí)例化filter類,然后指定url的匹配模式,設(shè)置過濾器名稱和執(zhí)行順序,這個過程和在web.xml中配置其實(shí)沒什么區(qū)別,只是形式不同而已。現(xiàn)在我們可以啟動服務(wù)器訪問任意url:
大家可以看到上面的配置已經(jīng)生效了。除了通過 filterregistrationbean 來配置以外,還有一種更直接的辦法,直接通過注解就可以完成了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@webfilter (urlpatterns = "/*" , filtername = "logfilter2" ) public class logcostfilter2 implements filter { @override public void init(filterconfig filterconfig) throws servletexception { } @override public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception { long start = system.currenttimemillis(); filterchain.dofilter(servletrequest, servletresponse); system.out.println( "logfilter2 execute cost=" + (system.currenttimemillis() - start)); } @override public void destroy() { } } |
這里直接用@webfilter就可以進(jìn)行配置,同樣,可以設(shè)置url匹配模式,過濾器名稱等。這里需要注意一點(diǎn)的是@webfilter這個注解是servlet3.0的規(guī)范,并不是spring boot提供的。除了這個注解以外,我們還需在配置類中加另外一個注解:@servletcomponetscan,指定掃描的包。
1
2
3
4
5
6
7
8
|
@springbootapplication @mapperscan ( "com.pandy.blog.dao" ) @servletcomponentscan ( "com.pandy.blog.filters" ) public class application { public static void main(string[] args) throws exception { springapplication.run(application. class , args); } } |
現(xiàn)在,我們再來訪問一下任意url:
可以看到,我們配置的兩個過濾器都生效了。細(xì)心的讀者會發(fā)現(xiàn),第二個filter我們并沒有指定執(zhí)行的順序,但是卻在第一個filter之前執(zhí)行。這里需要解釋一下,@webfilter這個注解并沒有指定執(zhí)行順序的屬性,其執(zhí)行順序依賴于filter的名稱,是根據(jù)filter類名(注意不是配置的filter的名字)的字母順序倒序排列,并且@webfilter指定的過濾器優(yōu)先級都高于filterregistrationbean配置的過濾器。有興趣的朋友可以自己實(shí)驗(yàn)一下。
三、攔截器的配置
上面我們已經(jīng)介紹了過濾器的配置方法,接下來我們再來看看如何配置一個攔截器。我們使用攔截器來實(shí)現(xiàn)上面同樣的功能,記錄請求的執(zhí)行時間。首先我們實(shí)現(xiàn)攔截器類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class logcostinterceptor implements handlerinterceptor { long start = system.currenttimemillis(); @override public boolean prehandle(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, object o) throws exception { start = system.currenttimemillis(); return true ; } @override public void posthandle(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, object o, modelandview modelandview) throws exception { system.out.println( "interceptor cost=" +(system.currenttimemillis()-start)); } @override public void aftercompletion(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, object o, exception e) throws exception { } } |
這里我們需要實(shí)現(xiàn)handlerinterceptor這個接口,這個接口包括三個方法,prehandle是請求執(zhí)行前執(zhí)行的,posthandler是請求結(jié)束執(zhí)行的,但只有prehandle方法返回true的時候才會執(zhí)行,aftercompletion是視圖渲染完成后才執(zhí)行,同樣需要prehandle返回true,該方法通常用于清理資源等工作。除了實(shí)現(xiàn)上面的接口外,我們還需對其進(jìn)行配置:
1
2
3
4
5
6
7
8
9
|
@configuration public class interceptorconfig extends webmvcconfigureradapter { @override public void addinterceptors(interceptorregistry registry) { registry.addinterceptor( new logcostinterceptor()).addpathpatterns( "/**" ); super .addinterceptors(registry); } } |
這里我們繼承了webmvcconfigureradapter,看過前面的文章的朋友應(yīng)該已經(jīng)見過這個類了,在進(jìn)行靜態(tài)資源目錄配置的時候我們用到過這個類。這里我們重寫了addinterceptors這個方法,進(jìn)行攔截器的配置,主要配置項(xiàng)就兩個,一個是指定攔截器,第二個是指定攔截的url。現(xiàn)在我們再啟動系統(tǒng)訪問任意一個url:
可以看到,我們通過攔截器實(shí)現(xiàn)了同樣的功能。不過這里還要說明一點(diǎn)的是,其實(shí)這個實(shí)現(xiàn)是有問題的,因?yàn)閜rehandle和posthandle是兩個方法,所以我們這里不得不設(shè)置一個共享變量start來存儲開始值,但是這樣就會存在線程安全問題。當(dāng)然,我們可以通過其他方法來解決,比如通過threadlocal就可以很好的解決這個問題,有興趣的同學(xué)可以自己實(shí)現(xiàn)。不過通過這一點(diǎn)我們其實(shí)可以看到,雖然攔截器在很多場景下優(yōu)于過濾器,但是在這種場景下,過濾器比攔截器實(shí)現(xiàn)起來更簡單。
四、總結(jié)
本文主要對基于spring boot對過濾器和攔截器的配置進(jìn)行的講解。無論是過濾器還是攔截器都屬于aop(面向切面編程)思想的具體實(shí)現(xiàn)。除了這兩種實(shí)現(xiàn)我們還見過另一種更靈活的aop實(shí)現(xiàn)技術(shù),即aspect,我們可以通過aspect來完成更多更強(qiáng)大的功能。這個后續(xù)再給大家分享。
原文鏈接:http://www.cnblogs.com/paddix/p/8365558.html