Hi,
To illustrate, I'm choosing the mvc-basic example in spring-samples. I created two classes org.springframework.samples.mvc.basic.account.X and org.springframework.samples.mvc.basic.account.Y and then I created a converter using spring API. The code is below
Code:
public class X {
private int value;
public X(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
Code:
public class Y {
private int value;
public Y(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
Code:
@Component
public class MyConverter implements Converter<X, Y> {
public Y convert(X arg0) {
Y value = null;
value = new Y(arg0.getValue());
return value;
}
}
After this I copy the src/main/webapp/WEB-INF/spring directory to src/test/resources (This is done to support maven test configuration else I would have to manually create all the configuration myself) and create a junit test shown below
Code:
package org.springframework.samples.mvc.basic;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.samples.mvc.basic.account.X;
import org.springframework.samples.mvc.basic.account.Y;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(locations={"classpath:spring/app-config.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class SimpleTest {
@Autowired
private ConversionService service;
@Test
public void test() {
assertTrue(service.canConvert(X.class, Y.class));
}
}
There's no change in the spring xml files app-config.xml and mvc-config.xml. Now the assertion in my test fails. But if I change the mvc-config.xml declarations to
Code:
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- Forwards requests to the "/" resource to the "welcome" view -->
<mvc:view-controller path="/" view-name="welcome"/>
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="org.springframework.samples.mvc.basic.account.MyConverter" />
</list>
</property>
</bean>
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
The assertion succeeds. Thus I've to register my converter manually and not via context-scanning as is desired.