【发布时间】:2012-01-16 16:09:04
【问题描述】:
我有一个小型 Spring (3.1.0.RELEASE) 应用程序,它工作得很好,直到我决定需要一个转换器来将东西从字符串转换为其他类型。
我的应用程序上下文文件包含另一个文件 mvc-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<mvc:annotation-driven />
<mvc:view-controller path="/" view-name="index"/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" ref="finnishLocale"/>
</bean>
<bean id="finnishLocale" class="java.util.Locale">
<constructor-arg index="0" value="fi" />
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
p:definitions="/WEB-INF/config/tiles-config.xml"/>
<bean id="tilesViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver">
<property name="viewClass" value="org.springframework.webflow.mvc.view.FlowAjaxTilesView"/>
</bean>
</beans>
这很好用。当我将以下 bean 定义添加到上述文件时,就会出现问题:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="fi.mydomain.app.converter.StringToClassConverter"/>
</list>
</property>
</bean>
(顺便说一下,除了转换器类之外,它与 Spring 文档中显示的 bean 完全相同)。我还像这样修改了注释驱动的行:
<mvc:annotation-driven conversion-service="conversionService"/>
(不过,仅通过添加conversionService bean 就会出现问题)。
(我也写了 fi.mydomain.app.converter.StringToClassConverter 类)。
问题是现在无法再部署应用程序了。日志文件显示错误消息:
2012-01-16 17:55:30,427 [http-8080-7] ERROR ContextLoader.initWebApplicationContext() - Context initialization failed
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'tilesViewResolver' defined in ServletContext resource [/WEB-INF/config/mvc-config.xml]:
Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException;
nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException:
Property 'viewClass' threw exception; nested exception is java.lang.IllegalArgumentException:
Given view class [null] is not of type
[org.springframework.web.servlet.view.AbstractUrlBasedView]
当我从 xml 中删除 conversionService bean 时,一切都恢复正常,当然,除了我不能使用自己的转换器。
我花了几个小时来解决这个问题。任何帮助,将不胜感激。谢谢。
-- 汉努
【问题讨论】:
标签: spring spring-mvc