Spring's bean configuration file path issue
When configuring beans, you can use Contextloaderlistener or Contextloaderservlet with Context-param named Contextconfiglocation. can also be defined in the Init-param of Dispatchservlet.
But it should be noted, however, that when the Web container initializes the Dispatchservlet, it will look for the configuration file it is for. The default location and name of this profile is/web-inf/servletname-servlet.xml. Therefore, even if you have already used Contextloaderlistener or Contextloaderservlet, the configuration file/web-inf/servletname-servlet.xml is still required.
Sometimes we need to customize all of the configuration files, for example, I want to put all the spring-related profiles under the directory/web-inf/spring/, and I want to replace them with the filename appcontent-servlet Envoy-servlet.xml. As an example:
The relevant sections of my profile Web.xml are as follows:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/web-inf/config/appcontent-servlet.xml
/web-inf/config/appcontent-service.xml
</param-value>
</context-param>
<listener>
<listener-class>
Org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>envoy</servlet-name>
<servlet-class>
Org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
Note that the/web-inf/config/appcontent-servlet.xml is actually the original default named/web-inf/envoy-servlet.xml configuration file.
We want this configuration to work. But unfortunately, it does not start properly.
Why, then? The reason is that when the Web container launches a servlet named Envoy, it tries to load the bean definition configuration file. Even though we have used Contextloaderlistener to load the bean definition, it still finds that no one has defined its default profile for this servlet, so it will try to load the file with the default path and name. This path is/web-inf/envoy-servlet.xml. But we have renamed this file and placed it under the path/web-inf/config/appcontent-servlet.xml, the WEB container will not find the file, and the error.
So what are we going to do to properly configure all the loading files? Instead of Contextloaderlistener or contextloaderservlet, we can init-param the child nodes configured with the servlet. Let's give you an example:
The relevant sections of the new profile Web.xml are as follows:
<servlet>
<servlet-name>envoy</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/web-inf/config/appcontent-service.xml,
/web-inf/config/appcontent-servlet.xml
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
Obviously, the new configuration is more concise, and this time when the Web container is named the envoy Servlet, the system can find that this servlet needs a parameter named Contextconfiglocation, which replaces the default path with the customer-defined path.
My doubt is that since we have used Context-param to define the contentconfiglocation variable, then when the Web container loads the servlet named envoy, it should use this path instead of the default path. Why is that not the case?
I guess the reason is that spring is not the path of the configuration to indicate the path to the Dispatchservlet configuration file, but to use this path to Contentloaderlistener or Contentloaderservlet indicates the path to its configuration file. So Dispatchservlet still needs to load the servlet's configuration file at boot time, which leads to the above results.
Because time is not enough, not to confirm whether it is the real reason, and then later to confirm it.