web.xml,beans.xml,applicationContext.xml等之间的区别 [英] The difference between web.xml, beans.xml, applicationContext.xml, etc

查看:167
本文介绍了web.xml,beans.xml,applicationContext.xml等之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Spring MVC一段时间,现在在Glassfish服务器上运行的Netbeans中创建我的项目。虽然一切工作正常但我觉得我对每个XML文件中的内容都缺乏理解 - 在某些情况下,这导致我只是在每个文件中一个接一个地尝试一大块XML直到它工作。



我在谷歌上找不到任何明确的描述,我已经尝试了几次。



<我会详细说明我目前的理解,然后如果有人可以跟进更详细的解释,或者让我知道我错在哪里会非常感激。



web.xml



这似乎是通过告诉它用于处理查询的类来配置servlet容器。混淆似乎是虽然在这里配置Spring不起作用 - 你需要在这里安装一些配置来安装Spring即

 <滤光器> 
< 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>

您可以在 web.xml 中添加它以使Spring Security正常工作 - 但是然后你实际上在另一个文件中配置Spring Security。



dispatcher-servlet



这似乎类似于 web.xml ,因为它是关于配置servlet容器以启用Spring - 但在我的项目中它大部分是空的,只包含一个 viewResolver 。这里应该介绍什么以及它与 web.xml 的区别?



beans.xml



目前,除了xml根标签< beans> 以及一些名称空间/模式定义之外,此文件为空我所有的项目。实际上是否需要任何东西?



根目录中 bean-discovery-mode =annotated> 标记它为空的原因?



applicationContext



这似乎是在哪里所有实际的Spring配置如< mvc:annotation-driven /> < context:component-scan /> 等。



您还可以将此配置拆分为多个文件并使用< import /> 将这些文件链接到应用程序上下文中。



glassfish-web



<我大部分都忽略了这个文件,我有什么理由不这样做吗?



问题



所以真正的问题是:




  • 我错过了什么?

  • 为什么单独的 beans.xml 似乎无能为力?它是引入注释驱动之前的遗产吗?

  • 为什么同时存在 dispatcher-servlet.xml web.xml 它们之间有什么区别?

  • glassfish-web.xml 与这两者有什么不同?

  • 如何我能告诉xml的一个片段是否应该进入这些文件中的哪一个而不是在它们全部尝试之前直到它工作? (到目前为止我开发的粗略经验是在 applicationContext.xml 中的spring配置,在 web.xml 中安装spring组件,忽略其他文件!)



提前致谢,



Tim

解决方案

web.xml 是一个应驻留在所有J2EE Web应用程序中的文件。其规范由J2EE规范定义。在这里,您可以配置应用的一般行为。例如servlet,过滤器,安全策略等。



dispatcher-servlet 是Spring MVC框架中的一个特殊servlet。您必须在 web.xml 中定义其映射,才能在您的网络应用中启用Spring。



beans.xml 是用于配置某些CDI设置的文件。例如 bean-discovery-mode =annotated表示只有带有CDI范围注释的类才会被视为CDI托管bean。



applicationContext.xml 这里你实际上是对的。它是主Spring配置文件的通用名称。你可以在这里设置很多东西,比如创建和连接一些Spring bean。



glassfish-web.xml 通常是 web.xml ,用于GlassFish服务器。并不总是需要它。如果想要专门为GlassFish服务器配置一些设置,则需要它。例如,如果您在 web.xml 中配置安全性部分,则必须将用户角色从 web.xml 映射到GlassFish领域角色。



希望有所帮助。


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.

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

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.

<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.xml to get Spring Security working - but then you actually configure Spring Security in another file.

dispatcher-servlet

This seems to be similar to web.xml in 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?

beans.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?

Is bean-discovery-mode="annotated"> in the root tag the reason it is empty?

applicationContext

This seems to be where all the actual Spring configuration goes such as <mvc:annotation-driven />, <context:component-scan /> etc.

You can also split this configuration up into multiple files and use <import /> to link those files into the application context.

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.xml that seems to do nothing? Is it a legacy from before annotation driven was brought in?
  • Why are there both dispatcher-servlet.xml and web.xml and what is the difference between them?
  • How is glassfish-web.xml different 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"!)

Thanks in advance,

Tim

解决方案

web.xml is 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.

dispatcher-servlet is a special servlet in Spring MVC framework. You must define its mapping in your web.xml to enable Spring in your web app.

beans.xml is 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.

applicationContext.xml here 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.

glassfish-web.xml is generally an extension to web.xml for 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.xml you have to map user roles from web.xml to GlassFish realm roles.

Hope it helps.

这篇关于web.xml,beans.xml,applicationContext.xml等之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆