2010-09-12 26 views
15

到目前爲止,<mvc:annotation-driven />給我帶來了很多麻煩,所以我想擺脫它。雖然spring framework docs clearly say what it is supposed to be doing,實際上summar <mvc:annotation-driven />缺乏列表的標籤。如何擺脫<mvc:annotation-driven />?

所以我只能和消除<mvc:annotation-driven />現在得到的錯誤

WARN osweb.servlet.PageNotFound - 沒有映射 發現HTTP請求與 URI [/ web應用/學員]的DispatcherServlet與名 「workoutsensor」

對於應該由控制器類(在這種情況下:./trainees)來解決所有的URL。任何建議,我可以閱讀更多關於<mvc:annotation-driven />?我非常想知道<mvc:annotation-driven />究竟代表什麼標籤。

回答

26

您可以使用BeanPostProcessor來自定義由<mvc:annotation-driven />定義的每個bean。 javadoc現在詳述了標籤註冊的所有bean。

如果你真的想擺脫它,你可以看看org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser

源代碼,你可以看到它是定義該豆。我做這個「運動」(不是所有的人,但對於那些我需要的),所以這裏是他們:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /> 
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" /> 

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
     <property name="webBindingInitializer"> 
      <bean class="com.yourpackage.web.util.CommonWebBindingInitializer" /> 
     </property> 
     <property name="messageConverters"> 
      <list> 
       <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> 
       <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" /> 
       <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> 
       <bean class="org.springframework.http.converter.feed.AtomFeedHttpMessageConverter" /> 
       <bean class="org.springframework.http.converter.feed.RssChannelHttpMessageConverter" /> 
       <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> 
       <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /> 
       <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" /> 
       <!-- bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" /--> 
      </list> 
     </property> 
    </bean> 
<bean id="handlerMapping" 
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 

現在,你上面看到CommonWebBindingInitializer。你必須創建這個類,以便使用轉換和驗證:

public class CommonWebBindingInitializer implements WebBindingInitializer { 

    @Autowired 
    private Validator validator; 

    @Autowired 
    private ConversionService conversionService; 

    @Override 
    public void initBinder(WebDataBinder binder, WebRequest request) { 
     binder.setValidator(validator); 
     binder.setConversionService(conversionService); 
    } 

} 

這對我來說工作到目前爲止。隨意報告任何問題。

+0

感謝Bozho,那只是我需要的輸入。我很確定我會遇到更多的配置問題,特別是執行。我會毫不猶豫地舉報他們;-) – 2010-09-12 11:03:38

+1

啊,有我正在尋找的Fisheye鏈接。 :] – earldouglas 2010-09-12 18:44:05

+1

BTW'AnnotationDrivenBeanDefinitionParser'源代碼也可以在GitHub上查看:https://github.com/cbeams/spring-framework/blob/master/org.springframework.web.servlet/src/main/java/org/springframework /web/servlet/config/AnnotationDrivenBeanDefinitionParser.java – 2011-10-05 17:58:04

5

如果你想避免mvc:annotation-driven標籤,你可以簡單地創建DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter豆自己,但它聽起來像它會更好地得到您的煩惱與標籤本身的根源。

你的問題有哪些症狀?你想用你的Spring MVC應用程序做什麼?

如果您想在使用mvc:annotation-driven時知道封面下方發生了什麼,請參閱AnnotationDrivenBeanDefinitionParser.parse()方法。

+1

「你的問題有什麼症狀?」 - O,只是當我定製彈簧安全接口(例如UserDetailsManager)時,我得到一個「雙重定義」錯誤。或者當我嘗試並定義自己的方面時,我的閱讀是永遠不會被閱讀的,因爲春天使用它自己的方式。此外,我更好地控制我的代碼。 Convention-over-configuration是一件了不起的事......如果你知道約定是什麼;-) – 2010-09-12 11:02:56

3

老問題我知道,但這可能有助於某人。感謝此頁面上的帖子以及over here,我使用以下內容替換了Roo 1.2應用中的註釋驅動標籤。他們踢我是在roo應用程序列表視圖中需要支持類型轉換。

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /> 
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" /> 

<bean id="conversionServiceExposingInterceptor" 
    class="org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor"> 
    <constructor-arg ref="conversionService" /> 
</bean> 

<bean 
    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> 
    <property name="order" value="0" /> 
    <property name="interceptors"> 
     <list> 
      <ref bean="conversionServiceExposingInterceptor" /> 
     </list> 
    </property> 
</bean> 

<bean 
    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
    <property name="webBindingInitializer"> 
     <bean 
      class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> 
      <property name="conversionService" ref="conversionService" /> 
      <property name="validator" ref="validator" /> 
     </bean> 
    </property> 
    <property name="messageConverters"> 
     <list> 
      <bean 
       class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> 
      <bean 
       class="org.springframework.http.converter.StringHttpMessageConverter" /> 
      <bean class="org.springframework.http.converter.FormHttpMessageConverter" /> 
      <bean 
       class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /> 
     </list> 
    </property> 
</bean> 
+0

Spring 3.1引入了[新的支持類](http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#用於@RequestMapping的mvc-ann-requestmapping-31-vs-30)應該被用來「利用新的Spring MVC 3.1特性」。已經更新了這個例子。 – sbk 2012-05-21 22:08:06

2

重寫時,也要考慮自定義的執行管理覆蓋。否則,您所有的自定義異常映射都將失敗。您將不得不重複使用帶有列表bean的messageCoverters:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /> 
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" /> 

<util:list id="messageConverters"> 
    <bean class="your.custom.message.converter.IfAny"></bean> 
    <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean> 
    <bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean> 
    <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean> 
    <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean> 
    <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean> 
    <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean> 
    <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> 
</util:list> 

<bean name="exceptionHandlerExceptionResolver" 
     class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"> 
    <property name="order" value="0"/> 
    <property name="messageConverters" ref="messageConverters"/> 
</bean> 

<bean name="handlerAdapter" 
     class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
    <property name="webBindingInitializer"> 
     <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> 
      <property name="conversionService" ref="conversionService" /> 
      <property name="validator" ref="validator" /> 
     </bean> 
    </property> 
    <property name="messageConverters" ref="messageConverters"/> 
</bean> 

<bean id="handlerMapping" 
     class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> 
</bean>