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

六月ゝ 毕业季﹏ 提交于 2020-01-02 08:22:12

问题


I am Working on a web application in eclipse. A spring MVC web applicatication to be precised. I have created servlet xml and mapped it in web.xml. Everything is working fine till here & Servlet loads the HTML page But now I want to add a JPA layer. So I have created applicationContext.xml in WEB-INF and put all JPA related spring configurations in that file. But when I start my web application, JPA related stuff is not getting loaded. What I believe is, applicationContext.xml itself is not getting loaded. Any Idea or any other configuration I am missing here?


回答1:


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>




回答2:


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>


来源:https://stackoverflow.com/questions/33906052/applicationcontext-xml-is-not-getting-loaded-when-i-have-kept-the-spring-servlet

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