Montag, 18. April 2011

How to write a SqlTimestampPropertyEditor in Spring

Sometimes it is important to have your own PropertyEditor [see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/validation.html#beans-beans-conversion] in Spring, for example when you want to have SqlTimestamp as property in a bean. For this JavaType spring has no build-in PropertyEditor Implementation.

First step: Implement your SqlTimestampPropertyEditor:

package org.springframework.beans.propertyeditors;

import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
* Property editor for java.sql.Timestamp, supporting SimpleDateFormat.
*
* Using default Constructor uses the pattern yyyy-MM-dd
* Using the constructor with String, you can use your own pattern.
*
*/
public class SqlTimestampPropertyEditor extends PropertyEditorSupport {

public static final String DEFAULT_BATCH_PATTERN = "yyyy-MM-dd";

private final SimpleDateFormat sdf;

/**
* uses default pattern yyyy-MM-dd for date parsing.
*/
public SqlTimestampPropertyEditor() {
this.sdf = new SimpleDateFormat(SqlTimestampPropertyEditor.DEFAULT_BATCH_PATTERN);
}

/**
* Uses the given pattern for dateparsing, see {@link SimpleDateFormat} for allowed patterns.
*
* @param pattern
* the pattern describing the date and time format
* @see SimpleDateFormat#SimpleDateFormat(String)
*/
public SqlTimestampPropertyEditor(String pattern) {
this.sdf = new SimpleDateFormat(pattern);
}

/**
* @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
*/
@Override
public void setAsText(String text) throws IllegalArgumentException {

try {
setValue(new Timestamp(this.sdf.parse(text).getTime()));
} catch (ParseException ex) {
throw new IllegalArgumentException("Could not parse date: " + ex.getMessage(), ex);
}
}

/**
* Format the Timestamp as String, using the specified DateFormat.
*/
@Override
public String getAsText() {
Timestamp value = (Timestamp) getValue();
return (value != null ? this.sdf.format(value) : "");
}
}

Second Step: Register the SqlTimestampPropertyEditor
Open your application-context.xml file and define the bean:

<!-- A Custom Property Editor for SqlTimestamp -->
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.sql.Timestamp" value="org.springframework.beans.propertyeditors.SqlTimestampPropertyEditor">
</map>
</property>
</bean>

Third step: Use it in a different bean:

<bean id="exampleSimpleBean" class="org.springframework.example.SimpleBean">
<property name="timestamp">
<value type="java.sql.Timestamp">2008-01-01</value>
</property>
</bean>

<bean id="exampleMapBean" class="org.springframework.example.MapBean">
<property name="parameterValues">
<map>
<entry key="timestamp">
<value type="java.sql.Timestamp">2008-01-01</value>
</entry>
</map>
</property>
</bean>

Where the date (2008-01-31) is defined you can also use the spring expression language to access application wide or scope wide variables.

Bean class examples:

org.springframework.example.SimpleBean

package org.springframework.example;

import java.sql.Timestamp;

public class SimpleBean {

private Timestamp timestamp;

public Timestamp getTimestamp() {
return timestamp;
}

public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
}

org.springframework.example.MapBean

package org.springframework.example;

import java.util.Map;

public class MapBean {

private Map parameterValues;

public void setParameterValues(Map parameterValues) {
this.parameterValues = parameterValues;
}
}

In another article we will see why I need a special SqlTimestampPropertyEditor

Keine Kommentare:

Kommentar veröffentlichen