How Spring loads xml configuration files: ApplicationContext
Everyone knows that Java reads common files through InputStream, OutStream, Reader, and Writer in Basic I/O. In the spring framework, how does it identify the xml configuration file?
This depends on the two interfaces of the IoC container.BeanFactoryAndApplicationContext:
BeanFactory(Interface)
| -------- XmlBeanFactory (Implementation class)
ApplicationContext(Interface)
| -------- ClassPathXmlApplicationContext (Implementation class)
| --------- FileSystemXmlApplicationContext (Implementation class)
| --------- XmlWebApplicationContext (Implementation class)
BeanFactory is an interface that Spring uses to instantiate, configure, and manage objects. It has a getBean () method that only provides the most basic features of spring, it is generally used in the case of low memory, such as Applet. The general spring project uses its derived class ApplicationContext. This class will automatically parse the xml file we configured, and then use the configured bean to create a new object and put the new object into a Map, the key is the bean id, and the value is the new object.
1.Compilation pathClassPathXmlApplicationContext to load xml files (example http://blog.csdn.net/shymi1991/article/details/48085955 in our getting started)
In this way, the xml file must be in the build path of the Project. Generally, classpath: is used as the prefix and can be omitted.
1) under the/src directory
ApplicationContext factory = new ClassPathXmlApplicationContext (classpath: appcontext. xml );
ApplicationContext factory = new ClassPathXmlApplicationContext (appcontext. xml );
2) In the/src/conf directory
ApplicationContext factory = new ClassPathXmlApplicationContext (conf/appcontext. xml );
3) Load multiple xml files at the same time
ApplicationContext factory = new ClassPathXmlApplicationContext (new String [] {bean1.xml, bean2.xml });
2. File System Path
The absolute path of an xml file. Generally, file: is used as the prefix. It can also be omitted.
ApplicationContext factory = new FileSystemXmlApplicationContext (file: G:/Test/src/appcontext. xml );
ApplicationContext factory = new FileSystemXmlApplicationContext (G:/Test/src/appcontext. xml );
3. XmlWebApplicationContext is customized for Web engineering
ServletContext servletContext = request. getSession (). getServletContext ();
ApplicationContext ctx = WebApplicationContextUtils. getWebApplicationContext (servletContext );
??