要了解Spring MVC是怎么把@RequestMapping注解的方法以及类关联到相应的url上,首先需要看下AbstractHandlerMethodMapping:
public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMapping implements InitializingBean {
AbstractHandlerMethodMapping是一个InitializingBean,因此来看afterPropertiesSet()方法:
@Override
public void afterPropertiesSet() {
initHandlerMethods();
}
protected void initHandlerMethods() {
// …略
// 读取Spring容器中的所有beanNames
String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
getApplicationContext().getBeanNamesForType(Object.class));
for (String beanName : beanNames) {
if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
Class<?> beanType = null;
try {
beanType = getApplicationContext().getType(beanName);
}
catch (Throwable ex) {
// …略
}
// 判断这个bean是不是处理类
// 根据这个类是否使用了@Controller或@RequestMapping
if (beanType != null && isHandler(beanType)) {
detectHandlerMethods(beanName);
}
}
}
handlerMethodsInitialized(getHandlerMethods());
}
关注点就需要放在detectHandlerMethods这个方法上:
protected void detectHandlerMethods(final Object handler) {
Class<?> handlerType = (handler instanceof String ?
getApplicationContext().getType((String) handler) : handler.getClass());
final Class<?> userType = ClassUtils.getUserClass(handlerType);
// 从处理类(@Controller)中获取处理方法(@RequestMapping)
Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
new MethodIntrospector.MetadataLookup<T>() {
@Override
public T inspect(Method method) {
try {
// 返回RequestMappingInfo,包含@RequestMapping的标签信息
return getMappingForMethod(method, userType);
}
catch (Throwable ex) {
throw new IllegalStateException("Invalid mapping on handler class [" +
userType.getName() + "]: " + method, ex);
}
}
});
// …略
// 注册方法
for (Map.Entry<Method, T> entry : methods.entrySet()) {
Method invocableMethod = AopUtils.selectInvocableMethod(entry.getKey(), userType);
T mapping = entry.getValue();
registerHandlerMethod(handler, invocableMethod, mapping);
}
}
先调用getMappingForMethod获取RequestMappingInfo,然后调用registerHandlerMethod注册到MappingRegistry中,在MappingRegistry中关联了url与HandlerMethod、url与RequestMappingInfo等。
到这里,知道了AbstractHandlerMethodMapping怎么处理了@RequestMapping,那么AbstractHandlerMethodMapping是什么时候加入到了Spring容器中的呢?答案是实例化RequestMappingHandlerMapping。
Spring Boot的自动注解中,由ConfigurationClassPostProcessor把在配置项中的WebMvcAutoConfiguration进行处理,会把WebMvcAutoConfiguration$EnableWebMvcConfiguration注册到beanFactory中, 在EnableWebMvcConfiguration中:
@Bean
@Primary
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
// Must be @Primary for MvcUriComponentsBuilder to work
return super.requestMappingHandlerMapping();
}
定义一个requestMappingHandlerMapping的bean:
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping mapping = createRequestMappingHandlerMapping();
// …略
return mapping;
}
在RequestMappingHandlerMapping定义如下:
public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMapping
implements MatchableHandlerMapping, EmbeddedValueResolverAware {
public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMethodMapping<RequestMappingInfo> {
在RequestMappingHandlerMapping继承了AbstractHandlerMethodMapping,就把AbstractHandlerMethodMapping注册到了Spring容器中。
SpringMVC路由映射解析
680

被折叠的 条评论
为什么被折叠?



