SPRINGMVC Source Analysis (6) Anatomy defaultannotationhandlermapping

Source: Internet
Author: User
Tags i18n

Connect <SPRINGMVC Source code Analysis (5) Analyze the important components handlermapping> Continue to dissect handlermapping,defaultannotationhandlermapping is SPRINGMVC The most important handlermapping component in the Although it was abandoned after the spring3.1 version.

Includes 2 sections of content

    1. Defaultannotationhandlermapping anatomy

    2. Handlermapping's Interceptor

1.DefaultAnnotationHandlerMapping Profiling

In view of its important position, put down the structure diagram

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/8B/03/wKioL1hCKnWx4gYCAACX0CQYtFA954.png "title=" 111. PNG "alt=" Wkiol1hcknwx4gycaacx0cqytfa954.png "/>

public class Defaultannotationhandlermapping extends Abstractdetectingurlhandlermapping {//Whether a suffix is used to register the URL (for example, if registered/    Users, also registering/users.* and/users/private boolean usedefaultsuffixpattern = true; Cache handler and requestmapping conditional relationships, validating with private final map<class, requestmapping> cachedmappings = new hashmap<      Class, requestmapping> (); ...   }

1.1. Overriding the Determineurlsforhandler method

Abstractdetectingurlhandlermapping subclasses, overriding Determineurlsforhandler methods;

Called when Initapplicationcontext. Invokes the context for Determineurlsforhandler. Called during the initialization of the SPRINGMVC container.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/8B/03/wKioL1hCK1yhXCRbAABFzPcjfXs824.png "title=" 233. PNG "alt=" Wkiol1hck1yhxcrbaabfzpcjfxs824.png "/>

As summarized in the previous article, the main responsibilities of handlermapping

    1. Register handler. Can be an annotation, which can be an XML declaration.

    2. Generate URLs, there are many policies. Beanname, prefix, package name all can be used as reference

    3. Maintain mapping relationships

    4. The ability to match URLs

Defaultannotationhandlermapping is the same, Determineurlsforhandler method, according to the method name can be guessed, "for handler match URL." It's the same thing as the controllerclassnamehandlermapping,controllerbeannamehandlermapping. The difference is that they are based on XML definitions and are not flexible enough. Defaultannotationhandlermapping is more flexible and functionally powerful, and the name is obtained from the requestmapping annotations. Refer to the Determineurlsforhandlermethods method for details.

Generate Urlprotected string[] determineurlsforhandlermethods on a per-method basis (class<?> handlertype,  final boolean hastypelevelmapping)  {   String[] subclassResult =  Determineurlsforhandlermethods (Handlertype);   if  (subclassresult != null)   {      return subclassresult;   }   final  Set<String> urls = new LinkedHashSet<String> ();   set< Class<?>> handlertypes = new linkedhashset<class<?>> ();    handlertypes.add (Handlertype);    handlertypes.addall (Arrays.aslist ( Handlertype.getinterfaces ());   for  (class<?> currenthandlertype :  Handlertypes)  {      reflectionutils.dowithmethods (CurrentHandlerType,  new reflectionutils.methodcallbacK ()  {         public void dowith (Method  method)  {            RequestMapping  Mapping = annotationutils.findannotation (Method, requestmapping.class);             if  (mapping != null)  {                string[] mappedpatterns =  mapping.value ();                if  (mappedpatterns.length > 0)  {                   for  (string mappedpattern :  Mappedpatterns)  {                      if  (!hastypelevelmapping && !mappedpattern.startswith ("/"))  {                          mappedPattern =  "/"  + mappedPattern;                      }                       addurlsforpath (Urls, mappedpattern);                   }                }                else if  (hastypelevelmapping)  {                   // empty method-level requestmapping                   urls.add (NULL);                }             }         }       }, reflectionutils.user_declared_methods);   }    return stringutils.tostringarray (URLs);}

2.HandlerMapping Interceptor

2.1 Interceptor Location

As can be seen from the structure diagram below, interceptors are distributed in 2 locations and are divided into 2 categories.

Mappedinterceptor is bound to the URL to intercept the eligible URLs;

The interceptor is global in scope and intercepts all requests.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/8B/07/wKiom1hCMDzyA3KAAAA1iMyumJU462.png "title=" 1.png " alt= "Wkiom1hcmdzya3kaaaa1imyumju462.png"/>


Class structure of 2.2 interceptor

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/8B/03/wKioL1hCMruCfElNAACC0tMAcZI252.png "title=" 2.png " alt= "Wkiol1hcmrucfelnaacc0tmaczi252.png"/>


userroleauthorizationinterceptor Check the current user's authorization
pathexposinghandlerinterceptor exposed Bestmatchingpattern
conversionserviceexposinginterceptor
exposed conversionservice
themechang Einterceptor support theme Toggle
Localechangeinterceptor support switching languages
Uritemplatevariableshandlerinterceptor exposing request variable
 webcontentinterceptor check, prepare request and response

It is the webrequestinterceptor that deserves attention. A chapter of research needs to be dedicated.

2.3 Default Intercepter Configuration

<mvc:interceptors> <bean class= "Org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> < Bean class= "Org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor"/> <mvc:interceptor > <mvc:mapping path= "/account"/> <bean class= "org.springframework.web.servlet.theme.ThemeChangeIn Terceptor "></bean> </mvc:interceptor></mvc:interceptors>

This is the most common statement.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/8B/04/wKioL1hCT1-ByAozAAByb2_2KaM218.png "title=" 3222. PNG "alt=" Wkiol1hct1-byaozaabyb2_2kam218.png "/>

I've got a misunderstanding.:<mvc:interceptors> 's sub-tags <bean> declared interceptors are set in Interceptor, which is not the result.

Conversionserviceexposinginterceptor is registered by default when parsing tags. <SPRINGMVC source Analysis (1) Label parsing > mentioned in the article.

2.4 It's okay. Configuration of the type

Understanding the tag parsing process of Spring MVC makes it easy to configure a processor class with a high degree of customization. The exact point is very cumbersome

As follows

Although this section of code can run, but the type conversion interceptor is missing, it needs to be configured.

<bean  class= "Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >    <property name= "Interceptors" >      <array>          <bean class= " Org.springframework.web.servlet.i18n.LocaleChangeInterceptor " />       </array>   </property>   <property name= "MappedInterceptors" >      <list>         < bean class= "Org.springframework.web.servlet.handler.MappedInterceptor" >             <constructor-arg index= "0" >                <list>                   <value>/account</value>                </list>             </constructor-arg>             <constructor-arg index= "1" >                <bean class= "Org.springframework.web.servlet.theme.ThemeChangeInterceptor" ></ bean>            </constructor-arg>          </bean>      </list>    </property></bean>

So declare, must put <mvc:annotation-driven/> commented out, otherwise the system will exist two defaultannotationhandlermapping.


Personal feeling this paragraph and the above <mvc:interceptors> effect is the same.

Difference

The public interceptor, which is only Localechangeinterceptor, is set on the Interceptors property,

Rather than mappedinterceptors.

Source parsing, must not stay on the setting surface, to gain insight into the underlying details.

Of course, is the default configuration, or the original ecological configuration is good, one is the introduction of transparency, a high degree of customization, picking.



This article is from a "simple" blog, so be sure to keep this source http://dba10g.blog.51cto.com/764602/1879103

SPRINGMVC Source Analysis (6) Anatomy defaultannotationhandlermapping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.