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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:24:47  来源:igfitidea点击:

Spring application context file location in IntelliJ

javaspringspring-mvctomcatintellij-idea

提问by iceman

I am trying to figure out where to put the spring application context XMLfile (mapping Service Beans) in a IntelliJgenerated Spring MVCtemplate. If I put it in the WEB-INFfolder, it complains when I import the XML in the dispatcher servlet XMLfile. If i put it in the webapp folder, tomcat complains that it can't find it in the WEB-INFfolder. So where do I actually put it?

我试图找出spring application context XMLIntelliJ生成的Spring MVC模板中放置文件(映射服务 Bean)的位置。如果我把它放在WEB-INF文件夹中,当我在dispatcher servlet XML文件中导入 XML 时它会抱怨。如果我把它放在 webapp 文件夹中,tomcat 会抱怨它在WEB-INF文件夹中找不到它。那么我实际上把它放在哪里呢?

The template generated by IntelliJfor a Spring MVCapplication has a 'pages' folder inside webapp dir. Do I have to put my jspsthere or in the WEB-INFfolder as in Eclipse? How is the root context path mapped to this folder when deployed in Tomcat?

IntelliJSpring MVC应用程序生成的模板在webapp dir. 我是否必须像在 Eclipse 中一样将我的放在jsps那里或WEB-INF文件夹中?部署在 中时,根上下文路径如何映射到此文件夹Tomcat

spring application XML

弹簧应用程序 XML

回答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-contextfile in the same directory where dispatcher-servletis exist or you can create new directory inside web-inf say spring in that you can put the spring-contextfile.

您可以将spring-context文件放在dispatcher-servlet存在的同一目录中,或者您可以在 web-inf 中创建新目录,例如 spring,您可以将spring-context文件放在其中。

suppose you have put the spring-contextinside spring directorythen 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 应用程序结构,并且运行良好:

enter image description here

在此处输入图片说明

still some problem then post me.

还有一些问题,然后发布我。