root和web applicationContext spring mvc之间的区别 [英] Difference between root and web applicationContext spring mvc

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

问题描述

我是新的春天mvc,并试图了解一些基础知识。
有些事情对我来说很清楚,但我仍然对春季基础知识有所怀疑。

I am new spring mvc and trying to understand some basics. Some of the things are clear to me but still I have some doubts regarding spring basics.

首先让我把清楚的东西放在我身上。 (如果我错了请反对我)。

First of all let me put things that are clear to me.(If I am wrong please object me).

spring mvc中有两种xml文件:rootApplicationContext.xml和webApplicationContext.xml

There are two kinds of xml files in spring mvc : rootApplicationContext.xml and webApplicationContext.xml

web.xml中的context-param标记定义了rootApplicationContext.xml
和具有DispatcherServlet(充当表单控制器)的servlet标记定义了webApplicationContext.xml参数,
和bean rootApplicationContext可在所有webApplicationContext.xml文件中访问

context-param tag in web.xml defines rootApplicationContext.xml and servlet tag having DispatcherServlet (acting as a form controller) defines the webApplicationContext.xml parameters, and beans of rootApplicationContext are eccessible in all webApplicationContext.xml files

hibernate和DAO配置等所有应用程序所需的功能都是用rootApplicationContext编写的

features that are required in all parts of application like hibernate and DAO configurations are written in rootApplicationContext

现在我的问题是:

(1)我认为在应用程序加载时加载了rootApplicationContext,并在加载DispatcherServlet类时加载了webApplicationContext。
那么什么时候加载DispatcherServlet类?

(1)I think rootApplicationContext is loaded at application loading time and webApplicationContext is loaded when DispatcherServlet class is loaded. So when does the DispatcherServlet class is loaded ?

(2)当创建控制器动作的对象时,在应用程序加载时或在调用时?

(2)when the objects of controller-action are created, at application loading time or at the calling time ?

(3)为控制器动作,单例或原型创建了哪种类型的对象?

(3)Which type of objects created for controller-action , singleton or prototype ?

(3) )哪个xml应该包含过滤器,拦截器,AOP类的bean定义以及为什么?

(3)Which xml should contain the bean definations of filter , interceptor , AOP classes and why ?

对于我的一些疑问是参考弹簧文档,但仍然有一些疑问。

For some of my doubts are reffered spring documentation but the still I have some doubts.

谢谢

推荐答案

你说的都是正确的,那就是一个非常好的开始。
所有servlet都是在容器启动时加载的(如果你在web.xml中指定了load-on-startup值),或者每当容器启动时都会加载,但是通常不需要担心。很明显,当需要时,单身人士会在开始时加载原型。大多数物体都是单身,因此应该是无国籍的。只有在您知道的情况下维持状态才是一个好主意。您可以在将要使用的上下文中定义bean。拦截器仅在Web上下文中使用,因此您可以在其中放置它们。相同的逻辑适用于其他所有内容,但要注意在Spring MVC之外使用的与Web相关的类,比如您自己的过滤器和类似的类。只有实际命中DispatcherServlet的请求才会知道Spring MVC的内容。

You are right in all points, and that's a very good start. All servlets are either loaded on container startup (if you specify a load-on-startup value in web.xml) or whenever the container feels like it otherwise, but it's usually not something you have to worry about. Singletons are loaded on the start, prototypes, obviously, when needed. Most objects are singletons, and should thus be stateless. It is a good idea to maintain state only where you know you can. You define beans in the context they will be used. Interceptors are used in the web context only, so that's where you put them. Same logic applies to everything else, but be mindful of web-related classes used outside of Spring MVC, like your own filters and similar. Only requests that have actually hit DispatcherServlet will be aware of Spring MVC stuff.

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

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