java Spring 应用程序上下文 XML 的命名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1334414/
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
Naming convention for Spring application context XML
提问by 0sumgain
I am deciding on a naming convention for my Spring application context files. I've come across this blogand a couple of tutorials suggesting applicationContext-<name>.xmlis the way to go. I'm generally not a find of mixing camel case with dashes/underscores. What are some other naming conventions have you seen?
我正在决定我的 Spring 应用程序上下文文件的命名约定。我遇到了这个博客,有几个教程建议applicationContext-<name>.xml是要走的路。我通常不会将驼峰式大小写与破折号/下划线混合使用。您还见过哪些其他命名约定?
Edit: I'm also thinking of nesting context files inside the package that pertains to them. For example, my classes/interfaces that relates to ordering would go in context file com/mycompany/order/spring-context.xml. I would have a top-level applicationContext.xmlthat pulls everything together. Suggestions?
编辑:我也在考虑在与它们相关的包中嵌套上下文文件。例如,我的与排序相关的类/接口将放在上下文文件中com/mycompany/order/spring-context.xml。我会有一个applicationContext.xml将所有东西整合在一起的顶级。建议?
采纳答案by Michael Wiles
If there were a convention, I would like it to be:
如果有一个约定,我希望它是:
- files stored in packages (not in default package) to obviate potential naming conflicts and also means I don't have to include the app name in them, it's defined by the package.
- files named all lower case with a '-' to separate the names
- 文件存储在包中(不是在默认包中)以避免潜在的命名冲突,也意味着我不必在其中包含应用程序名称,它是由包定义的。
- 文件名全部小写,用“-”分隔名称
I tend to prefix my spring config files with "spring" makes it more obvious what they are used for, but this is not necessarily mandatory.
我倾向于在我的 spring 配置文件前面加上“spring”,这使得它们的用途更加明显,但这不一定是强制性的。
But let me say this would work for the way that I've dealt with my spring files, may not work for all contexts.
但是让我说这适用于我处理 spring 文件的方式,可能不适用于所有上下文。
IMHO applicationContext-<name>.xml is a little verbose (long) and I like all lowercase as it's differentiates them from my java source and (I think) makes them easier to read.
恕我直言 applicationContext-<name>.xml 有点冗长(长),我喜欢所有小写字母,因为它与我的 java 源代码不同,并且(我认为)使它们更易于阅读。
回答by matt b
For a Web MVC project, I tend to break down the various context files along lines of responsibility:
对于 Web MVC 项目,我倾向于按照职责分解各种上下文文件:
appname-dao.xmlappname-servlet.xmlappname-services.xmlappname-security.xml
appname-dao.xmlappname-servlet.xmlappname-services.xmlappname-security.xml
回答by Marcel St?r
I very much support the applicationContext-<name>.xmlconvention.
我非常支持applicationContext-<name>.xml公约。
In general <name>refers to the Maven module name in all our projects. So, each module that requires a Spring configuration has its own applicationContext-<name>.xmlfile. The "execution module" i.e. the one module that represents the sort of the entry point into the application (WAR, EAR, etc.) has a single applicationContext.xmlthat only imports all the applicationContext-<name>.xmlfiles.
一般<name>指我们所有项目中的Maven模块名称。因此,每个需要 Spring 配置的模块都有自己的applicationContext-<name>.xml文件。“执行模块”,即表示应用程序入口点类型(WAR、EAR 等)的一个模块,有一个applicationContext.xml只导入所有applicationContext-<name>.xml文件的模块。
We use this convention company-wide strictly in all our Maven/Spring projects and it's proved to be extremely simple, clear and efficient.
我们在公司范围内的所有 Maven/Spring 项目中都严格使用这个约定,事实证明它非常简单、清晰和高效。
回答by Robin
Either camel case or '-' separated would work fine, as long as you are consistent. I know in our case though we don't put the context files in the same directory as the code, unless the code itself is Spring aware, which in our case it isn't.
只要您保持一致,驼峰式大小写或“-”分隔都可以正常工作。我知道在我们的例子中,虽然我们没有将上下文文件放在与代码相同的目录中,除非代码本身是 Spring 感知的,而在我们的例子中它不是。
We have two cases for this, where we use maven 2, the context file goes in a resource/spring directory, where resource is a sibling of the java source directory. Where maven 1 is used, we simply create a root spring package and put the context there. Both these cases assume 'regular' java code. In the case of Wars, EJB's and OSGi bundles, the files typically reside in the meta-inf directory.
我们有两种情况,我们使用 maven 2,上下文文件位于 resource/spring 目录中,其中 resource 是 java 源目录的同级。在使用 maven 1 的地方,我们只需创建一个根 spring 包并将上下文放在那里。这两种情况都假定“常规”Java 代码。对于 Wars、EJB 和 OSGi 包,文件通常位于 meta-inf 目录中。
Also, we don't use a top-level application context to 'pull' everything together. We simply create a context with multiple context files. We find this much simpler for testing in different ways, unit tests with mock objects, integration tests with no server and full integration tests deployed to a server. In all these scenarios, simply reconfigure how the context is created instead of having a 'master' context for each scenario.
此外,我们不使用顶级应用程序上下文将所有内容“拉”在一起。我们只需创建一个包含多个上下文文件的上下文。我们发现这对于以不同方式进行测试要简单得多,使用模拟对象进行单元测试,没有服务器的集成测试以及部署到服务器的完整集成测试。在所有这些场景中,只需重新配置上下文的创建方式,而不是为每个场景设置一个“主”上下文。

