Java 我在哪里放置 applicationContext.xml 文件在 Spring 项目目录中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21244114/
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
Where do I place the applicationContext.xml file within Spring project directory
提问by dickturnip
I am trying to learn about the Spring framework for Java and I am not sure where about's I am supposed to put the applicationContext.xml file for configuring the spring beans. My directory structure is as follows:
我正在尝试了解 Java 的 Spring 框架,但我不确定应该将 applicationContext.xml 文件放在哪里以配置 spring bean。我的目录结构如下:


Do I put in .settings? Or should it be put at the top level within springapp?
我输入 .settings 吗?还是应该放在 springapp 的顶层?
Thanks for the help.
谢谢您的帮助。
采纳答案by MariuszS
Put into directory WebContent/WEB-INF.
放入目录WebContent/WEB-INF.
Resources placed into WEB-INFfolder are not accessible from web, they are application internal resources.This is good, because your applicationContext.xmlshouldn't be accessible from web.
放置在WEB-INF文件夹中的资源无法从 Web 访问,它们是应用程序内部资源。这很好,因为您applicationContext.xml不应该从网络访问。
Other good options are WebContent/WEB-INF/classesor just src(both are equal).
其他好的选择是WebContent/WEB-INF/classes或只是src(两者都是平等的)。
Files and folders with .contains Eclipse configuration files, they are internal for Eclipse - do not use them.
.包含 Eclipse 配置文件的文件和文件夹,它们是 Eclipse 的内部文件 - 不要使用它们。
回答by AlexR
WEB-INFor its subdirectories. This folder's content is packed directly into the root warfile, so files that are directly under this folder are accessible as resources with path like '/foo.xml'(or in spring notation classpath:/foo.xml
WEB-INF或其子目录。此文件夹的内容直接打包到根war文件中,因此直接在此文件夹下的文件可以作为具有类似路径的资源访问'/foo.xml'(或使用 spring 表示法classpath:/foo.xml
回答by Ralph
I recommend to put it in the src(or src/META-INF) folder and access it via classpath:applicationContext.xml(or classpath:META-INF/applicationContext.xml). (Eclipse will copy this file to WebContent/WEB-INF/classes when it build the war archive.)
我建议将它放在src(或src/META-INF)文件夹中并通过classpath:applicationContext.xml(或classpath:META-INF/applicationContext.xml)访问它。(Eclipse 会在构建 war 存档时将此文件复制到 WebContent/WEB-INF/classes。)
Because:
因为:
- The mayor advantage of
srcoversrc/main/webapp/WEB-INF/WebContent-WEB-INFis, that you can access thesrcfiles even in the tests (viaclasspath:applicationContext.xml) - Do NOT put it
.settingsbecause the content of this directory gets not deployed in the Web App (it is eclipse configuration folder)
srcoversrc/main/webapp/WEB-INF/ 的WebContent-WEB-INF主要优势是,您src甚至可以在测试中访问文件(通过classpath:applicationContext.xml)- 不要放,
.settings因为这个目录的内容没有部署在Web App中(它是eclipse配置文件夹)
Of course when you use maven, then put the file in src\main\resources(or src\main\resources\META-INF), Maven will copy them to the classpath folder while compiling.
当然当你使用maven的时候,然后把文件放在src\main\resources(或src\main\resources\META-INF)中,Maven会在编译的时候把它们复制到classpath文件夹中。
回答by amphibient
It needs to be in the classpath. You can put the original editable instance anywhere (e.g. a configdirectory off the root) but then you will need to have your build management tool (e.g. Antor Maven) copy it into the classpath for the runtime.
它需要在类路径中。您可以将原始的可编辑实例放在任何地方(例如config根目录下的目录),但随后您需要让您的构建管理工具(例如Ant或Maven)将其复制到运行时的类路径中。

