2012-01-16 67 views
1

我有一个小的Spring(3.1.0.RELEASE)应用程序,它工作得很好,直到我决定我需要一个转换器将东西从字符串转换为其他类型。Spring 3.1 ConversionServiceFactoryBean打破tilesViewResolver

我的应用程序上下文文件包含另一个文件,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豆)。

(我也有写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豆,一切再次工作,但当然,我不能用我自己的转换器。

我花了几个小时与此无济于事。任何帮助,将不胜感激。谢谢。

- 哈努哈利

回答

2

我发现这个问题从我的转换出现。它是这样定义的:

final class StringToClassConverter implements Converter<String, Class>, InitializingBean { 

    private Map<String, Class> map = new HashMap<String, Class>(); 

    public Class convert(String key) { 
     return map.get(key); 
    } 

    public void afterPropertiesSet() throws Exception { 
     map.put("organizations", Class.forName("fi.mydomain.app.domain.Organization")); 
     map.put("invoices", Class.forName("fi.mydomain.app.domain.Invoice")); 
    } 
} 

这给了我原来问题中描述的症状。当我将Converter更改为:

final class StringToClassConverter implements Converter<String, Object>, InitializingBean { 

    private Map<String, Object> map = new HashMap<String, Object>(); 

    public Object convert(String key) { 
     return map.get(key); 
    } 

    public void afterPropertiesSet() throws Exception { 
     map.put("organizations", Class.forName("fi.mydomain.app.domain.Organization")); 
     map.put("invoices", Class.forName("fi.mydomain.app.domain.Invoice")); 
    } 
} 

也就是说,在我用Object替换Class之后,应用程序又开始工作了。不过,我不明白第一台Converter中有什么问题,但我也不完全满意解决方案,但我想现在不得不这样做。

- Hannu