java IntelliJ 中的 Spring 应用程序上下文文件位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25659381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring application context file location in IntelliJ
提问by iceman
I am trying to figure out where to put the spring application context XML
file (mapping Service Beans) in a IntelliJ
generated Spring MVC
template. If I put it in the WEB-INF
folder, it complains when I import the XML in the dispatcher servlet XML
file. If i put it in the webapp folder, tomcat complains that it can't find it in the WEB-INF
folder. So where do I actually put it?
我试图找出spring application context XML
在IntelliJ
生成的Spring MVC
模板中放置文件(映射服务 Bean)的位置。如果我把它放在WEB-INF
文件夹中,当我在dispatcher servlet XML
文件中导入 XML 时它会抱怨。如果我把它放在 webapp 文件夹中,tomcat 会抱怨它在WEB-INF
文件夹中找不到它。那么我实际上把它放在哪里呢?
The template generated by IntelliJ
for a Spring MVC
application has a 'pages' folder inside webapp dir
. Do I have to put my jsps
there or in the WEB-INF
folder as in Eclipse? How is the root context path mapped to this folder when deployed in Tomcat
?
IntelliJ
为Spring MVC
应用程序生成的模板在webapp dir
. 我是否必须像在 Eclipse 中一样将我的放在jsps
那里或WEB-INF
文件夹中?部署在 中时,根上下文路径如何映射到此文件夹Tomcat
?
回答by Atul
In the web.xmlyou mention contextConfigLocationwhich mentions the locations and names of the resources which the context reads. This element should contain the path and name of the XML files.
在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>
Now the dbcontext.xmlis directly under the classpath whereas applicationContext is in the classpath in the folder heirarchy com/some/path
现在dbcontext.xml直接位于类路径下,而 applicationContext 位于文件夹 heirarchy com/some/path 中的类路径中
When the application gets built, the classess generally go into WEB-INF/classesdirectory. So if a war is made with above config, dbContext will be under classes dir. Generally, it could be a good idea to create another source folder in your project. Name it something like resources or config and put your configuration files there.
构建应用程序时,类通常会进入WEB-INF/classes目录。因此,如果使用上述配置进行War,dbContext 将位于类目录下。通常,在项目中创建另一个源文件夹可能是个好主意。将其命名为 resources 或 config 并将您的配置文件放在那里。
Does this help?
这有帮助吗?
回答by RPaul
You can keep it in classpath and have the following configuration in web.xml
您可以将其保留在类路径中,并在 web.xml 中进行以下配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
org.springframework.web.context.ContextLoaderListener In a maven project we are keeping it inside resources folder.
org.springframework.web.context.ContextLoaderListener 在 Maven 项目中,我们将它保存在资源文件夹中。
回答by user3145373 ツ
You can put spring-context
file in the same directory where dispatcher-servlet
is exist or you can create new directory inside web-inf say spring in that you can put the spring-context
file.
您可以将spring-context
文件放在dispatcher-servlet
存在的同一目录中,或者您可以在 web-inf 中创建新目录,例如 spring,您可以将spring-context
文件放在其中。
suppose you have put the spring-context
inside spring directory
then you can call it like :
假设你把spring-context
里面放了,spring directory
那么你可以这样称呼它:
<import resource="spring/spring-context.xml"/>
in dispatcher-servlet.xml
.
在dispatcher-servlet.xml
.
look this is my structure of spring application and working great :
看看这是我的 Spring 应用程序结构,并且运行良好:
still some problem then post me.
还有一些问题,然后发布我。