BeanFactory在创建Bean过程中需要执行populateBean方法,populateBean方法便是@Autowired注解的处理过程,执行的属性的自动注入等操作。
其中InitiaizingBean接口,它的实现Bean会在容器完成属性注入后执行一个自定义操作
<mvc:annotation-driven/>当在配置文件中加上该标记后,Spring(3.1后)会默认为我们注册RequestMappingHandlerMapping等Bean定义。
而RequestMappingHandlerMapping实现了InitializingBean接口,因此,在初始化并装配该Bean实例时,执行到上述代码是,便会执行他的afterPropertySet方法。
afterPropertySet方法直接调用了initHandlerMethods()方法,并且该方法被描述为:扫描ApplicationContext中的beans,检测并注册处理器方法
//扫描所有注册的Bean
//遍历这些Bean,依次判断是否是处理器,并检测其HandlerMethod
整个的检测过程:
1)遍历Handler中的所有方法,找出其中被@RequestMapping注解标记的方法。
[怎样判断是否是处理器的,以及怎么detect Handler Methods 的:
((AnnotationUtils.findAnnotation(beanType, Controller.class) != null) ||
(AnnotationUtils.findAnnotation(beanType, RequestMapping.class) != null));
看看有没有被@Controller或者@RequestMapping注解标记]
2)然后遍历这些方法,生成RequestMappingInfo实例。
-根据方法上的@RequestMapping来创建RequestMappingInfo实例。(先获取方法上的@RequestMapping信息,然后获取类级别上的@RequestMapping 信息,然后将两者结合)getMappingForMethod
--获取方法method上的@RequestMapping实例。
--检查方法所属的类有没有@RequestMapping注解
--将类层次的RequestMapping和方法级别的RequestMapping结合 createRequestMappingInfo
[怎样创建RequestMappingInfo对象的(包括他的内部结构),以及怎样将类级别的request mapping信息和方法级别的进行结合的?
其中涉及到了几个类,我们大致了解下含义:
PatternRequestCondition 它其实就是URL模式的封装,它包含了一个URL模式的Set集合。其实就是@RequestMapping注解中的value值得封装。
RequestMethodRequestCondition 它是@RequestMapping 注解中method属性的封装
ParamsRequestCondition 它是@RequestMapping注解中params属性的封装
等等,依次类推。因此RequestMappingInfo其实就是对@RquestMapping 的封装。
怎样进行Combine的:对每一个元素都进行combine操作,我们这里只看PatternRequestCondition是怎么结合的
(1)两个pattern都存在是,调用PathMatcher的combine方法合并两个pattern。
(2)只有一个有时,使用这个。
(3)两个都没有时,为空“”。
现在真正的url拼接是由PathMatcher来完成的了。]
-注册请求映射
--registerHandlerMethod 方法:当请求到达时,去urlMap中需找匹配的url,以及获取对应mapping实例,然后去handlerMethods中获取匹配HandlerMethod实例。
其中InitiaizingBean接口,它的实现Bean会在容器完成属性注入后执行一个自定义操作
<mvc:annotation-driven/>当在配置文件中加上该标记后,Spring(3.1后)会默认为我们注册RequestMappingHandlerMapping等Bean定义。
而RequestMappingHandlerMapping实现了InitializingBean接口,因此,在初始化并装配该Bean实例时,执行到上述代码是,便会执行他的afterPropertySet方法。
afterPropertySet方法直接调用了initHandlerMethods()方法,并且该方法被描述为:扫描ApplicationContext中的beans,检测并注册处理器方法
//扫描所有注册的Bean
//遍历这些Bean,依次判断是否是处理器,并检测其HandlerMethod
整个的检测过程:
1)遍历Handler中的所有方法,找出其中被@RequestMapping注解标记的方法。
[怎样判断是否是处理器的,以及怎么detect Handler Methods 的:
((AnnotationUtils.findAnnotation(beanType, Controller.class) != null) ||
(AnnotationUtils.findAnnotation(beanType, RequestMapping.class) != null));
看看有没有被@Controller或者@RequestMapping注解标记]
2)然后遍历这些方法,生成RequestMappingInfo实例。
-根据方法上的@RequestMapping来创建RequestMappingInfo实例。(先获取方法上的@RequestMapping信息,然后获取类级别上的@RequestMapping 信息,然后将两者结合)getMappingForMethod
--获取方法method上的@RequestMapping实例。
--检查方法所属的类有没有@RequestMapping注解
--将类层次的RequestMapping和方法级别的RequestMapping结合 createRequestMappingInfo
[怎样创建RequestMappingInfo对象的(包括他的内部结构),以及怎样将类级别的request mapping信息和方法级别的进行结合的?
其中涉及到了几个类,我们大致了解下含义:
PatternRequestCondition 它其实就是URL模式的封装,它包含了一个URL模式的Set集合。其实就是@RequestMapping注解中的value值得封装。
RequestMethodRequestCondition 它是@RequestMapping 注解中method属性的封装
ParamsRequestCondition 它是@RequestMapping注解中params属性的封装
等等,依次类推。因此RequestMappingInfo其实就是对@RquestMapping 的封装。
怎样进行Combine的:对每一个元素都进行combine操作,我们这里只看PatternRequestCondition是怎么结合的
(1)两个pattern都存在是,调用PathMatcher的combine方法合并两个pattern。
(2)只有一个有时,使用这个。
(3)两个都没有时,为空“”。
现在真正的url拼接是由PathMatcher来完成的了。]
-注册请求映射
--registerHandlerMethod 方法:当请求到达时,去urlMap中需找匹配的url,以及获取对应mapping实例,然后去handlerMethods中获取匹配HandlerMethod实例。
3)将RequestMappingInfo实例以及处理器方法注册到缓存中。
总结:
扫描所有注册的Bean
遍历这些Bean,依次判断是否是处理器,并检测其HandlerMethod
遍历Handler中的所有方法,找出其中被@RequestMapping注解标记的方法。
获取方法method上的@RequestMapping实例。
检查方法所属的类有没有@RequestMapping注解
将类层次的RequestMapping和方法级别的RequestMapping结合 (createRequestMappingInfo)
当请求到达时,去urlMap中需找匹配的url,以及获取对应mapping实例,然后去handlerMethods中获取匹配HandlerMethod实例。
将RequestMappingInfo实例以及处理器方法注册到缓存中。