2、源码
2.1、入口
DefaultBeanDefinitionDocumentReader.parseBeanDefinitions
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
//如果是缺省命名空间,进行默认解析注册,否则进行自定义元素解析方法
if (delegate.isDefaultNamespace(root)) {
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
//循环遍历节点,如果是默认命名空间,则采取默认元素解析方法,否则采用自定义元素解析方法
if (delegate.isDefaultNamespace(ele)) {
parseDefaultElement(ele, delegate);
}
else {
delegate.parseCustomElement(ele);
}
}
}
}
else {
delegate.parseCustomElement(root);
}
}
2.2、解析过程
BeanDefinitionParserDelegate.parseCustomElement
public BeanDefinition parseCustomElement(Element ele, @Nullable BeanDefinition containingBd) {
//获取当前元素的命名空间
String namespaceUri = getNamespaceURI(ele);
if (namespaceUri == null) {
return null;
}
//根据命名空间获取对应Handler
NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
if (handler == null) {
error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
return null;
}
//通过返回的处理器解析当前元素
return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
}
2.2.1、获取节点的命名空间
String namespaceUri = getNamespaceURI(ele);
2.2.2、根据命名空间获取对应的NamespaceHandler
NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
this.readerContext.getNamespaceHandlerResolver();
此方法返回结果是NamespaceHandlerResolver,而NamespaceHandlerResolver的实现类是DefaultNamespaceHandlerResolver,进入到DefaultNamespaceHandlerResolver.resolve:
public NamespaceHandler resolve(String namespaceUri) {
//获取handlerMapping,即spring.handlers中存储的信息,以 namespaceUri -> NamespaceHandler名字 的形式存储
//如果已经实例化,则该namespaceUri对应的value值是NamespaceHandler对象,而不是String类型
Map<String, Object> handlerMappings = getHandlerMappings();
//获取到对应的NamespaceHandler名字或者实例
Object handlerOrClassName = handlerMappings.get(namespaceUri);
if (handlerOrClassName == null) {
return null;
}
//如果当前类已经实例化,则直接返回
else if (handlerOrClassName instanceof NamespaceHandler) {
return (NamespaceHandler) handlerOrClassName;
}
//还未实例化,则上面获取的handlerOrClassName只是名字,还需实例化
else {
//获取到className
String className = (String) handlerOrClassName;
try {
//通过className以及当前类加载器加载class类
Class<?> handlerClass = ClassUtils.forName(className, this.classLoader);
if (!NamespaceHandler.class.isAssignableFrom(handlerClass)) {
throw new FatalBeanException("Class [" + className + "] for namespace [" + namespaceUri +
"] does not implement the [" + NamespaceHandler.class.getName() + "] interface");
}
//实例化对应的Handler
NamespaceHandler namespaceHandler = (NamespaceHandler) BeanUtils.instantiateClass(handlerClass);
namespaceHandler.init();
//将实例化好的Handler以namespaceUri->Handler的形式存储在handlerMappings中
handlerMappings.put(namespaceUri, namespaceHandler);
return namespaceHandler;
}
catch (ClassNotFoundException ex) {
throw new FatalBeanException("Could not find NamespaceHandler class [" + className +
"] for namespace [" + namespaceUri + "]", ex);
}
catch (LinkageError err) {
throw new FatalBeanException("Unresolvable class definition for NamespaceHandler class [" +
className + "] for namespace [" + namespaceUri + "]", err);
}
}
}
以上代码一共做了这么几件事:
1、读取所有META-INF/spring.handlers的文件,并且以namespaceUri -> NamespaceHandler名字 的形式存储
2、根据传入的namespaceUri获取对应的值,如果该值类型是NamespaceHandler,则直接返回;如果是字符串类型,则还未实例化,进行实例化,然后将实例化NamespaceHandler替代原来的NamespaceHandler名字
3、返回对应的NamespaceHandler
2.2.3、解析
return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
这句代码的做了这么2件事:
1、查找到对应的BeandefinitionParser
2、解析
2.2.3.1、定位BeanDefinitionParser
首先我们可以得知当前NamespaceHandler为UserNamespaceHandler,那么要如何获取到对应的UserBeanDefinitionParser,首先我们知道UserNamespaceHandler有如下方法:
public class UserNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
//以节点本地名称->BeanDefinitioinParser的形式存储在Map<String, BeanDefinitionParser>中
registerBeanDefinitionParser("user",new UserBeanDefinitionParse());
}
}
由于UserNamespaceHandler继承了NamespaceHandlerSupport,进入到NamespaceHandlerSupport:
public abstract class NamespaceHandlerSupport implements NamespaceHandler {
/**
* Stores the {@link BeanDefinitionParser} implementations keyed by the
* local name of the {@link Element Elements} they handle.
* 以节点本地名称->BeanDefinitionParser的形式存储于Map
*/
private final Map<String, BeanDefinitionParser> parsers =
new HashMap<String, BeanDefinitionParser>();
/**
* 将对应的BeanDefinitionParser存放到map中
*/
protected final void registerBeanDefinitionParser(String elementName, BeanDefinitionParser parser) {
this.parsers.put(elementName, parser);
}
//根据Element的localName定位到对应的BeanDefinitionParser
private BeanDefinitionParser findParserForElement(Element element, ParserContext parserContext) {
//获取节点的本地名称
String localName = parserContext.getDelegate().getLocalName(element);
//根据节点本地名称获取到对应BeanDefinitionParser
BeanDefinitionParser parser = this.parsers.get(localName);
if (parser == null) {
parserContext.getReaderContext().fatal(
"Cannot locate BeanDefinitionParser for element [" + localName + "]", element);
}
return parser;
}
}
因此,我们可以知道:
1、NamespaceHandler在初始化(init)时将对应的BeanDefinitionParser以elementName->BeanDefinitionParser的形式存储在map中
2、NamespaceHandler可以根据Element的localName,从而定位到对应的BeanDefinitionParser
2.2.3.2、解析
由于当前定位到的解析器为UserBeanDefinitionParse,它的继承体系图如图所示:
2.2.3.2.1、AbstractBeanDefinitionParser.parse
public final BeanDefinition parse(Element element, ParserContext parserContext) {
//模板方法,由子类完成,解析相应的标签,除了id以及name以外
AbstractBeanDefinition definition = parseInternal(element, parserContext);
if (definition != null && !parserContext.isNested()) {
try {
//解析该标签的id属性
String id = resolveId(element, definition, parserContext);
if (!StringUtils.hasText(id)) {
parserContext.getReaderContext().error(
"Id is required for element '" + parserContext.getDelegate().getLocalName(element)
+ "' when used as a top-level tag", element);
}
String[] aliases = null;
if (shouldParseNameAsAliases()) {
String name = element.getAttribute(NAME_ATTRIBUTE);
if (StringUtils.hasLength(name)) {
aliases = StringUtils.trimArrayElements(StringUtils.commaDelimitedListToStringArray(name));
}
}
//将id作为beanName,结合definition构建BeanDefinitionHolder
BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, id, aliases);
//以beanName->BeanDefinition的形式注册BeanDefinition
registerBeanDefinition(holder, parserContext.getRegistry());
if (shouldFireEvents()) {
BeanComponentDefinition componentDefinition = new BeanComponentDefinition(holder);
postProcessComponentDefinition(componentDefinition);
parserContext.registerComponent(componentDefinition);
}
}
catch (BeanDefinitionStoreException ex) {
String msg = ex.getMessage();
parserContext.getReaderContext().error((msg != null ? msg : ex.toString()), element);
return null;
}
}
return definition;
}
这个方法主要做了这么几件事:
1、解析自定义标签相关属性parseInternal(element, parserContext);,此方法交由子类完成
2、获取节点的id属性,并将其作为beanName
3、注册BeanDefinition
由上面的类体系图可以得知,AbstractSingleBeanDefinitionParser实现了parserInternal
2.2.3.2.2、AbstractSingleBeanDefinitionParser.parserInternal
protected final AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
//构建BeanDefinition
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
//设置parnetName属性
String parentName = getParentName(element);
if (parentName != null) {
builder.getRawBeanDefinition().setParentName(parentName);
}
//设置beanClass
Class<?> beanClass = getBeanClass(element);
if (beanClass != null) {
builder.getRawBeanDefinition().setBeanClass(beanClass);
}
else {
String beanClassName = getBeanClassName(element);
if (beanClassName != null) {
builder.getRawBeanDefinition().setBeanClassName(beanClassName);
}
}
builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
BeanDefinition containingBd = parserContext.getContainingBeanDefinition();
if (containingBd != null) {
// Inner bean definition must receive same scope as containing bean.
builder.setScope(containingBd.getScope());
}
if (parserContext.isDefaultLazyInit()) {
// Default-lazy-init applies to custom bean definitions as well.
builder.setLazyInit(true);
}
//模板方法,交由子类实现
doParse(element, parserContext, builder);
return builder.getBeanDefinition();
}
这个方法一共做了这么几件事:
1、构建BeanDefinition
2、设置相关基本属性
3、解析节点,由子类实现doParse
因此调用UserBeanDefinitionParse.doParse
2.2.3.2.3、最后调用具体实现类的doParse方法
protected void doParse(Element element, BeanDefinitionBuilder builder) {
String userName = element.getAttribute("userName");
String password = element.getAttribute("password");
if(StringUtils.hasText(userName)){
builder.addPropertyValue("userName",userName);
}
if(StringUtils.hasText(password)){
builder.addPropertyValue("password",password);
}
}
DefaultBeanDefinitionDocumentReader.parseBeanDefinitions
InterceptorsBeanDefinitionParser http://www.cnblogs.com/fangjian0423/p/springMVC-interceptor.html