Spring的使用及Spring3.2控制器增強@ControllerAdvice

 在xml配置了這個標籤後,spring可以自動去掃描base-pack下面或者子包下面的java文件,如果掃描到有@Component、 @Controller、@Service等這些註解的類,則把這些類註冊爲bean

注意:如果配置了<context:component-scan>那麼<context:annotation-config/>標籤就可以不用再xml中配置了,因爲前者包含了後者。另外<context:annotation-config/>還提供了兩個子標籤

1.        <context:include-filter>(包含)

2.       <context:exclude-filter>(排除)

在說明這兩個子標籤前,先說一下<context:component-scan>有一個use-default-filters屬性,改屬性默認爲true,這就意味着會掃描指定包下的全部的標有@Component的類,並註冊成bean.也就是@Component的子註解@Service,@Reposity等。所以如果僅僅是在配置文件中這麼寫

<context:component-scan base-package="tv.huan.weisp.web"/>

 Use-default-filter此時爲true那麼會對base-package包或者子包下的所有的進行java類進行掃描,並把匹配的java類註冊成bean。

 

 可以發現這種掃描的粒度有點太大,如果你只想掃描指定包下面的Controller,該怎麼辦?此時子標籤<context:incluce-filter>就起到了用武之地。如下所示

<context:component-scan base-package="tv.huan.weisp.web .controller">  

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   

   <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> 

</context:component-scan>  

這樣就會只掃描base-package指定下的有@Controller下的java類,並註冊成bean

但是因爲use-dafault-filter在上面並沒有指定,默認就爲true,所以當把上面的配置改成如下所示的時候,就會產生與你期望相悖的結果(注意base-package包值得變化)

<context:component-scan base-package="tv.huan.weisp.web ">  

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  

   <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  

</context:component-scan>  

此時,spring不僅掃描了@Controller,還掃描了指定包所在的子包service包下註解@Service的java類

此時指定的include-filter沒有起到作用,只要把use-default-filter設置成false就可以了。這樣就可以避免在base-packeage配置多個包名這種不是很優雅的方法來解決這個問題了。

另外在我參與的項目中可以發現在base-package指定的包中有的子包是不含有註解了,所以不用掃描,此時可以指定<context:exclude-filter>來進行過濾,說明此包不需要被掃描。綜合以上說明

Use-dafault-filters=”false”的情況下:<context:exclude-filter>指定的不掃描,<context:include-filter>指定的掃描

接下來看段代碼:

Java代碼  收藏代碼
  1. @ControllerAdvice  
  2. public class ControllerAdviceTest {  
  3.   
  4.     @ModelAttribute  
  5.     public User newUser() {  
  6.         System.out.println("============應用到所有@RequestMapping註解方法,在其執行之前把返回值放入Model");  
  7.         return new User();  
  8.     }  
  9.   
  10.     @InitBinder  
  11.     public void initBinder(WebDataBinder binder) {  
  12.         System.out.println("============應用到所有@RequestMapping註解方法,在其執行之前初始化數據綁定器");  
  13.     }  
  14.   
  15.     @ExceptionHandler(UnauthenticatedException.class)  
  16.     @ResponseStatus(HttpStatus.UNAUTHORIZED)  
  17.     public String processUnauthenticatedException(NativeWebRequest request, UnauthenticatedException e) {  
  18.         System.out.println("===========應用到所有@RequestMapping註解的方法,在其拋出UnauthenticatedException異常時執行");  
  19.         return "viewName"//返回一個邏輯視圖名  
  20.     }  
  21. }  

 

如果你的spring-mvc配置文件使用如下方式掃描bean

Java代碼  收藏代碼
  1. <context:component-scan base-package="com.sishuok.es" use-default-filters="false">  
  2.        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  3.    </context:component-scan>  

 需要把@ControllerAdvice包含進來,否則不起作用:

Java代碼  收藏代碼
  1. <context:component-scan base-package="com.sishuok.es" use-default-filters="false">  
  2.        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  3.        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  4.    </context:component-scan>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章