Order of loading contextConfigLocation in web.xml of Spring Servlet project

前端 未结 4 486
栀梦
栀梦 2020-12-13 00:00

Suppose that I have a Spring Java project and I am trying to configure it as a web server servlet. Here is a stripped-down version of the web.xml f

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 00:23

    If you are having ContextLoaderListener in your web.xml spring will load the generalApplicationContext.xml first. This will create beans and provide them all Servlets and Filters. This xml should have the common classes, beans which are used in your application.

    Later spring container will load the specificApplicationContext.xml, because you have load on startup in the servlet config. If you don't specify the load on start up, this specificApplicationContext.xml will load when the first request comes to your application with the specific url-pattern.

    As your question when you move you springconfig from one config to another this will change the application resource availability to the container. If you specify the Controller beans in generalApplicationContext.xml and you don't specify them in specificApplicationContext.xml then your DispatcherServlet wont find the mappings, so you will see 404 error.

    If you want to create some bean objects on-demand you can create one more servlet-config to load that specific specificConfigurationFile2.xml, and map to the url-pattern.

提交回复
热议问题