Spring原始碼——BeanFactory 註冊bean
阿新 • • 發佈:2019-01-24
XmlBeanFactory
繼承DefaultListableBeanFactory,實現通過XML讀取的方式來獲取Bean的定義。 XmlBeanFactory維護一個XmlbeanDefinitionReader來載入需要的resource解析出的BeanDefinition。 XmlBeanFactory構造方法如下:構造方法,通過reader和resource來載入對應的BeanDefinition。
/**
* Create a new XmlBeanFactory with the given resource,
* which must be parsable using DOM.
* @param resource XML resource to load bean definitions from
* @throws BeansException in case of loading or parsing errors
*/
publicXmlBeanFactory(Resource resource) throws BeansException{
this(resource, null);
}
/**
* Create a new XmlBeanFactory with the given input stream,
* which must be parsable using DOM.
* @param resource XML resource to load bean definitions from
* @param parentBeanFactory parent bean factory
* @throws BeansException in case of loading or parsing errors
*/
publicXmlBeanFactory(Resource resource,BeanFactory parentBeanFactory) throws BeansException{
super(parentBeanFactory);
this.reader.loadBeanDefinitions(resource);
}
loadBeanDefinitions
方法中校驗對應的資原始檔,校驗完成後獲取輸入流,最後在進行Load。
/**
* Load bean definitions from the specified XML file.
* @param encodedResource the resource descriptor for the XML file,
* allowing to specify an encoding to use for parsing the file
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
publicint loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException{
Assert.notNull(encodedResource,"EncodedResource must not be null");
if(logger.isInfoEnabled()){
logger.info("Loading XML bean definitions from "+ encodedResource.getResource());
}
Set<EncodedResource> currentResources =this.resourcesCurrentlyBeingLoaded.get();
if(currentResources == null){
currentResources =newHashSet<EncodedResource>(4);
this.resourcesCurrentlyBeingLoaded.set(currentResources);
}
if(!currentResources.add(encodedResource)){
thrownewBeanDefinitionStoreException(
"Detected cyclic loading of "+ encodedResource +" - check your import definitions!");
}
try{
InputStream inputStream = encodedResource.getResource().getInputStream();
try{
InputSource inputSource =newInputSource(inputStream);
if(encodedResource.getEncoding()!= null){
inputSource.setEncoding(encodedResource.getEncoding());
}
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
finally {
inputStream.close();
}
}
catch(IOException ex){
thrownewBeanDefinitionStoreException(
"IOException parsing XML document from "+ encodedResource.getResource(), ex);
}
finally {
currentResources.remove(encodedResource);
if(currentResources.isEmpty()){
this.resourcesCurrentlyBeingLoaded.remove();
}
}
}
獲取對應的Document,然後註冊BeanDefinition.
/**
* Actually load bean definitions from the specified XML file.
* @param inputSource the SAX InputSource to read from
* @param resource the resource descriptor for the XML file
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
* @see #doLoadDocument
* @see #registerBeanDefinitions
*/
protectedint 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){
thrownewXmlBeanDefinitionStoreException(resource.getDescription(),
"Line "+ ex.getLineNumber()+" in XML document from "+ resource +" is invalid", ex);
}
catch(SAXException ex){
thrownewXmlBeanDefinitionStoreException(resource.getDescription(),
"XML document from "+ resource +" is invalid", ex);
}
catch(ParserConfigurationException ex){
thrownewBeanDefinitionStoreException(resource.getDescription(),
"Parser configuration exception parsing XML from "+ resource, ex);
}
catch(IOException ex){
thrownewBeanDefinitionStoreException(resource.getDescription(),
"IOException parsing XML document from "+ resource, ex);
}
catch(Throwable ex){
thrownewBeanDefinitionStoreException(resource.getDescription(),
"Unexpected exception parsing XML document from "+ resource, ex);
}
}
doLoadDocument中會對資原始檔進行校驗。 校驗完成後,註冊。
protectedDocument doLoadDocument(InputSource inputSource,Resource resource) throws Exception{
returnthis.documentLoader.loadDocument(inputSource, getEntityResolver(),this.errorHandler,
getValidationModeForResource(resource), isNamespaceAware());
}
publicint registerBeanDefinitions(Document doc,Resource resource) throws BeanDefinitionStoreException{
BeanDefinitionDocumentReader documentReader =