IOC part 1: parsing of xml files

We start the analysis with a line of code in the main function:

ConfigurableListableBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring.xml"));

Most people may have doubts that now loading the XML file is not via :

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); Is the spring application context to load and manipulate XML documents? That's right, there is no spring application context object in the spring 2.5 version. This is the application context object added by spring after the version spring 3. This object contains all the functions of the XmlBeanFactory object and extends it. We first analyze the parsing process of XmlBeanFactory .

1.1 Encapsulation of configuration files

Spring is encapsulated by the ClassPathResource object. The ClassPathResource object implements the esource interface, and the Resource interface object encapsulates all the underlying resources used internally by Spring : File , URL , ClassPath , etc. It defines three methods for judging the current resource status: existence ( exists ), readability ( isReadable ), and whether it is in an open state ( isOpen ). There are corresponding implementations for resource files from different sources: file (FileSystemResource) , ClassPath resource (ClassPathResource) , URL resource ( UrlResource ), InputStream resource (InputStreamResource) , etc. With the Resource interface, resource files can be processed uniformly. whenAfter Resoure completes the encapsulation of the configuration file, the reading of the configuration file is fully handed over to XmlBeanDefineReader for processing.

new  ClassPathResource( "spring.xml" ) to complete the encapsulation of the resource file. The data loading is completed through the constructor of XmlBeanFactory: 

 

 

public XmlBeanFactory(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

 */

public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {

super(parentBeanFactory);

this.reader.loadBeanDefinitions(resource);

}

 

this.reader.loadBeanDefinitions(resource) is the final implementation of loading resource files. Reader refers to XmlBeanDefineReader.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325205834&siteId=291194637