spring问题,关于BeanFactoryUtils这个类,还有就是getbean和getbeans的区别

2024-11-28 19:34:15
推荐回答(1个)
回答(1):

spring 容器会在早厅衡启动的时候为DispatcherServlet初始化handlerMapping.
// Find all HandlerMappings in the ApplicationContext,
// including ancestor contexts.
Map matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
context, HandlerMapping.class, true, false);
即找到在context里声明的,所有HandlerMapping和其子类的bean. 可以有多个,陆做伏蔽这些handlerMapping里有顺序声明,可以根据它来排序。

public static Map beansOfTypeIncludingAncestors(ListableBeanFactory lbf, Class type)
throws BeansException {

Assert.notNull(lbf, "ListableBeanFactory must not be null");
Map result = new LinkedHashMap(4);
result.putAll(lbf.getBeansOfType(type));
if (lbf instanceof HierarchicalBeanFactory) {
HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf;
if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) {
Map parentResult = beansOfTypeIncludingAncestors(
(ListableBeanFactory) hbf.getParentBeanFactory(), type);
for (Iterator it = parentResult.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String beanName = (String) entry.getKey();
if (!result.containsKey(beanName) && !hbf.containsLocalBean(beanName)) {
result.put(beanName, entry.getValue());
}
}
}
}
return result;
}

参考当中的源码