Results 1 to 3 of 3

Thread: Converter Registration via context-scanning

  1. #1
    Join Date
    Dec 2009
    Location
    India
    Posts
    108

    Question Converter Registration via context-scanning

    Hi,

    I've created few custom converters using the Spring converter API and annotated them with @Component. These converters are available in application context but don't get automatically registered with the conversionService unless I do it explicitly which means I can't use spring context/classpath scanning to auto-register custom converters. Any help is welcome.

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    These converters are available in application context but don't get automatically registered with the conversionService unless I do it explicitly which means I can't use spring context/classpath scanning to auto-register custom converters
    Can you share your configuration? and your solution?

    and your error stack trace if you got one?
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    Dec 2009
    Location
    India
    Posts
    108

    Exclamation Sample Application Code

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •