【问题标题】:Spring application context file location in IntelliJIntelliJ 中的 Spring 应用程序上下文文件位置
【发布时间】:2014-10-28 20:14:11
【问题描述】:
我试图找出将spring application context XML 文件(映射服务Bean)放在IntelliJ 生成的Spring MVC 模板中的位置。如果我把它放在WEB-INF 文件夹中,当我在dispatcher servlet XML 文件中导入XML 时它会报错。如果我把它放在 webapp 文件夹中,tomcat 会抱怨它在WEB-INF 文件夹中找不到它。那么我到底应该把它放在哪里呢?
IntelliJ 为Spring MVC 应用程序生成的模板在webapp dir 中有一个“pages”文件夹。我是否必须像在 Eclipse 中那样将我的 jsps 放在那里或放在 WEB-INF 文件夹中?在Tomcat中部署时,根上下文路径如何映射到该文件夹?
【问题讨论】:
标签:
java
spring
spring-mvc
tomcat
intellij-idea
【解决方案1】:
在 web.xml 中,您提到了 contextConfigLocation,其中提到了上下文读取的资源的位置和名称。此元素应包含 XML 文件的路径和名称。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:com/some/path/applicationContext.xml classpath:dbContext.xml
</param-value>
</context-param>
现在 dbcontext.xml 直接位于类路径下,而 applicationContext 位于文件夹 heirarchy com/some/path 中的类路径中
构建应用程序时,类通常会进入WEB-INF/classes 目录。因此,如果使用上述配置进行战争,则 dbContext 将位于 classes 目录下。
通常,在您的项目中创建另一个源文件夹可能是个好主意。将其命名为 resources 或 config 之类的名称,并将您的配置文件放在那里。
这有帮助吗?
【解决方案2】:
您可以将spring-context 文件放在存在dispatcher-servlet 的同一目录中,或者您可以在web-inf 中创建新目录,例如spring,您可以放入spring-context 文件。
假设您已将spring-context 放入spring directory 中,那么您可以这样称呼它:
<import resource="spring/spring-context.xml"/>
在dispatcher-servlet.xml.
看看这是我的 spring 应用程序的结构并且工作得很好:
还有一些问题,然后给我发帖。
【解决方案3】:
你可以把它保存在classpath中,在web.xml中有如下配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
org.springframework.web.context.ContextLoaderListener
在 Maven 项目中,我们将其保存在资源文件夹中。