applicationContext.xml is not getting loaded when I have kept the spring-servlet.xml in Web application

老子叫甜甜 提交于 2019-12-06 04:06:20

The applicationContext.xml file is not loaded by default unless you configure spring ContextLoaderListener in the web.xml or other similar configuration file.

I am assuming that the applicationContext.xml is present inside WEB-INF folder.

  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/applicationContext.xml
    </param-value>
    </context-param>

If you have multiple configuration files, you can specify them as , separated values like

<param-value> classpath:applicationContext.xml, classpath:securityContext.xml </param-value>

It appears that your previous configuration worked because you provided the dispatcher servlet with the name that matched the naming format of application context associated with it.

Now since you want to load the root web app context, you need to define the listener in web.xml called ContextLoaderListener which by default will read the context definition from /WEB-INF/applicationContext.xml

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!