This article combines the previous compilation of spring source code and unit tests andSpring official documentStart with the details, learn how to use spring frmaework and how to implement it. Another purpose: learn how to view open source project source code.
First from the spring entrance, found in the spring compiled source code/spring-webmvc/src/test/resources/org/springframework/web/context/WEB-INF/web.xmlFile, why did you find this file first?(Web.xml detailed),
<!-- This servlet must be loaded first to configure the log4j system and create the WebApplicationContext -->
<servlet>
<servlet-name>config</servlet-name>
<servlet-class>org.springframework.framework.web.context.ContextLoaderServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.framework.web.context.XMLWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>log4jPropertiesUrl</param-name>
<param-value>/WEB-INF/log4j_PRODUCTION.properties</param-value>
</init-param>
<!-- This is essential -->
<load-on-startup>1</load-on-startup>
</servlet>
Can see the first loadorg.springframework.framework.web.context.ContextLoaderServletFile, then eclipse Ctrl+Shift+R, search this file, even did not find 😢, the teacher is not good.
BaiduCannot find ContextLoaderServlet", the prompt was removed in Spring 3.0
ContextLoaderServletwithLog4jConfigServlet, so there will be an exception error that can't find the class. There are two other startup methods available under Spring 3.0:ContextLoaderListenerwithContextLoaderPlugIn. RecommendedContextLoaderListener. Then look upContextLoaderListenerFile, this is finally, do not understand why the source code and configuration files do not match? Then I found a use on the Internet.ContextLoaderListenerConfigurationweb.xml 。
<!-- When using Spring in the project, there is no BeanFactory in the applicationContext.xml configuration file. To directly reference the Spring container managed bean in the class file in the business layer, you can do the following -->
<!--1, configure the listener ContextLoaderListener--> in web.xml
The role of <!--ContextLoaderListener is to automatically assemble the configuration information of the ApplicationContext when starting the web container. Because it implements the ServletContextListener interface, configure this listener in web.xml, and when it starts the container, it will execute its implementation by default.
The ContextLoader class is associated with the ContextLoaderListener, so the entire load configuration process is done by the ContextLoader.
Its API description
The first paragraph shows that the ContextLoader can be generated by the ContextLoaderListener and ContextLoaderServlet.
If you look at the ContextLoaderServlet API, you can see that it is also associated with the ContextLoader class and it implements the HttpServlet interface.
In the second paragraph, the ContextLoader creates a class such as XmlWebApplicationContext, and the interface it implements is WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->
BeanFactory, so all the beans in spring are created by this class.
IUploaddatafileManager uploadmanager = (IUploaddatafileManager) ContextLoaderListener.getCurrentWebApplicationContext().getBean("uploadManager");
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--2, deploy the xml file of applicationContext -->
<!--If you do not write any parameter configuration information in web.xml, the default path is "/WEB-INF/applicationContext.xml,
The name of the xml file created in the WEB-INF directory must be applicationContext.xml.
If you want to customize the file name, you can add the contextConfigLocation context parameter in web.xml:
Specify the corresponding xml file name in <param-value> </param-value>. If there are multiple xml files, they can be written together and separated by ",".
You can also use wildcards in applicationContext-*.xml. For example, there is applicationContext-ibatis-base.xml in this directory.
ApplicationsContext-action.xml, applicationContext-ibatis-dao.xml, etc. will all be loaded together.
The ContextLoader class is associated with the ContextLoaderListener, so the entire load configuration process is done by the ContextLoader. -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
1. If you do not write any parameter configuration information in web.xml, the default path is "/WEB-INF/applicationContext.xml, the name of the xml file created in the WEB-INF directory must be applicationContext.xml2. If you want to customize the file name, you can add the contextConfigLocation context parameter in web.xml.
Look at the source code with the question, 1. How to specify the default path? 2. How is the custom file name implemented?
Open the ContextLoaderListener.java file.
/**
* Initialize the root web application context.
*/
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}```
Enter the `initWebApplicationContext` method

Set a custom configuration file in this method. If not specified, the default path is called by the refresh() method.
1. How is the default path specified?

When the contextClassName is not specified, the default className is taken from defaultStrategies


Wac returns the default XmlWebApplicationContext instance.

Find the `refresh()` method of `XmlWebApplicationContext`, which is not found in the class. It is always found in the parent class of the upper layer, and finally found in `AbstractApplicationContext`.

`refresh()` The first level call is until the `getDefaultConfigLoactions()` method of `XmlWebApplicationContext`

Return the default configuration file address
2. How is the custom file name implemented?
`public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";`

Check if the contextConfigLocation parameter is specified? If there is, wac.setConfigLocation() adds a custom configuration file.

If you have already specified a custom file, the default location is no longer used. Before I found this place, `getDefaultConfigLocation()` returned `null`, which did not return defaultContextLoaction. Later I want to understand that because wac is an instance of `XmlWebApplicationContext`, I should also call the `getDefaultConfigLocation()` method of `XmlWebApplicationContext` instead of the parent class method here.
View my other Spring source code analysis article 1. This () method a. Construct DEFAULTLISTableBeanfactory, AnnotatedBeandefinitionReader, ClassPathBeandefinitionScanner b. In the process of building...
The full text is based on Python 2.7 macOS 10.12.2 Werkzeug is a library of functions used by the Python implementation of Python. What is WSGI?How to understand CGI, WSGIThere are a lot of instructio...
Original content, please indicate the source 1, the Spring framework The Spring framework is a layered architecture, with modular partitioning, as shown below, listing the main modules. Core con...
A spring framework structure The functionality of the Spring framework consists of approximately 20 modules, which can be grouped into: Core Container (core container): Beans: Responsible for the asse...
It is recommended to use [ClassPathXMLApplicationContext] as the debug entrance. The general steps are as follows: 1. Create a Spring project and write simple test cases; 2. Create the META-INF direct...
-------------------------- group is spring-boot-2.0.0.BUILD-SNAPSHOT ----------- --------------------------- Normally, we would be a spring boot entry procedures so written about In this section we an...
Article Directory 1. Account in advance Second, the entrance 1. Account in advance This series is based on thinkphp5.1.39 LTS version analysis, you can go to the official website to download. The use ...
Article Directory Preface Inspiration for this article Give a UML diagram Ask the first question Ask the second question Benefits of interface-parent-child model Preface The physical state precedes th...
We mentioned the Vue.js build process before. In the web application, we analyze the Vue.js built by Runtime + Compiler. The entry point is src/platforms/web/entry-runtime-with-compiler.js : Vue's ent...
1: Core thinking The core thinking of RXJAVA1.0 and RXJAVA2.0 is the observer mode, but RXJAVA2.0 optimizes some methods in the basis of RXJAVA1.0, which is convenient for developers to better underst...