问题
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 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:
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