Java 理解SpringMVC应用程序上下文
我试图理解基本的Spring MVC内容 以web.xml为例:Java 理解SpringMVC应用程序上下文,java,spring,spring-mvc,Java,Spring,Spring Mvc,我试图理解基本的Spring MVC内容 以web.xml为例: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xm
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
也考虑下面的图表:在
疑问: DispatcherServlet和ContextLoaderListener创建的实例的确切类型是什么?出现混淆的原因是,一些在线文章说ContextLoaderListener创建ApplicationContext实例,DispatcherServlet创建WebApplicationContext实例。但是,在查看了它们的源代码之后,我感觉既创建了WebApplicationContext类型的实例,又因为它的子类型ApplicationContext,一些文章说ContextLoaderListener创建了ApplicationContext实例。是否也只有ApplicationContext提供的由ContextLoaderListener创建的实例的功能被使用,因此文章说ContextLoaderListener创建ApplicationContext,而不是WebApplicationContext ` 它是否像ContextLoaderListener创建的WebApplicationContext是根WebApplicationContext,只是为了区别于DispatcherServlet创建的WebApplicationContext 通过阅读,我了解到DispatcherServlet是从[servlet name]-servlet.xml文件加载的,该文件应该加载web层组件。另一方面,ContextLoaderListener加载中间层组件。假设web.xml中只能有一个ContextLoaderListener,对吗?应该只有一组中间层组件,而由于可以有多个DispatcherServlet,因此可以有多组web层组件。是这样吗?如果是这样,那么为什么上图显示了中间层组件的多个WebApplicationContext和web层组件的单个WebApplicationContext WebApplicationContext和ApplicationContext都是接口,所以当文档中说创建ApplicationContext实例时,意味着它创建了实现ApplicationContext的类的对象实例。该类是否也实现了WebApplicationContext并不重要。可能吧。可能不会。不会使创建ApplicationContext实例的语句无效 什么通常在线文章称为根 不,它是javadoc本身所指的根: 执行根应用程序上下文的实际初始化工作 为什么上图显示了中间层组件的多个WebApplicationContext和web层组件的单个WebApplicationContext 它只显示web层组件的单个WebApplicationContext,因为该图是针对单个DispatcherServlet的 仅仅因为ContextLoaderListener只创建一个根上下文,并不意味着不能通过其他方式创建多个上下文。这张图表准确地显示了什么是可能的1 WebApplicationContext和ApplicationContext都是接口,所以当文档中说创建ApplicationContext实例时,意味着它创建了实现ApplicationContext的类的对象实例。该类是否也实现了WebApplicationContext并不重要。可能吧。可能不会。不会使它创建ApplicationContext instance.ohh的语句无效,这太糟糕了……我怎么没有注意到这一点。当我们用web.xml创建普通的Spring MVC应用程序时,它会创建什么?另外,你能回答另外两个问题吗?或者它们是毫无意义的?谢谢,这消除了我头脑中相当多的困惑。但是,仅仅因为ContextLoaderListener只创建一个根上下文而产生的一个新的小混乱并不意味着不能通过其他方式创建多个上下文。你能详细说明/举个小例子说明其他的方法吗?@anir例如,写下你自己的听众,创建你想要的任意多的上下文,任意分割。