【问题标题】:Spring 3.1 ConversionServiceFactoryBean breaks tilesViewResolverSpring 3.1 ConversionServiceFactoryBean 打破了tilesViewResolver
【发布时间】: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


    【解决方案1】:

    我发现问题来自我的转换器。它是这样定义的:

    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"));
        }
    }
    

    这给了我原始问题中描述的症状。当我将转换器更改为:

    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"));
        }
    }
    

    也就是说,在我将 Class 替换为 Object 之后,应用程序又开始工作了。不过,我不明白第一个 Converter 出了什么问题,而且我对解决方案也不完全满意,但我想现在必须这样做。

    -- 汉努

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-21
      • 2021-01-05
      • 2012-07-17
      • 2015-09-27
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多