Ok我明白通過DispatcherServlet配置的任何Web應用程序上下文都會繼承已在根WebApplicationContext中定義的所有Bean。然而,我在我的web.xml在我的根ApplicationContext中配置一些攔截在根ApplicationContext中配置的Spring攔截器無法從子WebApplicationContext訪問
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="2"/>
<property name="interceptors">
<list>
<ref bean="statisticsInterceptor"/>
<ref bean="sessionDiagnosticsInterceptor"/>
...
然後,我有這樣的事情:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext*.xml
</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>dispatcher-api</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appOtherContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher-api</servlet-name>
<url-pattern>/example/apiOther/*</url-pattern>
</servlet-mapping>
所以我的問題是,每次我去任何網址其中包含「/example/apiOther/」,攔截器將無法到達/運行。我的問題是:爲什麼? handlerMapping的範圍是什麼?我認爲,因爲它在根應用程序上下文中,它應該適用於所有子上下文。我一直在做一些研究,我認爲HandlerMappings僅限於它的上下文,即使它是根上下文。是對的嗎?