/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.handler;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextException;
import org.springframework.util.ObjectUtils;
/**
* {@link org.springframework.web.servlet.HandlerMapping}接口的抽象实现,
* 通过对应用程序上下文中所有已定义的bean进行内省来检测处理程序bean的URL映射。
*
* @author Juergen Hoeller
* @since 2.5
* @see #determineUrlsForHandler
*/
public abstract class AbstractDetectingUrlHandlerMapping extends AbstractUrlHandlerMapping {
private boolean detectHandlersInAncestorContexts = false;
/**
* 设置是否在祖先ApplicationContexts中检测处理程序bean。
* 默认为“ false”:仅在当前ApplicationContext 中将检测到处理程序bean,
* 即仅在定义了此HandlerMapping本身的上下文中(通常在当前DispatcherServlet的上下文中)。
* 打开此标志,以在祖先上下文中检测处理程序Bean(通常是Spring根WebApplicationContext)。
*/
public void setDetectHandlersInAncestorContexts(boolean detectHandlersInAncestorContexts) {
this.detectHandlersInAncestorContexts = detectHandlersInAncestorContexts;
}
/**
* 除了*超类的初始化之外,还调用{@link #detectHandlers()}方法。
*/
@Override
public void initApplicationContext() throws ApplicationContextException {
super.initApplicationContext();
detectHandlers();
}
/**
* 注册在当前ApplicationContext中找到的所有处理程序。
* 处理程序的实际URL确定取决于具体的*
* {@link #determineUrlsForHandler(String)}实现。
* 不能确定此类URL的*的bean根本不被视为处理程序。
* @throws org.springframework.beans.BeansException if the handler couldn't be registered
* @see #determineUrlsForHandler(String)
*/
protected void detectHandlers() throws BeansException {
ApplicationContext applicationContext = obtainApplicationContext();
String[] beanNames = (this.detectHandlersInAncestorContexts ?
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class) :
applicationContext.getBeanNamesForType(Object.class));
// 取任何我们可以确定其URL的bean名称。
for (String beanName : beanNames) {
String[] urls = determineUrlsForHandler(beanName);
if (!ObjectUtils.isEmpty(urls)) {
// URL paths found: Let's consider it a handler.
registerHandler(urls, beanName);
}
}
if ((logger.isDebugEnabled() && !getHandlerMap().isEmpty()) || logger.isTraceEnabled()) {
logger.debug("Detected " + getHandlerMap().size() + " mappings in " + formatMappingName());
}
}
/**
* 确定给定处理程序bean的URL。
* @param beanName the name of the candidate bean
* @return the URLs determined for the bean, or an empty array if none
*/
protected abstract String[] determineUrlsForHandler(String beanName);
}
springmvc组件HandleMapping源码-AbstractDetectingUrlHandlerMapping
最新推荐文章于 2021-08-31 21:59:16 发布