Springmvc source Handlermapping and Handleradapter (2)

Source: Internet
Author: User
Tags aliases

In the previous section we saw that when an entity bean is not declared handlermapping in ***-serlvet.xml, the default construct

Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping and
Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

The instance.

So this section starts with these two classes, and the most important method in Handlermapping is to gethandler the object used to get handlerexcutionchain.

First look at the Beannameurlhandlermapping class, its interior does not implement the GetHandler method, and it inherits from Abstractdetectingurlhandlermapping, So from the bottom up we go to see the implementation of the Abstractdetectingurlhandlermapping class.

In this class, I see a way to Java code   /**       * register all handlers  found in the current ApplicationContext.       *  <p>the actual url determination for a handler is up to  the concrete       * {@link   #determineUrlsForHandler (String) } implementation. a bean for       * which no  such urls could be determined is simply not considered a  handler.       *  @throws  org.springframework.beans.BeansException  If the handler couldn ' t be registered       *  @see   #determineUrlsForHandler (String)        */        protecteD void detecthandlers ()  throws BeansException {            if  (logger.isdebugenabled ())  {                logger.debug ("looking for url mappings in  application context:  " + getapplicationcontext ());            }           string[] beannames  =  (this.detecthandlersinancestorcontexts ?                     Beanfactoryutils.beannamesfortypeincludingancestors (Getapplicationcontext (),  object.class)  :                     Getapplicationcontext (). Getbeannamesfortype (Object.class));   &NBSP;&NBSp         // take any bean name that we  can determine URLs for.           for   (string beanname : beannames)  {                string[] urls = determineurlsforhandler (BeanName);                if  (! Objectutils.isempty (URLs))  {                    // url paths found: let ' S consider it a  handler.                    registerhandler (urls, beanname);                }               else {                    if  (logger.isdebugenabled () )  {                        logger.debug ("rejected bean name  '"  + beanName +   "':  no url paths identified");                    }                }           }        }  

 whether to detect handler beans in ancestor applicationcontexts. Java Code    private boolean detecthandlersinancestorcontexts = false;  

  This method first queries all the beans in the context, Because the type of the query is Object.class, get all the bean names, then parse the bean name and see if the Urlname,determineurlsforhandler method is specifically implemented in the Beannameurlhandlermapping class: Java Code   /**       * checks name and aliases of  the given bean for URLs, starting with  "/" .        */        @Override        protected  String[] determineurlsforhandler (string beanname)  {            List<String> urls = new ArrayList<String> ();           if  (Beanname.startswith ("/"))  {                urls.add (beanname);            }           string[] aliases = getapplicationcontext (). getaliases (beanName);            for  (int i = 0; i <  aliases.length; i++)  {                if  (Aliases[i].startswith ("/"))  {                    urls.add (Aliases[i]);                }           }            return stringutils.tostringarray (URLs);        }  

  can see that it finds all the beans that beanname with "/" and returns their name as a URL.

The next step is to register the handler for the bean that handles the URL request. The Registerhandler method is in the parent class of Abstractdetectingurlhandlermapping abstracturlhandlermapping class: Java code   /**       * Register the specified handler for the  given url paths.       *  @param  urlpaths the urls  that the bean should be mapped to       *   @param  beanName the name of the handler bean        *  @throws  beansexception if the handler couldn ' t be  registered       *  @throws  illegalstateexception if there  is a conflicting handler registered       */        protected void registerhandler (string[] urlpaths, stRing beanname)  throws BeansException, IllegalStateException {            assert.notnull (urlpaths,  "Url path array must not  be null ");           for  (String urlpath  : urlpaths)  {                registerhandler (urlpath, beanname);           }   

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.