Spring bean configuration file path

Source: Internet
Author: User

Spring bean configuration file path

When configuring beans, you can use contextloaderlistener or contextloaderservlet with context-Param named contextconfiglocation, or define it in init-Param of dispatchservlet.

However, it should be noted that, in any case, when the Web Container initializes dispatchservlet, it will find the configuration file for it. The default location and name of this profile is/WEB-INF/servletname-servlet.xml. So, even if you have used the contextloaderlistener or contextloaderservlet, the configuration file/WEB-INF/servletname-servlet.xml is still required.

Sometimes we need to customize all the configuration files, for example, I want to put all the spring-related configuration files under the directory/WEB-INF/spring, I also want to replace the envoy-servlet.xml with the appcontent-Servlet File Name. For example:

The related part of my configuration file web. XML is 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 in it is actually the configuration file with the original default name/WEB-INF/envoy-servlet.xml.

We hope this configuration can work. However, it is a pity that it cannot be started normally.

Why? The reason is that when the Web Container starts the servlet named envoy, it will try to load the bean definition configuration file. Even if we have used contextloaderlistener to load bean definitions, it will still find that no one defines its default configuration file for this servlet, so it will try to use the default path and name to load the file. This path is/WEB-INF/envoy-servlet.xml. But we have renamed this file and put it under the path/Web-INF/config/appContent-servlet.xml, the Web Container will not find the file, and reported an error.

So what can we do to correctly configure all the loaded files? We can use the child node init-Param configured by Servlet instead of contextloaderlistener or contextloaderservlet. Let's take an example:

The related part of the new configuration file web. XML is 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. This time, when the Web Container name is envoy's servlet, the system can find that the parameter contextconfiglocation required by this servlet is here, the default path is replaced by the custom path.

My doubt is that since we have used context-Param to define the contentconfiglocation variable, when the Web container loads a servlet named envoy, we should use this path to replace the default path. Why is this not the case?

I guess the reason is that spring does not specify the path of the dispatchservlet configuration file, but uses this path to specify the path of the configuration file for contentloaderlistener or contentloaderservlet. Therefore, the dispatchservlet still needs to load the servlet configuration file at startup, resulting in the above results.

Due to insufficient time, I did not confirm whether it was the actual reason. I will confirm it later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.