Java web.xml、beans.xml、applicationContext.xml等的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20539842/
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
The difference between web.xml, beans.xml, applicationContext.xml, etc
提问by Tim B
I've been working with Spring MVC for a while now creating my projects in Netbeans running on Glassfish servers. While everything is working fine I feel like I lack understanding on just what should be in each of the XML files - and in several cases this has led to me just trying a chunk of XML in each file one after another until it works.
我一直在使用 Spring MVC 一段时间,现在在运行在 Glassfish 服务器上的 Netbeans 中创建我的项目。虽然一切正常,但我觉得我对每个 XML 文件中应该包含的内容缺乏了解 - 在某些情况下,这导致我只是一个接一个地尝试每个文件中的一大块 XML,直到它起作用为止。
I've not been able to find any clear description of this on Google and I've tried a few times.
我在谷歌上找不到任何明确的描述,我已经尝试了几次。
I'll detail my current understanding here and then if anyone can follow up with a more detailed explanation or let me know where I'm mistaken that would be much appreciated.
我将在这里详细说明我目前的理解,然后如果有人可以跟进更详细的解释或让我知道我错在哪里,将不胜感激。
web.xml
网页.xml
This seems to be configuring the servlet container by telling it what classes to use for handling queries. The confusion seems to be that while configuring Spring in here does not work - you need to put some configuration in here to install Spring i.e.
这似乎是通过告诉它使用哪些类来处理查询来配置 servlet 容器。混乱似乎是,虽然在此处配置 Spring 不起作用 - 您需要在此处放置一些配置以安装 Spring 即
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
You add that in web.xmlto get Spring Security working - but then you actually configure Spring Security in another file.
您在web.xml 中添加它以使 Spring Security 工作 - 但实际上您在另一个文件中配置了 Spring Security。
dispatcher-servlet
调度程序小服务程序
This seems to be similar to web.xmlin that it is about configuring the servlet container to enabling Spring - but in my projects it is mostly empty and just contains a single viewResolver
. What should go in here and how does it differ from web.xml?
这似乎与web.xml类似,因为它是关于配置 servlet 容器以启用 Spring - 但在我的项目中它大部分是空的,只包含一个viewResolver
. 这里应该放什么,它与web.xml 有什么不同?
beans.xml
bean.xml
At the moment this file is empty apart from an xml root tag <beans>
and a few namespace/schema definitions in all my projects. Is it actually needed for anything?
目前,除了<beans>
我所有项目中的 xml 根标记和一些命名空间/模式定义之外,该文件是空的。它真的需要任何东西吗?
Is bean-discovery-mode="annotated">
in the root tag the reason it is empty?
是bean-discovery-mode="annotated">
在根标签是空的原因是什么?
applicationContext
应用上下文
This seems to be where all the actual Spring configuration goes such as <mvc:annotation-driven />
, <context:component-scan />
etc.
这似乎是所有实际 Spring 配置的所在,例如<mvc:annotation-driven />
,<context:component-scan />
等等。
You can also split this configuration up into multiple files and use <import />
to link those files into the application context.
您还可以将此配置拆分为多个文件,并用于<import />
将这些文件链接到应用程序上下文中。
glassfish-web
glassfish-web
I've mostly been ignoring this file, is there any reason I shouldn't?
我大部分时间都忽略了这个文件,有什么理由不应该吗?
The questions
问题
So really the questions are:
所以真正的问题是:
- What have I missed from the above?
- Why is there a separate beans.xmlthat seems to do nothing? Is it a legacy from before annotation driven was brought in?
- Why are there both dispatcher-servlet.xmland web.xmland what is the difference between them?
- How is glassfish-web.xmldifferent from those two?
- How do I tell whether a fragment of xml should go into which of these files without trying it in them all until it works? (The rough rule of thumb I've developed so far is "spring config in applicationContext.xml, installing spring components in web.xml, ignore the other files"!)
- 我从上面遗漏了什么?
- 为什么有一个单独的beans.xml似乎什么都不做?它是引入注释驱动之前的遗产吗?
- 为什么同时有dispatcher-servlet.xml和web.xml,它们之间有什么区别?
- 如何与GlassFish web.xml中来自这两个有什么不同?
- 我如何判断一个 xml 片段是否应该进入这些文件中的哪个文件而不在它们中全部尝试直到它起作用?(到目前为止,我开发的粗略经验法则是“ applicationContext.xml 中的spring 配置,在web.xml 中安装 spring 组件,忽略其他文件”!)
Thanks in advance,
提前致谢,
Tim
蒂姆
采纳答案by Flying Dumpling
web.xmlis a file that should reside in all J2EE web application. Its specification is defined by J2EE spec. Here you configure a general behaviour of your app. For example servlets, filters, security policy etc.
web.xml是应该驻留在所有 J2EE Web 应用程序中的文件。它的规范是由 J2EE 规范定义的。在这里,您可以配置应用程序的一般行为。例如 servlet、过滤器、安全策略等。
dispatcher-servletis a special servlet in Spring MVC framework. You must define its mapping in your web.xmlto enable Spring in your web app.
dispatcher-servlet是 Spring MVC 框架中的一个特殊的 servlet。您必须在web.xml 中定义其映射才能在您的 Web 应用程序中启用 Spring。
beans.xmlis a file that is use for configure some CDI settings. For example bean-discovery-mode="annotated"
means that only classes annoteded with CDI scope annotation will be consider as CDI managed beans.
beans.xml是用于配置一些 CDI 设置的文件。例如bean-discovery-mode="annotated"
意味着只有用 CDI 范围注释注释的类才会被视为 CDI 托管 bean。
applicationContext.xmlhere you are actually right. It is the common name of the main Spring configuration file. You can set many things here like for example create and wire some Spring beans.
applicationContext.xml在这里你其实是对的。它是主 Spring 配置文件的通用名称。您可以在这里设置很多东西,例如创建和连接一些 Spring bean。
glassfish-web.xmlis generally an extension to web.xmlfor GlassFish server. It is not always needed. You need it if want to configure some settings specially for GlassFish server. For example if you configure the security part in your web.xmlyou have to map user roles from web.xmlto GlassFish realm roles.
glassfish-web.xml通常是GlassFish 服务器的web.xml的扩展。它并不总是需要的。如果要专门为 GlassFish 服务器配置一些设置,则需要它。例如,如果您在web.xml 中配置安全部分,则必须将用户角色从web.xml映射到 GlassFish 领域角色。
Hope it helps.
希望能帮助到你。