常用用于實現(xiàn)攔截的有:Filter、HandlerInterceptor、MethodInterceptor
第一種Filter屬于Servlet提供的,后兩者是spring提供的,HandlerInterceptor屬于Spring MVC項目提供的,用來攔截請求,在MethodInterceptor之前執(zhí)行。
實現(xiàn)一個HandlerInterceptor可以實現(xiàn)接口HandlerInterceptor,也可以繼承HandlerInterceptorAdapter類,兩種方法一樣。這個不在本文范圍,具體使用之前已經(jīng)寫過SpringBoot的(SpringMVC的使用一樣,區(qū)別只是配置)
MethodInterceptor是AOP項目中的攔截器,它攔截的目標(biāo)是方法,即使不是Controller中的方法。
實現(xiàn)MethodInterceptor攔截器大致也分為兩種,一種是實現(xiàn)MethodInterceptor接口,另一種利用Aspect的注解或配置。
關(guān)于實現(xiàn)MethodInterceptor接口的這種方法,還需要在配置文件中做配置,在SpringMVC中使用還可以,在SpringBoot中使用起來似乎沒有那么方便。
本文主要還是說Aspect注解方式,個人覺得這種方法才比較靈活,與配置與工程整個代碼都沒有耦合(你添加一個類,做幾個注解就可以用了,無需在其他地方再做什么),更易應(yīng)用。
首先為你的SpringBoot項目添加maven依賴,讓其支持aop(其實就是自動引入aop需要的一些jar)
在pom.xml中添加依賴:
1
2
3
4
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-aop</ artifactId > </ dependency > |
然后創(chuàng)建Aspect測試類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.shanhy.sboot.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Aspect // FOR AOP @Order (- 99 ) // 控制多個Aspect的執(zhí)行順序,越小越先執(zhí)行 @Component public class TestAspect { @Before ( "@annotation(test)" ) // 攔截被TestAnnotation注解的方法;如果你需要攔截指定package指定規(guī)則名稱的方法,可以使用表達(dá)式execution(...),具體百度一下資料一大堆 public void beforeTest(JoinPoint point, TestAnnotation test) throws Throwable { System.out.println( "beforeTest:" + test.name()); } @After ( "@annotation(test)" ) public void afterTest(JoinPoint point, TestAnnotation test) { System.out.println( "afterTest:" + test.name()); } } |
這樣就完成了,然后創(chuàng)建一個Controller驗證一下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@RestController @RequestMapping ( "/test" ) public class TestController { @TestAnnotation (name= "abc" ) @RequestMapping ( "/show" ) public String show() { return "OK" ; } @RequestMapping ( "/show2" ) public String show2() { return "OK2" ; } } |
此時我們訪問show請求,就會被攔截,控制臺會打印輸出。如果請求show2則不會被攔截。
注意:
1、在application.properties中也不需要添加spring.aop.auto=true,因為這個默認(rèn)就是true,值為true就是啟用@EnableAspectJAutoProxy注解了。
2、你不需要手工添加 @EnableAspectJAutoProxy 注解。
3、當(dāng)你需要使用CGLIB來實現(xiàn)AOP的時候,需要配置spring.aop.proxy-target-class=true,這個默認(rèn)值是false,不然默認(rèn)使用的是標(biāo)準(zhǔn)Java的實現(xiàn)。
其實aspectj的攔截器會被解析成AOP中的advice,最終被適配成MethodInterceptor,這些都是Spring自動完成的,如果你有興趣,詳細(xì)的過程請參考springAOP的實現(xiàn)。
關(guān)于集中攔截方法的區(qū)別總結(jié):
HandlerInterceptoer攔截的是請求地址,所以針對請求地址做一些驗證、預(yù)處理等操作比較合適。當(dāng)你需要統(tǒng)計請求的響應(yīng)時間時MethodInterceptor將不太容易做到,因為它可能跨越很多方法或者只涉及到已經(jīng)定義好的方法中一部分代碼。
MethodInterceptor利用的是AOP的實現(xiàn)機(jī)制,在本文中只說明了使用方式,關(guān)于原理和機(jī)制方面介紹的比較少,因為要說清楚這些需要講出AOP的相當(dāng)一部分內(nèi)容。在對一些普通的方法上的攔截HandlerInterceptoer就無能為力了,這時候只能利用AOP的MethodInterceptor。
Filter是Servlet規(guī)范規(guī)定的,不屬于spring框架,也是用于請求的攔截。但是它適合更粗粒度的攔截,在請求前后做一些編解碼處理、日志記錄等。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/catoop/article/details/71541612