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?
-
Please check out my answer. You may accept and up vote, if it serves the purpose. – SyntaX Nov 25 '15 at 06:52
2 Answers
The
applicationContext.xml
file is not loaded by default unless you configure springContextLoaderListener
in theweb.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,090
- 17
- 30
-
Great to know that it worked for you, I hope you don't mind giving me an up vote. Thanks! – SyntaX Nov 26 '15 at 05:06
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>

- 1,056
- 3
- 12
- 27