Spring字符自动解释成Enum

最近在搭新项目的开发框架的时候,有些配置中需要使用到enum类,在以前项目的spring-mvc.xml中都是直接字符配置好就OK了,但是在新的项目中,启动的时候就是抛出org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type [java.lang.String] to required type [com.kongming.api.interceptor.AnnotationHandlerInterceptor$AnnotationTarget] for property 'annotationTarget'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.kongming.api.interceptor.AnnotationHandlerInterceptor$AnnotationTarget] for property 'annotationTarget': no matching editors or conversion strategy found

后来我在对比新老项目的配置的时候发现在老项目中有一处是配置了org.springframework.format.support.FormattingConversionServiceFactoryBean.把这个配置加上去之前就正常了.(配置如下)

AnnotationTarget

enum AnnotationTarget {
<span style="white-space:pre">	</span>TYPE_AND_METHOD, TYPE, METHOD
}

AnnotationHandlerInterceptor

public class AnnotationHandlerInterceptor extends HandlerInterceptorAdapter {
	private AnnotationTarget annotationTarget;
	public void setAnnotationTarget(AnnotationTarget annotationTarget) {
		this.annotationTarget = annotationTarget;
	}
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
              //TODO 
	}
}

spring-mvc.xml

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
	<property name="formatters">
		<set>
			<bean class="com.kongming.spring.converter.DecryptionConversionServiceFactoryBean" />
		</set>
	</property>
</bean>

<mvc:interceptor>
<span style="white-space:pre">	</span><mvc:mapping path="/**" />
	<bean class="com.kongming.api.interceptor.AnnotationHandlerInterceptor">
		<property name="annotationTarget">
			<value>TYPE_AND_METHOD</value>
		</property>
	</bean>
</mvc:interceptor>


具体什么原因还没有去搞清楚,目前也没有时间去看源码.以后弄清楚了再发一篇文章吧.如果有了解的同学,欢迎在评论中留言!

发布了40 篇原创文章 · 获赞 19 · 访问量 22万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章