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

查看:184
本文介绍了了解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.

我正在使用STS Eclipse插件.我创建了一个 使用插件的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,我知道调度程序servlets的工作是创建一个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 Application上下文.这些中的每一个都可以包含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天全站免登陆