springMVC源码分析--AbstractDetectingUrlHandlerMapping(五)

转载自 https://blog.csdn.net/qq924862077/article/details/53760824

在上一博客中我们介绍了handlerMap有一个注册url和Handler关系的注册函数,这个函数的调用是在实现类AbstractDetectingUrlHandlerMapping中实现的,目的是springMVC容器启动时将url和handler的对应关系注册到handlerMap中。
AbstractDetectingUrlHandlerMapping 抽象类:通过重写initApplicationContext来注册Handler,调用detectHandlers方法会根据配置的detectHand-lersInAcestorContexts参数从springMVC容器或者springMVC集群父容器中找到所有bean的beanName,然后调用determineUrlsForHandler方法对每个beanName解析出对应的urls,如果解析的结果不为空,则将解析出的urls和beanName注册到父类的map中。

//初始化容器
	@Override
	public void initApplicationContext() throws ApplicationContextException {
		super.initApplicationContext();
		detectHandlers();
	}

在调用父类的super.initApplicationContext后就是注册每个bean和url的关系,调用detectHandlers

//注册每个bean对应的url的关系
	protected void detectHandlers() throws BeansException {
		if (logger.isDebugEnabled()) {
			logger.debug("Looking for URL mappings in application context: " + getApplicationContext());
		}
		//获取容器的所有bean的名字
		String[] beanNames = (this.detectHandlersInAncestorContexts ?
				BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
				getApplicationContext().getBeanNamesForType(Object.class));
 
		// Take any bean name that we can determine URLs for.
		//对每个beanName解析url,如果能解析到就注册到父类的map中
		for (String beanName : beanNames) {
			//子类具体去实现
			String[] urls = determineUrlsForHandler(beanName);
			if (!ObjectUtils.isEmpty(urls)) {
				// URL paths found: Let's consider it a handler.
				//将解析的url注册到父类
				registerHandler(urls, beanName);
			}
			else {
				if (logger.isDebugEnabled()) {
					logger.debug("Rejected bean name '" + beanName + "': no URL paths identified");
				}
			}
		}
	}

其中determineUrlsForHandler函数是在其子类中实现的,registerHandler函数操作是在父类AbstractUrlHandlerMapping中实现的,将bean和url的关系注册到handlerMap中。
AbstractDetectingUrlHandlerMapping的完整源码如下:
 

public abstract class AbstractDetectingUrlHandlerMapping extends AbstractUrlHandlerMapping {
 
	private boolean detectHandlersInAncestorContexts = false;
 
 
	
	public void setDetectHandlersInAncestorContexts(boolean detectHandlersInAncestorContexts) {
		this.detectHandlersInAncestorContexts = detectHandlersInAncestorContexts;
	}
 
 
	//初始化容器
	@Override
	public void initApplicationContext() throws ApplicationContextException {
		super.initApplicationContext();
		detectHandlers();
	}
 
	//注册每个bean对应的url的关系
	protected void detectHandlers() throws BeansException {
		if (logger.isDebugEnabled()) {
			logger.debug("Looking for URL mappings in application context: " + getApplicationContext());
		}
		//获取容器的所有bean的名字
		String[] beanNames = (this.detectHandlersInAncestorContexts ?
				BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
				getApplicationContext().getBeanNamesForType(Object.class));
 
		// Take any bean name that we can determine URLs for.
		//对每个beanName解析url,如果能解析到就注册到父类的map中
		for (String beanName : beanNames) {
			//子类具体去实现
			String[] urls = determineUrlsForHandler(beanName);
			if (!ObjectUtils.isEmpty(urls)) {
				// URL paths found: Let's consider it a handler.
				//将解析的url注册到父类
				registerHandler(urls, beanName);
			}
			else {
				if (logger.isDebugEnabled()) {
					logger.debug("Rejected bean name '" + beanName + "': no URL paths identified");
				}
			}
		}
	}
	//抽象方法,子类中实现
	protected abstract String[] determineUrlsForHandler(String beanName);
 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值