三:RequestMapping(一)

1.      在分析autowired注解的时候讲到populateBean中装配bean,之后就调用initializeBean (来实现对url 到 mapping 的映射 以及到handlerMethod)

2.      在invokeInitMethods方法中调用((InitializingBean)bean).afterPropertiesSet();

beanName = org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0

 由于开启了

<mvc:annotation-driven/>的文章,我们说过了,当在配置文件中加上该标记后,Spring(3.1后)会默认为我们注册RequestMappingHandlerMapping等Bean定义。而RequestMappingHandlerMapping 实现了InitializingBean接口,因此,在初始化并装配该Bean实例时,执行到上述代码是,便会执行他的afterPropertySet方法

 

来自 <https://my.oschina.net/HeliosFly/blog/212329>

3.  for(StringbeanName:beanNames){

if(!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)&&

isHandler(getApplicationContext().getType(beanName))){

detectHandlerMethods(beanName);

}

}

Scan beans in the ApplicationContext, detect and registerhandler methods.

 

1)遍历Handler中的所有方法,找出其中被@RequestMapping注解标记的方法。2)然后遍历这些方法,生成RequestMappingInfo实例。3)将RequestMappingInfo实例以及处理器方法注册到缓存中。

4.      获取方法method上的@RequestMapping实例,创建MappingInfo。之后将将类层次的RequestMapping和方法级别的RequestMapping结合,返回它。利用combine将各种信息合并起来,如url

Returns a new instance with URL patterns from the current instance("this") and the "other" instance as follows:

If there are patterns in both instances, combine the patterns in"this" with the patterns in "other" usingPathMatcher.combine(String, String).

If only one instance has patterns, use them.

If neither instance has patterns, use an empty String (i.e. "").

 

 

 

PatternRequestCondition 它其实就是URL模式的封装,它包含了一个URL模式的Set集合。其实就是@RequestMapping注解中的value值得封装。

 

来自 <https://my.oschina.net/HeliosFly/blog/212329>

 

 

5. 当请求到达时,去urlMap中需找匹配的url,以及获取对应mapping实例,然后去handlerMethods中获取匹配HandlerMethod实例。

 

 

this.handlerMethods.put(mapping,newHandlerMethod);

if(logger.isInfoEnabled()){

logger.info("Mapped\""+mapping+"\"onto"+newHandlerMethod);

}

Set<String>patterns=getMappingPathPatterns(mapping);

for(String  pattern:patterns){

if(!getPathMatcher().isPattern(pattern)){

this.urlMap.add(pattern,mapping);

}

}

 

三:RequestMapping(一)