Announcement Announcement Module
Collapse
No announcement yet.
Spring can't find CustomEditorConfigurer, but it's defined in app-ctx.xml Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring can't find CustomEditorConfigurer, but it's defined in app-ctx.xml

    Hello!
    I'm trying to repeat an example from "Pro Spring 3 Clarence Ho & Rob Harrop" book
    Using Spring Property Editor to convert data from java.lang.String type to 13 special types.

    Here is a code of my PropertyEditorBean
    Code:
    import ...
    public class PropertyEditorBean {
    	private byte[] bytes;
    // 13 fields with different types.
    	private Date date;
    	private String trimString;
    // Date and StringTrim types not defined in Spring as default, and it's necessary to define they manually. 
    	private URL url;
     
    	public void setBytes(byte[] bytes) {
    		System.out.println("Adding " + bytes.length + " bytes");
    		this.bytes = bytes;
    	}
    ...
    // usual setters
    ...
    	public void setUrl(URL url) {
    		System.out.println("Setting URL: " + url.toExternalForm());
    		this.url = url;
    	}
    	public static void main(String[] args) {
    		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
    		ctx.load("classpath:/META-INF/spring/propertyeditor/builtIn.xml");
    		PropertyEditorBean propertyEditor = (PropertyEditorBean) ctx.getBean("builtInSample");
    	}
    }
    My app-ctx file "builtIn.xml" where I have defined CustomEditorConfigurer.
    CustomEditorConfigurer contains CustomDateEditor and StringTrimmerEditor
    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:util="http://www.springframework.org/schema/util"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    						http://www.springframework.org/schema/util 
    						http://www.springframework.org/schema/util/spring-util-3.0.xsd">
     
    		<bean id="dateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor">
    			<constructor-arg>
    				<bean class="java.text.SimpleDateFormat">
    					<constructor-arg value="yyyy-MM-dd" />
    				</bean>
    			</constructor-arg>
    			<constructor-arg value="true" />
    		</bean>
    		
    		<bean id="stringTrimmerEditor" class="org.springframework.beans.propertyeditors.StringTrimmerEditor">
    			<constructor-arg value="true" />
    		</bean>
    		
    		<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    			<property name="customEditors">
    				<map>
    					<entry key="java.util.Date">
    						<ref local="dateEditor"/>
    					</entry>
    					<entry key="java.lang.String">
    						<ref local="stringTrimmerEditor"/>
    					</entry>
    				</map>
    			</property>
    		</bean>
    		
    		<bean id="builtInSample" class="com.apress.prospring3.ch5.propertyeditor.PropertyEditorBean">
    			<property name="bytes">
    				<value>Hello World!</value>
    			</property>
    			<property name="clazz">
    				<value>java.lang.String</value>
    			</property>
    			<property name="trueOrFalse">
    				<value>true</value>
    			</property>
    			<property name="listOfString">
    				<util:list>
    					<value>String member 1</value>
    					<value>String member 2</value>
    				</util:list>
    			</property>
    			<property name="date" value="2011-12-29" />
    			<property name="floatValue">
    				<value>123.45678</value>
    			</property>
    			<property name="file">
    				<value>classpath:test.txt</value>
    			</property>
    			<property name="inputStream">
    				<value>classpath:test.txt</value>
    			</property>
    			<property name="locale">
    				<value>en_US</value>
    			</property>
    			<property name="pattern">
    				<value>a*b</value>
    			</property>
    			<property name="props">
    				<value>
    					name=foo
    					age=19
    				</value>
    			</property>
    			<property name="trimString">
    				<value>   String need trimming   </value>
    			</property>
    			<property name="url">
    				<value>http://www.springframework.org</value>
    			</property>
    		</bean>
    </beans>
    Here is my POM.xml with declared dependencies:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>org.springframework.samples.spring</groupId>
    	<artifactId>spring-utility</artifactId>
    	<version>1.0.0.CI-SNAPSHOT</version>
    	<packaging>jar</packaging>
    	<name>Spring Utility</name>
    	<url>http://www.springframework.org</url>
    	<description>
    		<![CDATA[This project is a minimal jar utility with Spring configuration.]]>
    	</description>
    	<properties>
    		<maven.test.failure.ignore>true</maven.test.failure.ignore>
    		<spring.framework.version>3.0.6.RELEASE</spring.framework.version>
    	</properties>	<dependencies>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.7</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-test</artifactId>
    			<version>${spring.framework.version}</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context</artifactId>
    			<version>${spring.framework.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>log4j</groupId>
    			<artifactId>log4j</artifactId>
    			<version>1.2.14</version>
    		</dependency>
    	</dependencies>
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<configuration>
    					<source>1.5</source>
    					<target>1.5</target>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build> 
    </project>
    Running the program I see the ERROR :
    Code:
    Error creating bean with name 'builtInSample' defined in class path resource [META-INF/spring/propertyeditor/builtIn.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'date': no matching editors or conversion strategy found
    Could any body tell me, what's wrong ?

  • #2
    Resolved. It looks like fun, but after rewriting code from beginnig - it works coorectly without mistakes.
    And I can't find my mistake in previous code, probably, I used wrong import for Date.

    Comment

    Working...
    X