在上一博客中我们介绍了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中。
需要看一下determineUrlsForHandler
我们介绍了AbstractDetectingUrlHandlerMapping,其定义了一个抽象方法determineUrlsForHandler在子类AbstractControllerUrlHandlerMapping中实现。
实现原理就是根据beanName从容器中获取bean,
然后调用buildUrlsForHandler完成beanName和beanClass的对应关系,其具体实现还是在其子类中实现。
protected String[] determineUrlsForHandler(String beanName) {
Class<?> beanClass = getApplicationContext().getType(beanName);
//判断是不是支持的类型
if (isEligibleForMapping(beanName, beanClass)) {
//模板方法,在子类实现
return buildUrlsForHandler(beanName, beanClass);
}
else {
return null;
}
}
抽象方法子类实现
protected abstract String[] buildUrlsForHandler(String beanName, Class<?> beanClass);
除此之外AbstractControllerUrlHandlerMapping还提供了一些配置,用于排除掉一些包或者一些类,可以在配置中进行配置
public void setIncludeAnnotatedControllers(boolean includeAnnotatedControllers) {
this.predicate = (includeAnnotatedControllers ?
new AnnotationControllerTypePredicate() : new ControllerTypePredicate());
}
public void setExcludedPackages(String... excludedPackages) {
this.excludedPackages = (excludedPackages != null) ?
new HashSet<String>(Arrays.asList(excludedPackages)) : new HashSet<String>();
}
public void setExcludedClasses(Class<?>... excludedClasses) {
this.excludedClasses = (excludedClasses != null) ?
new HashSet<Class<?>>(Arrays.asList(excludedClasses)) : new HashSet<Class<?>>();
}
判断beanName和beanClass是否已经配置排除对应关系。
protected boolean isEligibleForMapping(String beanName, Class<?> beanClass) {
if (beanClass == null) {
if (logger.isDebugEnabled()) {
logger.debug("Excluding controller bean '" + beanName + "' from class name mapping " +
"because its bean type could not be determined");
}
return false;
}
if (this.excludedClasses.contains(beanClass)) {
if (logger.isDebugEnabled()) {
logger.debug("Excluding controller bean '" + beanName + "' from class name mapping " +
"because its bean class is explicitly excluded: " + beanClass.getName());
}
return false;
}
String beanClassName = beanClass.getName();
for (String packageName : this.excludedPackages) {
if (beanClassName.startsWith(packageName)) {
if (logger.isDebugEnabled()) {
logger.debug("Excluding controller bean '" + beanName + "' from class name mapping " +
"because its bean class is defined in an excluded package: " + beanClass.getName());
}
return false;
}
}
//
return isControllerType(beanClass);
}
其他方法
//判断是否实现了Controller接口或者使用了@Controller
protected boolean isControllerType(Class<?> beanClass) {
return this.predicate.isControllerType(beanClass);
}
protected boolean isMultiActionControllerType(Class<?> beanClass) {
return this.predicate.isMultiActionControllerType(beanClass);
}
protected abstract String[] buildUrlsForHandler(String beanName, Class<?> beanClass);
:AbstractControllerUrlHandlerMapping的实现机制就是根据beanName从容器中获取实现类beanClass,同时beanName和beanClass的对应关系的操作是在其子类中完成实现的,同时AbstractControllerUrlHandlerMapping提供了一些配置用于排除一些类的关系。