上海建网站开发公,哪些做网站的公司比较好,wordpress的seo标题怎么写,中国临海建设规划局网站解释一#xff1a; 要想很好理解这三个上下文的关系#xff0c;需要先熟悉spring是怎样在web容器中启动起来的。spring的启动过程其实就是其IoC容器的启动过程#xff0c;对于web程序#xff0c;IoC容器启动过程即是建立上下文的过程。 spring的启动过程#xff1a; 首先 要想很好理解这三个上下文的关系需要先熟悉spring是怎样在web容器中启动起来的。spring的启动过程其实就是其IoC容器的启动过程对于web程序IoC容器启动过程即是建立上下文的过程。 spring的启动过程 首先对于一个web应用其部署在web容器中web容器提供其一个全局的上下文环境这个上下文就是ServletContext其为后面的spring IoC容器提供宿主环境 其次在web.xml中会提供有contextLoaderListener。在web容器启动时会触发容器初始化事件此时contextLoaderListener会监听到这个事件其contextInitialized方法会被调用在这个方法中spring会初始化一个启动上下文这个上下文被称为根上下文即WebApplicationContext这是一个接口类确切的说其实际的实现类是XmlWebApplicationContext。这个就是spring的IoC容器其对应的Bean定义的配置由web.xml中的context-param标签指定。在这个IoC容器初始化完毕后spring以WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE为属性Key将其存储到ServletContext中便于获取 再次contextLoaderListener监听器初始化完毕后开始初始化web.xml中配置的Servlet这个servlet可以配置多个以最常见的DispatcherServlet为例这个servlet实际上是一个标准的前端控制器用以转发、匹配、处理每个servlet请求。DispatcherServlet上下文在初始化的时候会建立自己的IoC上下文用以持有spring mvc相关的bean。在建立DispatcherServlet自己的IoC上下文时会利用WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE先从ServletContext中获取之前的根上下文(即WebApplicationContext)作为自己上下文的parent上下文。有了这个parent上下文之后再初始化自己持有的上下文。这个DispatcherServlet初始化自己上下文的工作在其initStrategies方法中可以看到大概的工作就是初始化处理器映射、视图解析等。这个servlet自己持有的上下文默认实现类也是mlWebApplicationContext。初始化完毕后spring以与servlet的名字相关(此处不是简单的以servlet名为Key而是通过一些转换具体可自行查看源码)的属性为属性Key也将其存到ServletContext中以便后续使用。这样每个servlet就持有自己的上下文即拥有自己独立的bean空间同时各个servlet共享相同的bean即根上下文(第2步中初始化的上下文)定义的那些bean。 解释二 在Web容器比如Tomcat中配置Spring时你可能已经司空见惯于web.xml文件中的以下配置代码 [plain] view plaincopy span stylefont-family:SimSun;font-size:14px;context-param param-namecontextConfigLocation/param-name param-value/WEB-INF/applicationContext.xml/param-value /context-param listener listener-class org.springframework.web.context.ContextLoaderListener /listener-class /listener servlet servlet-namemvc-dispatcher/servlet-name servlet-class org.springframework.web.servlet.DispatcherServlet /servlet-class load-on-startup1/load-on-startup /servlet servlet-mapping servlet-namemvc-dispatcher/servlet-name url-pattern//url-pattern /servlet-mapping/span 以上配置首先会在ContextLoaderListener中通过context-param中的applicationContext.xml创建一个ApplicationContext再将这个ApplicationContext塞到ServletContext里面通过ServletContext的setAttribute方法达到此目的在ContextLoaderListener的源代码中我们可以看到这样的代码 [java] view plaincopy span stylefont-family:SimSun;font-size:14px;servletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);/span 以上由ContextLoaderListener创建的ApplicationContext是共享于整个Web应用程序的而你可能早已经知道DispatcherServlet会维持一个自己的ApplicationContext默认会读取/WEB-INFO/dispatcherServletName-servlet.xml文件而我么也可以重新配置 [plain] view plaincopy span stylefont-family:SimSun;font-size:14px;servlet servlet-name customConfiguredDispacherServlet /servlet-name servlet-class org.springframework.web.servlet.DispatcherServlet /servlet-class init-param param-name contextConfigLocation /param-name param-value /WEB-INF/dispacherServletContext.xml /param-value /init-param load-on-startup1/load-on-startup /servlet/span 问题是以上两个ApplicationContext的关系是什么它们的作用作用范围分别是什么它们的用途分别是什么 ContextLoaderListener中创建ApplicationContext主要用于整个Web应用程序需要共享的一些组件比如DAO数据库的ConnectionFactory等。而由DispatcherServlet创建的ApplicationContext主要用于和该Servlet相关的一些组件比如Controller、ViewResovler等。 对于作用范围而言在DispatcherServlet中可以引用由ContextLoaderListener所创建的ApplicationContext而反过来不行。 在Spring的具体实现上这两个ApplicationContext都是通过ServletContext的setAttribute方法放到ServletContext中的。但是ContextLoaderListener会先于DispatcherServlet创建ApplicationContextDispatcherServlet在创建ApplicationContext时会先找到由ContextLoaderListener所创建的ApplicationContext再将后者的ApplicationContext作为参数传给DispatcherServlet的ApplicationContext的setParent()方法在Spring源代码中你可以在FrameServlet.java中找到如下代码 wac.setParent(parent);其中wac即为由DisptcherServlet创建的ApplicationContext而parent则为有ContextLoaderListener创建的ApplicationContext。此后框架又会调用ServletContext的setAttribute()方法将wac加入到ServletContext中。 当Spring在执行ApplicationContext的getBean时如果在自己context中找不到对应的bean则会在父ApplicationContext中去找。这也解释了为什么我们可以在DispatcherServlet中获取到由ContextLoaderListener对应的ApplicationContext中的bean。 Spring API中的解释 public interface WebApplicationContextextends ApplicationContextInterface to provide configuration for a web application. This is read-only while the application is running, but may be reloaded if the implementation supports this. This interface adds a getServletContext() method to the generic ApplicationContext interface, and defines a well-known application attribute name that the root context must be bound to in the bootstrap process. Like generic application contexts, web application contexts are hierarchical. There is a single root context per application, while each servlet in the application (including a dispatcher servlet in the MVC framework) has its own child context. In addition to standard application context lifecycle capabilities, WebApplicationContext implementations need to detect ServletContextAware beans and invoke the setServletContext method accordingly. 转载于:https://www.cnblogs.com/jiligalaer/p/4443481.html