Spring's use and Spring3.2 Controller enhances @ControllerAdvice

 After this tag xml configuration, spring automatically to scan the following base-pack or sub-packets following java file, if the scan to these annotations @Component, @ Controller, @ Service class etc., put these classes registered as bean

Note: If you configure the <context: component-scan> then <context: annotation-config /> tag can no longer xml configuration because the former includes the latter. Further <context: annotation-config /> tag also provides two sub

1. <context: include-filter> (inclusive)

2. <context: exclude-filter> (negative)

In the description of these two sub-label before, first talk about <context: component-scan> have a use-default-filters property, change the default attribute is true, which means that scans all of the standard package has designated under the @Component class, and registered as a bean. @Component is sub-notes @ Service, @ Reposity and so on. So if only so written in the configuration file

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

 Use-default-filter is true that at this time will all be at the java class or sub-base-package bag package scanning and matching the java class registered as bean.

 

 This scan can be found granularity bit too much, if you want to specify the following Controller scan package, how to do? At this time, the sub-label <context: incluce-filter> to play a useless. As follows

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

This will only scan for the java class @Controller in the base-package designated and registered as bean

But because the use-dafault-filter does not specify above, it is by default true, so when the above configuration is changed as shown below and they will produce (note change in base-package package worth) and you expect inconsistent results

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

At this time, spring only scanned @Controller, java class designation also scan the package is located under the sub-packets of the service package annotations @Service

Include-filter specified at this time not play a role, as long as the use-default-filter disposed on it to false. This can be avoided in the base-packeage configure multiple package names this is not a very elegant way to solve this problem.

Also in the project I was involved in base-package can be found in the package specified in sub-package does not contain any notes, so do not scan, then you can specify <context: exclude-filter> to filter, this package does not You need to be scanned. Based on the above description

Case Use-dafault-filters = "false" is: <context: exclude-filter> is not specified scan, <context: include-filter> specified scan

Next, look at the code section:

Java code  
@ControllerAdvice  
public class ControllerAdviceTest {  
  
    @ModelAttribute  
    public newUser the User () {  
        System.out.println ( "============ @RequestMapping annotations applied to all methods, before it performs a return value into the Model ");  
        return the User new new ();  
    }  
  
    @InitBinder  
    public void of an initBinder (WebDataBinder Binder) {  
        System.out.println (" ============ @RequestMapping annotations applied to all methods, initialization data before it performs a binder ");  
    }  
  
    @ExceptionHandler (UnauthenticatedException.class)  
    @ResponseStatus (HttpStatus.UNAUTHORIZED)  
    public String processUnauthenticatedException (NativeWebRequest Request, UnauthenticatedException E) {  
        System.out.println ( "=========== method is applied to all annotated @RequestMapping, UnauthenticatedException thrown at the abnormal execution");  
        return "viewName"; // return a logical view name  
    }  
}  
 

If your spring-mvc configuration file using the following scanned bean

Java code  
<context: Component Base-Package-Scan = "com.sishuok.es" use-default-Filters = "to false">  
       <context: the include filter-type = "Annotation" = expression The "org.springframework.stereotype.Controller "/>  
   </ context: Component-Scan>  
 need to include it @ControllerAdvice otherwise ineffective:

Java代码  
<context:component-scan base-package="com.sishuok.es" use-default-filters="false">  
       <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>  

Guess you like

Origin www.cnblogs.com/exmyth/p/11058083.html