之前在阅读refreshBeanFactory的实现时,还没有对loadBeanDefinitions(beanFactory)进行分析,现在我们继续分析这一块内容。
@Override
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
loadBeanDefinitions(beanFactory);
synchronized (this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
refreshBeanFactory是AbstractApplicationContext中的抽象方法,在子类AbstractRefreshableApplicationContext中实现,该方法是获取beanFactory的方法,同时也是解析bean配置的方法。其中,beanFactory初始化由createBeanFactory()负责,该方法返回一个仅仅简单初始化的beanFactory对象。针对beanFactory对象真正进行信息填充的方法是loadBeanDefinitions(beanFactory)方法,很明显,该方法具有副作用。loadBeanDefinitions(beanFactory)方法是在AbstractRefreshableApplicationContext的子类AbstractXmlApplicationContext中实现的。具体实现如下:
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's
// resource loading environment.
beanDefinitionReader.setEnvironment(this.getEnvironment());
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
// Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
initBeanDefinitionReader(beanDefinitionReader);
loadBeanDefinitions(beanDefinitionReader);
}
loadBeanDefinitions(beanFactory)中定义了XmlBeanDefinitionReader对象,在XmlBeanDefinitionReader对象就绪后,调用loadBeanDefinitions(beanDefinitionReader)。下面是其实现:
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
Resource[] configResources = getConfigResources();
if (configResources != null) {
reader.loadBeanDefinitions(configResources);
}
String[] configLocations = getConfigLocations();
if (configLocations != null) {
reader.loadBeanDefinitions(configLocations);
}
}
loadBeanDefinitions(beanDefinitionReader)实际又调用了reader. loadBeanDefinitions进行解析,传入的参数是相关的资源文件,该资源文件就是ApplicationContext的xml配置文件。最终,经过一连串的函数间的委托调用,看到了真正负责加载工作的方法:XmlBeanDefinitionReader的方法——doLoadBeanDefinitions(InputSource inputSource, Resource resource)。inputSource包装了实际的xml配置文件输入流,resource是xml配置的资源描述类。
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
throws BeanDefinitionStoreException {
try {
Document doc = doLoadDocument(inputSource, resource);
return registerBeanDefinitions(doc, resource);
}
catch (BeanDefinitionStoreException ex) {
throw ex;
}
catch (SAXParseException ex) {
throw new XmlBeanDefinitionStoreException(resource.getDescription(),
"Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);
}
catch (SAXException ex) {
throw new XmlBeanDefinitionStoreException(resource.getDescription(),
"XML document from " + resource + " is invalid", ex);
}
catch (ParserConfigurationException ex) {
throw new BeanDefinitionStoreException(resource.getDescription(),
"Parser configuration exception parsing XML from " + resource, ex);
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(resource.getDescription(),
"IOException parsing XML document from " + resource, ex);
}
catch (Throwable ex) {
throw new BeanDefinitionStoreException(resource.getDescription(),
"Unexpected exception parsing XML document from " + resource, ex);
}
}
doLoadDocument实现如下。背后实际调用documentLoader加载xml配置文件,documentLoader类型为DefaultDocumentLoader。documentLoader.loadDocument的参数中,inputSource是直接传入的,getEntityResolver()对象是前面loadBeanDefinitions(beanFactory)方法中直接set进去的,errorHandler是随类初始化就已经初始化的对象,类型为SimpleSaxErrorHandler。该方法会返回一个标准的代表xml解析结果的Document对象。
protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception {
return this.documentLoader.loadDocument(inputSource, getEntityResolver(), this.errorHandler,
getValidationModeForResource(resource), isNamespaceAware());
}
接下来,会执行registerBeanDefinitions(doc, resource)对doc对象进行解析。
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
documentReader.setEnvironment(getEnvironment());
int countBefore = getRegistry().getBeanDefinitionCount();
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
return getRegistry().getBeanDefinitionCount() - countBefore;
}
而其中实际负责工作的下面这句代码,由docmentReader对象负责解析和注册bean定义信息:
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
createReaderContext返回了的对象为XmlReaderContext,该XmlReaderContext的构造函数中的第四个参数NamespaceHandlerResolver非常重要,它携带着解析spring配置的必要信息。这个对象的实际类型为DefaultNamespaceHandlerResolver。
public XmlReaderContext createReaderContext(Resource resource) {
return new XmlReaderContext(resource, this.problemReporter, this.eventListener,
this.sourceExtractor, this, getNamespaceHandlerResolver());
}
public NamespaceHandlerResolver getNamespaceHandlerResolver() {
if (this.namespaceHandlerResolver == null) {
this.namespaceHandlerResolver = createDefaultNamespaceHandlerResolver();
}
return this.namespaceHandlerResolver;
}
protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() {
return new DefaultNamespaceHandlerResolver(getResourceLoader().getClassLoader());
}
DefaultNamespaceHandlerResolver,从名字看它的意思是缺省的命名空间的处理解析器。最终调用的构造函数包含两个参数,classloader和DEFAULT_HANDLER_MAPPINGS_LOCATION。DEFAULT_HANDLER_MAPPINGS_LOCATION是常量字符串,字符串值为"META-INF/spring.handlers"。
public static final String DEFAULT_HANDLER_MAPPINGS_LOCATION = "META-INF/spring.handlers";
public DefaultNamespaceHandlerResolver(ClassLoader classLoader) {
this(classLoader, DEFAULT_HANDLER_MAPPINGS_LOCATION);
}
public DefaultNamespaceHandlerResolver(ClassLoader classLoader, String handlerMappingsLocation) {
Assert.notNull(handlerMappingsLocation, "Handler mappings location must not be null");
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
this.handlerMappingsLocation = handlerMappingsLocation;
}
查看spring几个jar包中的META-INF,发现了相应的配置文件spring.handlers,如下图:
以spring-context中的handlers配置为例,查看其中内容,内容以key value的形式存放,key为命名空间,value为相应命名空间的处理器。http://www.springframework.org/schema/context命名空间对应的处理器为org.springframework.context.config.ContextNamespaceHandler。
http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
http\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler
http\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler
http\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler
http\://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler
ContextNamespaceHandler实现如下,这个类实现了父类的init方法,方法中注册了多种bean解析器,这些解析器的类型均为BeanDefinitionParser接口的子类。另外,注册方法的参数为key value,看到这些key值是不是很熟悉,这些key值就是spring xml中的一些配置的值,因为我还没有怎么深入使用过spring,仅了解个别配置项,如annotation-config。不过这些配置网上资料都很多,感兴趣的直接google或者百度即可。后面我们可以看到,从源码中也可以了解这些配置的实际用处。
public class ContextNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("property-placeholder", new PropertyPlaceholderBeanDefinitionParser());
registerBeanDefinitionParser("property-override", new PropertyOverrideBeanDefinitionParser());
registerBeanDefinitionParser("annotation-config", new AnnotationConfigBeanDefinitionParser());
registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser());
registerBeanDefinitionParser("load-time-weaver", new LoadTimeWeaverBeanDefinitionParser());
registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
registerBeanDefinitionParser("mbean-export", new MBeanExportBeanDefinitionParser());
registerBeanDefinitionParser("mbean-server", new MBeanServerBeanDefinitionParser());
}
}
BeanDefinitionParser接口,只包含一个方法,该方法的参数为xml的Element元素,返回值为bean的定义信息。所有的parser都是用来对bean信息进行解析的。
public interface BeanDefinitionParser {
/**
* Parse the specified {@link Element} and register the resulting
* {@link BeanDefinition BeanDefinition(s)} with the
* {@link org.springframework.beans.factory.xml.ParserContext#getRegistry() BeanDefinitionRegistry}
* embedded in the supplied {@link ParserContext}.
* Implementations must return the primary {@link BeanDefinition} that results
* from the parse if they will ever be used in a nested fashion (for example as
* an inner tag in a {@code } tag). Implementations may return
* {@code null} if they will not be used in a nested fashion.
* @param element the element that is to be parsed into one or more {@link BeanDefinition BeanDefinitions}
* @param parserContext the object encapsulating the current state of the parsing process;
* provides access to a {@link org.springframework.beans.factory.support.BeanDefinitionRegistry}
* @return the primary {@link BeanDefinition}
*/
BeanDefinition parse(Element element, ParserContext parserContext);
}
现在,再回到前面DefaultNamespaceHandlerResolver的初始化过程,实际上就是将所有spring包中的spring.handlers配置文件读入,然后将这些处理器的信息(命名空间名称为key值,处理器名称为value值)保存到DefaultNamespaceHandlerResolver对象的handlerMappings中。
/** Stores the mappings from namespace URI to NamespaceHandler class name / instance */
private volatile Map handlerMappings;
/**
* Load the specified NamespaceHandler mappings lazily.
*/
private Map getHandlerMappings() {
if (this.handlerMappings == null) {
synchronized (this) {
if (this.handlerMappings == null) {
try {
Properties mappings =
PropertiesLoaderUtils.loadAllProperties(this.handlerMappingsLocation, this.classLoader);
if (logger.isDebugEnabled()) {
logger.debug("Loaded NamespaceHandler mappings: " + mappings);
}
Map handlerMappings = new ConcurrentHashMap(mappings.size());
CollectionUtils.mergePropertiesIntoMap(mappings, handlerMappings);
this.handlerMappings = handlerMappings;
}
catch (IOException ex) {
throw new IllegalStateException(
"Unable to load NamespaceHandler mappings from location [" + this.handlerMappingsLocation + "]", ex);
}
}
}
}
return this.handlerMappings;
}
DefaultNamespaceHandlerResolver作为构造器参数传入XmlReaderContext构造函数后,XmlReaderContext初始化完成。随后,调用documentReader.registerBeanDefinitions,doc和readerContext作为参数传入该方法。registerBeanDefinitions方法中,打印"Loading bean definitions",说明在此处开始加载bean定义信息。实际执行加载功能的方法是doRegisterBeanDefinitions(root),方法中创建了一个代理对象delegate,对象类型为BeanDefinitionParserDelegate,也从它的创建参数parent说明这个类也具有层次。
@Override
public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
this.readerContext = readerContext;
logger.debug("Loading bean definitions");
Element root = doc.getDocumentElement();
doRegisterBeanDefinitions(root);
}
/**
* Register each bean definition within the given root {@code } element.
*/
protected void doRegisterBeanDefinitions(Element root) {
// Any nested elements will cause recursion in this method. In
// order to propagate and preserve default-* attributes correctly,
// keep track of the current (parent) delegate, which may be null. Create
// the new (child) delegate with a reference to the parent for fallback purposes,
// then ultimately reset this.delegate back to its original (parent) reference.
// this behavior emulates a stack of delegates without actually necessitating one.
BeanDefinitionParserDelegate parent = this.delegate;
this.delegate = createDelegate(getReaderContext(), root, parent);
if (this.delegate.isDefaultNamespace(root)) {
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
if (StringUtils.hasText(profileSpec)) {
String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
return;
}
}
}
preProcessXml(root);
parseBeanDefinitions(root, this.delegate);
postProcessXml(root);
this.delegate = parent;
}
createDelegate方法,创建delegate对象,构造函数传入readerContext,从而让readerContext可以作为代理类解析bean定义信息。
protected BeanDefinitionParserDelegate createDelegate(
XmlReaderContext readerContext, Element root, BeanDefinitionParserDelegate parentDelegate) {
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
delegate.initDefaults(root, parentDelegate);
return delegate;
}
delegate对象执行的initDefaults方法,调用了populateDefaults,该方法主要用于初始化一些缺省参数。如DEFAULT_AUTOWIRE_ATTRIBUTE为"default-autowire",从下面的populateDefaults方法中可以看出,如果用户在xml配置中beans元素的属性中不指定该值,那么该值默认就为no。
public void initDefaults(Element root, BeanDefinitionParserDelegate parent) {
populateDefaults(this.defaults, (parent != null ? parent.defaults : null), root);
this.readerContext.fireDefaultsRegistered(this.defaults);
}
protected void populateDefaults(DocumentDefaultsDefinition defaults, DocumentDefaultsDefinition parentDefaults, Element root) {
String lazyInit = root.getAttribute(DEFAULT_LAZY_INIT_ATTRIBUTE);
if (DEFAULT_VALUE.equals(lazyInit)) {
lazyInit = parentDefaults != null ? parentDefaults.getLazyInit() : FALSE_VALUE;
}
defaults.setLazyInit(lazyInit);
String merge = root.getAttribute(DEFAULT_MERGE_ATTRIBUTE);
if (DEFAULT_VALUE.equals(merge)) {
merge = parentDefaults != null ? parentDefaults.getMerge() : FALSE_VALUE;
}
defaults.setMerge(merge);
String autowire = root.getAttribute(DEFAULT_AUTOWIRE_ATTRIBUTE);
if (DEFAULT_VALUE.equals(autowire)) {
autowire = parentDefaults != null ? parentDefaults.getAutowire() : AUTOWIRE_NO_VALUE;
}
defaults.setAutowire(autowire);
// don't fall back to parentDefaults for dependency-check as it's no
// longer supported in as of 3.0. Therefore, no nested
// would ever need to fall back to it.
defaults.setDependencyCheck(root.getAttribute(DEFAULT_DEPENDENCY_CHECK_ATTRIBUTE));
if (root.hasAttribute(DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE)) {
defaults.setAutowireCandidates(root.getAttribute(DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE));
}
else if (parentDefaults != null) {
defaults.setAutowireCandidates(parentDefaults.getAutowireCandidates());
}
if (root.hasAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE)) {
defaults.setInitMethod(root.getAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE));
}
else if (parentDefaults != null) {
defaults.setInitMethod(parentDefaults.getInitMethod());
}
if (root.hasAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE)) {
defaults.setDestroyMethod(root.getAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE));
}
else if (parentDefaults != null) {
defaults.setDestroyMethod(parentDefaults.getDestroyMethod());
}
defaults.setSource(this.readerContext.extractSource(root));
}
delegate初始化完成后,DefaultBeanDefinitionDocumentReader便相继调用preProcessXml(root),parseBeanDefinitions(root, this.delegate),postProcessXml(root)方法。其中preProcessXml和postProcessXml在DefaultBeanDefinitionDocumentReader是两个空实现,直接忽略。所以解析过程由parseBeanDefinitions(root, this.delegate)负责。该方法遍历xml中的元素,依次对相关元素进行解析处理。delegate.isDefaultNamespace(root)判断是否该元素的缺省命名空间为http://www.springframework.org/schema/beans,一般情况下,xml配置跟元素的缺省命名空间都会是http://www.springframework.org/schema/beans,所以一般都会到if的代码中。随后遍历所有相关元素,遇到对象为Element元素时,对元素进行解析,如果为缺省命名空间,调用parseDefaultElement(ele, delegate)方法解析,否则调用delegate.parseCustomElement(ele)。
/**
* Parse the elements at the root level in the document:
* "import", "alias", "bean".
* @param root the DOM root element of the document
*/
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);
}
}
下面是缺省命名空间(http://www.springframework.org/schema/beans)解析。缺省命名空间解析包含了import,alias,bean以及嵌套beans元素的解析。
private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
importBeanDefinitionResource(ele);
}
else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
processAliasRegistration(ele);
}
else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
processBeanDefinition(ele, delegate);
}
else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
// recurse
doRegisterBeanDefinitions(ele);
}
}
以bean元素解析为例,可以看到最终由delegate对象代理解析xml元素,返回相应的BeanDefinitionHolder对象,该对象包含需要实例化该bean对象的所有信息。具体的解析过程实际上就是针对xml中bean元素的信息进行解析,解析出相应的id,name,别名等信息。返回BeanDefinitionHolder对象后,会使用bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder)对其进行包装并返回包装过的对象,该包装过程实际上是判断该元素是否存非缺省命名空间的信息,如果发现有非缺省命名空间的信息,那么会调用相应的handler对BeanDefinitionHolder对象再处理一遍,这样才能解析完整,包装完成后,将最终包装后的BeanDefinitionHolder对象注册进去。至此,缺省命名空间的bean定义信息解析完成。
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
if (bdHolder != null) {
bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
try {
// Register the final decorated instance.
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
}
catch (BeanDefinitionStoreException ex) {
getReaderContext().error("Failed to register bean definition with name '" +
bdHolder.getBeanName() + "'", ele, ex);
}
// Send registration event.
getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
}
}
非缺省命名空间元素的解析,调用的是delegate.parseCustomElement(ele),实际调用的是parseCustomElement(Element ele, BeanDefinition containingBd) 。看到这个方法的实现,就可以明白前面spring.handlers中配置文件中加载的处理器信息,其实是用来对非缺省命名空间的xml元素进行解析的。其过程首先将xml元素中的命名空间namespaceUri取出,然后用相应的namespaceUri取出对应的handler,最终调用handler的parse方法进行解析。
public BeanDefinition parseCustomElement(Element ele) {
return parseCustomElement(ele, null);
}
public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) {
String namespaceUri = getNamespaceURI(ele);
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));
}
以如下xml元素为例,
前面已经列出了ContextNamespaceHandler的实现。而annotation-config对应的具体parser为AnnotationConfigBeanDefinitionParser,这就意味,解析到这个xml元素时,会调用AnnotationConfigBeanDefinitionParser的parse方法解析该元素。
registerBeanDefinitionParser("annotation-config", new AnnotationConfigBeanDefinitionParser());
下面代码是AnnotationConfigBeanDefinitionParser的实现,parse方法进行具体解析。该部分代码注册了一些跟该xml元素功能相关的组件(主要是跟spring注释相关)。
public class AnnotationConfigBeanDefinitionParser implements BeanDefinitionParser {
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
Object source = parserContext.extractSource(element);
// Obtain bean definitions for all relevant BeanPostProcessors.
Set processorDefinitions =
AnnotationConfigUtils.registerAnnotationConfigProcessors(parserContext.getRegistry(), source);
// Register component for the surrounding element.
CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
parserContext.pushContainingComponent(compDefinition);
// Nest the concrete beans in the surrounding component.
for (BeanDefinitionHolder processorDefinition : processorDefinitions) {
parserContext.registerComponent(new BeanComponentDefinition(processorDefinition));
}
// Finally register the composite component.
parserContext.popAndRegisterContainingComponent();
return null;
}
}
当所有的xml的element都被遍历过,并且被相关对应的handler的相应parser处理完成后,beanFactory的初始化便完成了。
总结一下,beanFactory的初始化,最重要的工作就是将xml加载成doc对象,针对doc对象进行解析,期间会读取spring.handlers的配置文件,根据doc对象中的xml元素的命名空间进行解析。针对缺省命名空间(beans)的解析直接使用默认方法;针对非缺省命名空间的解析,需要获取相应命名空间的handler,通过handler对xml元素进行解析,而实际执行解析方法的是handler对象中注册的一组parser。可以看出,parser实际上是支持用户注入的,通过增加parser和handler,我们就可以增强spring框架xml配置的描述能力以及spring的能力。后续会抽时间针对spring的主要的parser进行分析。