[英]Adding ContextLoaderListener to web.xml in Spring MVC
[英]Spring-MVC web.xml file not using ContextLoaderListener
我了解 ContextLoaderListener 和 DispatcherServlet 的目的是什么。
我不明白的是,如果我沒有在 web.xml 文件中指定 ContextLoaderListener 類,為什么我的 Sprin-MVC 應用程序會啟動。
我希望看到一個錯誤,說缺少上下文或類似的東西。
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
網頁.xml
<display-name>Camel Routes</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
調度程序-servlet.xml
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.crmProject"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://mysql:3306/web_customer_tracker"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<property name="initialPoolSize" value="5"/>
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="20"/>
<property name="maxIdleTime" value="30000"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan" value="com.crmProject.entity"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="myTransactionManager"/>
<mvc:resources location="/resources/" mapping="/resources/**"/>
由於您的上下文定義似乎非常傳統和完整,因此您不需要 ContextLoaderListener。 Spring 可以開箱即用地處理您的所有配置。
如果您有一些 spring 無法處理的奇怪初始化需求,那么 ContextLoaderListener 就會發揮作用。 實現您自己的 ContextLoaderListener 后代並對其進行配置(在 xml 文件中或通過注釋)。 您的實現可以自由地做任何需要做的事情(例如,從非標准源獲取配置以將其注入 spring 上下文)。
首先: DispatcherServlet
加載的上下文和ContextLoaderListener
加載的上下文是兩個不同的上下文。
DispatcherServlet
自行加載其上下文。 默認情況下,它使用模板[servletName]-servlet.xml
查找配置文件。 因此,對於名稱為dispatcher
的DispatcherServlet
它將是dispatcher-servlet.xml
。 但是您可以使用contextConfigLocation
servlet 參數指定您自己的文件名:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:custom-name-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
您的應用程序中可以有多個DispatcherServlet
實例。 每個實例代表單獨的 MVC 應用程序。 每個實例都有自己獨立的上下文。 DispatcherServlet
實例不能訪問彼此的上下文。
ContextLoaderListener
加載所謂的根上下文。 您的應用程序中可以只有一個根上下文(或者根本沒有)。 並且DispatcherServlet
每個實例都可以訪問此上下文。 因此,您可以將此上下文視為父上下文,而DispatcherServlet
的上下文是子上下文。 每次您的應用程序需要獲取 bean 的實例時,它都會首先在子上下文中查找 bean。 如果應用程序在子上下文中找不到 bean,它將在根上下文中查找。
您可以在 Spring 文檔中找到更多詳細信息。 章節上下文層次結構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.