Reputation: 33
I have a small Spring (3.1.0.RELEASE) application, which was working just fine, until I decided I need to have a Converter for converting stuff from Strings to other types.
My application context file includes another file, 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>
This works fine. The problem occurs, when I add the following bean definition to the above file:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="fi.mydomain.app.converter.StringToClassConverter"/>
</list>
</property>
</bean>
(Which, by the way, is exactly the same bean shown in Spring documentation, apart from the converter class). I've also modified the the annotation-driven line like this:
<mvc:annotation-driven conversion-service="conversionService"/>
(The problem occurs, though, by merely adding the conversionService bean).
(I also do have the fi.mydomain.app.converter.StringToClassConverter class written).
The problem is that now the application cannot be deployed anymore. The log file shows an error message:
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]
And when I remove the conversionService bean from the xml, everything works again, except of course, that I cannot use my own converters.
I've spent hours with this to no avail. Any help would be appreciated. Thanks.
-- Hannu
Upvotes: 1
Views: 2487
Reputation: 33
I found out that the problem emerges from my Converter. It was defined like this:
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"));
}
}
This gave me the symptoms described in the original question. When I changed the Converter to this:
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"));
}
}
That is, after I replaced Class with Object, the application started working again. I do not understand what's wrong in the first Converter, though, and I'm not completely happy with the solution either, but I guess that will have to do now.
-- Hannu
Upvotes: 2