理解 Spring MVC 中的上下文 [英] Understanding contexts in Spring MVC

查看:37
本文介绍了理解 Spring MVC 中的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 spring 的新手,我正在创建一个简单的 Web 应用程序.我一直在阅读 Spring MVC 中的上下文.

I am new to spring and I am creating a simple web application. I have been reading about contexts in Spring MVC.

我正在为 Eclipse 使用 STS 插件.我创建了一个使用插件的 Spring MVC 项目.

I am using STS plugin for eclipse. I created a Spring MVC project using the plugin.

现在项目中有三个xml文件,web.xml、root-context.xml和servlet-context.xml.这些是 STS 为我创建的.

Now I have three xml documents in the project, web.xml, root-context.xml and servlet-context.xml. These were created by STS for me.

  1. 在 web.xml 中,调度程序 servlet 指向 servlet-context.xml,我理解调度程序 servlet 的工作是创建一个 web 应用程序上下文,它知道如何解析视图并且是控制器 bean 存在的地方.我的理解正确吗?如果是这样,此上下文还完成了哪些其他工作?

  1. In web.xml, dispatcher servlet is pointed towards servlet-context.xml and I understand the dispatcher servlets job is to create a web application context which knows how to resolve views and is a place for controller beans to exist. Is my understanding correct? If so, what other job is accomplished by this context?

现在,有一个名为 root-context.xml 的文件,它对我的​​项目默认包进行了组件扫描.我的理解是此上下文需要具有许多 servlet 可能使用的全局 bean.我的理解正确吗?这还有什么作用?使用这个文件创建了什么样的上下文?

Now, there is a file called root-context.xml which has a component scan on my projects default package. My understanding is this context needs to have global beans which many servlets might use. Is my understanding correct? What else does this do? What kind of context is created using this file?

现在,我在项目中更进一步,我有几个 *-context.xml 文件(dao-context.xml、security-context.xml 等),它们使用 contextLoaderListner(在 web.xml 中)加载.这是一个好主意吗?或者一切都应该进入servlet-context.xml?我认为拥有不同的上下文是个好主意,因为它提供了关注点分离.评论?另外,从这些 *-context.xml 文件中创建了什么样的上下文?这些文件的正确文件夹位置是什么?

Now, I am further along in the project and I have several *-context.xml files (dao-context.xml, security-context.xml etc) which are loaded using contextLoaderListner (in web.xml). Is this a good idea? Or should everything go into servlet-context.xml? I think it's a good idea to have different contexts as it provides separation of concern. Comments? Also, what kind of context is created from these *-context.xml files? What is the proper folder location for these files?

Web.xml 用于 servlet 容器,如 tomcat 等,项目中的所有其他 xml 文件用于 spring 容器.那是对的吗?所有这些文件都是分开的以提供关注点分离吗?

Web.xml is for the servlet container like tomcat etc and all other xml files in the project are for the spring container. Is that correct? All these files are separated to provide separation of concern?

当前场景中存在多少个应用上下文和Web应用上下文?

How many application contexts and web application contexts exists in the current scenario?

为什么有人需要多个调度程序 servlet?

Why would anyone need more than one dispatcher servlet?

为什么有人需要多个应用程序上下文?

Why would anyone need more than one application context?

想法?评论?更正?最佳实践?

Thoughts? Comments? Corrections? Best practices?

推荐答案

这种设计背后的整个想法是在典型的 Web 应用程序中处理不同的架构层,并为跨上下文的 bean 提供继承/覆盖机制.Spring 中的每种类型的上下文都与不同的架构层相关,例如 Web 层、服务层等.

The whole idea behind this design is to handle different architectural layers in a typical web application and provide for inheritance / override mechanism for beans across contexts. Each type of context in Spring is related to different architectural layer for e.g, web layer, service layer etc.

基于 Spring 的 Web 应用程序可以配置多个调度程序 servlet(尽管在大多数情况下它是单个 servlet - 但调度程序 serlvet 仍然是一个 servlet,并且可以在 web.xml 中配置多个).这些可以配置为处理不同的 url 模式.很明显,每个都是不同的 servlet,因此可以有不同的 Spring Web 应用程序上下文.其中每一个都可以包含 Spring web 层的不同配置,如控制器、拦截器、视图解析器、区域设置解析器等,因为它们通常属于应用程序的 web 层.所有这些配置和 bean 对每个调度程序 servlet 都是私有的,因此它们彼此不可见.因此,拥有单独的 Spring Web 应用程序上下文对于启用此隐私是有意义的.然而,还有其他 bean 被设计为共享,因此属于根上下文.所以所有可共享的东西都属于根上下文,对于这个 Web 应用程序来说,它可以被认为是全局的.

A Spring based web application can have multiple dispatcher servlet configured (although in majority of cases its a single servlet - but dispatcher serlvet is a servlet nonetheless and there could be multiple configured in web.xml). These can be configured to handle different url patterns. So obviously each is a different servlet and hence can have different Spring web Application context. Each of these can contain different configurations for Spring web layer like controllers,interceptors,view resolvers,locale resolvers etc. as these typically belong to the web layer of an application. All these configurations and beans are private to each dispatcher servlet so they are not visible to each other. Hence having a seperate spring web application context makes sense to enable this privacy. However there are other beans which are designed to be shared hence belong to a root context. So all the shareable things belong to the root context and it can be considered global for this web application.

每个调度程序 servlet 都继承在根上下文中定义的所有 bean.然而,需要注意的重要一点是共享 bean 可以被相应的调度程序 servlet 特定 bean 覆盖.因此,在 Web 应用程序中,根上下文可以被视为可以被继承但可以被覆盖的东西.

Each dispatcher servlet inherits all the beans defined in the root context. However the important point to note is that the shared beans can be overridden by respective dispatcher servlet specific beans. So in web applications root context can be viewed as something which is inherited but can be overridden.

这篇关于理解 Spring MVC 中的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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