天天看點

淺談BeanNameUrlHandlerMapping

spring為我們提供了多種處理器映射的支援,比如org.springframework.web.servlet.handler.SimpleUrlHandlerMapping、

org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping、org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping,等等。

我用SimpleUrlHandlerMapping和DefaultAnnotationHandlerMapping比較多,其它的基本沒用過。

最近在項目中使用了BeanNameUrlHandlerMapping,發現有這樣一個現象:

如果沒有明确聲明任何處理器映射,spring會預設使用BeanNameUrlHandlerMapping,但如果明确聲明了其它的處理器映射,則需要将BeanNameUrlHandlerMapping明确聲明出來,而且在每個包含被映射的bean的配置檔案中都要加入BeanNameUrlHandlerMapping,否則會抛異常:

WARN - No mapping found for HTTP request with URI [/BOSS_SUPPORT/service/httpService] in DispatcherServlet with name 'backend'

2010-01-08 11:14:57 [backend]:253 ERROR - Servlet.service() for servlet backend threw exception

java.io.IOException: Did not receive successful HTTP response: status code = 404, status message = [Not Found]

從異常看,報告沒有找到相應的映射位址,這個現象很是奇怪,也許是我沒有搞清楚,不過在網上也沒有找到相關的說明,希望高手指點。

相關配置檔案如下:

主配置檔案a.xml:

<!-- 定義注解URL映射處理器 -->

<bean id="urlMapping"

class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

<property name="interceptors" ref="interceptors" />

<property name="order" value="1"></property>

</bean>

<bean id="beanNameUrlHandlingMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">

<property name="order" value="2"></property>

包含映射的一個配置檔案b.xml:

<bean id="httpService" name="/httpService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">

<property name="service">

<ref bean="ucService" />

</property>

<property name="serviceInterface" value="com.netqin.baike.service.UcService"/>

以上兩個檔案都在伺服器啟動時加載,雖然a.xml中已經聲明了BeanNameUrlHandlerMapping,但如果b.xml中沒有聲明BeanNameUrlHandlerMapping,系統就會抛異常。