类结构
此为抽象方法,并实现了initializingBean
接口,其实主要的注册操作则是通过afterPropertiesSet()接口方法来调用的。
@Override
public void afterPropertiesSet() {
initHandlerMethods();
}
/**
* Scan beans in the ApplicationContext, detect and register handler methods.
* @see #getCandidateBeanNames()
* @see #processCandidateBean
* @see #handlerMethodsInitialized
*/
protected void initHandlerMethods() {
//获取springmvc上下文的所有注册的bean
for (String beanName : getCandidateBeanNames()) {
if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
//根据名字获取类,判断该类是不是要转换的类型。
processCandidateBean(beanName);
}
}
handlerMethodsInitialized(getHandlerMethods());
}
protected void processCandidateBean(String beanName) {
Class<?> beanType = null;
try {
beanType = obtainApplicationContext().getType(beanName);
}
catch (Throwable ex) {
// An unresolvable bean type, probably from a lazy bean - let's ignore it.
if (logger.isTraceEnabled()) {
logger.trace("Could not resolve type for bean '" + beanName + "'", ex);
}
}
//isHandler()是抽象方法,主要供子类需要扫描什么类型的bean
if (beanType != null && isHandler(beanType)) {
//调用detectHandlerMethods开始做类型和路径映射转换,解析其中的HandlerMethod进行注册
detectHandlerMethods(beanName);
}
}
@Override
protected boolean isHandler(Class<?> beanType) {
return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
detectHandlerMethods()解析
protected void detectHandlerMethods(Object handler) {
Class<?> handlerType = (handler instanceof String ?
obtainApplicationContext().getType((String) handler) : handler.getClass());
if (handlerType != null) {
//因为有些是CGLIB代理生成的,获取真实的类
Class<?> userType = ClassUtils.getUserClass(handlerType);
Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
(MethodIntrospector.MetadataLookup<T>) method -> {
try {
//模板方法获取handlerMethod的mapping属性,可扩展
return getMappingForMethod(method, userType);
}
catch (Throwable ex) {
throw new IllegalStateException("Invalid mapping on handler class [" +
userType.getName() + "]: " + method, ex);
}
});
if (logger.isTraceEnabled()) {
logger.trace(formatMappings(userType, methods));
}
//对查找到的HandlerMethod进行注册,保存至内部类mappingRegistry对象中
methods.forEach((method, mapping) -> {
/作下判断,method是否从属于userType
Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
registerHandlerMethod(handler, invocableMethod, mapping);
});
}
}
看一下子类RequestMappingHandlerMapping
@Override
public void afterPropertiesSet() {
//config为RequestMappingInfo的创建对象
this.config = new RequestMappingInfo.BuilderConfiguration();
//设置urlPathHelper默认为UrlPathHelper.class
this.config.setUrlPathHelper(getUrlPathHelper());
//默认为AntPathMatcher,路径匹配校验器
this.config.setPathMatcher(getPathMatcher());
//是否支持后缀补充,默认为true
this.config.setSuffixPatternMatch(this.useSuffixPatternMatch);
//是否添加"/"后缀,默认为true
this.config.setTrailingSlashMatch(this.useTrailingSlashMatch);
//是否采用mediaType匹配模式,比如.json/.xml模式的匹配,默认为false
this.config.setRegisteredSuffixPatternMatch(this.useRegisteredSuffixPatternMatch);
//mediaType处理类
this.config.setContentNegotiationManager(getContentNegotiationManager());
//调用父类进行HandlerMethod的注册工作
super.afterPropertiesSet();
}