spring vmc3.1.1 上,經過AnnotationMethodHandlerAdap...

spring vmc3.1.1 下,經過AnnotationMethodHandlerAdapter配置webBindingInitializer失效解決方案

問題: java

spring vmc3.1.1 下,經過AnnotationMethodHandlerAdapter配置webBindingInitializer失效:  web

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="webBindingInitializer">
			<bean class="com.xxx.ark.core.web.CustomerBindingInitializer" />
		</property>
		<property name="messageConverters">
		  <list>
		    <ref bean="jacksonMessageConverter"/>
		  </list>
		</property>
	</bean>
	
	<mvc:annotation-driven >
    	<mvc:argument-resolvers>
    		<bean class="com.xxx.ark.core.web.FormBeanArgumentResolver" /> 
    	</mvc:argument-resolvers>
    </mvc:annotation-driven>

  緣由: spring

annotation-driven缺省註冊類的改變  mvc

Spring 3.0.x中使用了annotation-driven後,缺省使用DefaultAnnotationHandlerMapping 來註冊handler method和request的mapping關係。 AnnotationMethodHandlerAdapter來在實際調用handlermethod前對其參數進行處理。   app

在spring mvc 3.1中,對應變動爲 
DefaultAnnotationHandlerMapping -> RequestMappingHandlerMapping 
AnnotationMethodHandlerAdapter -> RequestMappingHandlerAdapter 
AnnotationMethodHandlerExceptionResolver -> ExceptionHandlerExceptionResolver 

以上都在使用了annotation-driven後自動註冊。 
  並且對應分別提供了AbstractHandlerMethodMapping , AbstractHandlerMethodAdapter和 AbstractHandlerMethodExceptionResolver以便於讓用戶更方便的實現自定義的實現類。   ide

<mvc:annotation-driven/>至關於註冊了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter兩個bean,配置一些messageconverter。即解決了@Controller註解的使用前提配置。  spa

spring mvc <mvc:annotation-driven />會自動啓動Spring MVC的註解功能,但實際它作了哪些工做呢?  code

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
  <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
   <property name="conversionService" ref="conversionService" />
   <property name="validator" ref="validator" />
  </bean>
</property>
</bean>
<bean id="conversionService" class="org.springframework.samples.petclinic.util.PetclinicConversionServiceFactory" />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

 

從上面的配置能夠看出,個人配置應該是被sping的配置覆蓋了,<mvc:annotation-driven />配置中已經包含了webBindingInitializer的配置,看來使用<mvc:annotation-driven />後與原來的配置出現了重複,這種狀況下無論<mvc:annotation-driven />放在上面仍是放在下面都會出現問題。 orm

解決方法: xml

使用conversion-service來註冊自定義的converter 
DataBinder實現了PropertyEditorRegistry, TypeConverter這兩個interface,而在spring mvc實際處理時,返回值都是return binder.convertIfNecessary(見HandlerMethodInvoker中的具體處理邏輯)。所以可使用customer conversionService來實現自定義的類型轉換。 

<mvc:annotation-driven />中配置能夠看出,AnnotationMethodHandlerAdapter已經配置了webBindingInitializer,咱們能夠經過設置其屬性conversionService來實現自定義類型轉換。

 

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">  
		<property name="converters">  
	    	<list>  
	        	<bean class="com.xxx.ark.core.web.DateConverter" />  
	    	</list>  
		</property>  
	</bean>

  須要修改spring service context xml配置文件中的annotation-driven,增長屬性conversion-service指向新增的conversionService bean。 

<mvc:annotation-driven conversion-service="conversionService" />

  實際自定義的converter以下。 

public class DateConverter implements Converter<String, Date> {  
@Override  
public Date convert(String source) {  
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
    dateFormat.setLenient(false);  
    try {  
        return dateFormat.parse(source);  
    } catch (ParseException e) {  
        e.printStackTrace();  
    }         
    return null;  
}
相關文章
相關標籤/搜索