By Jim White (Director of Training and Instructor)
Its fall already! Where did the summer go? Its been almost a year since Spring 3.0 was released. Have you and your organization migrated to this new platform yet? I suspect if you have not, you will be doing so shortly. Spring 3 is loaded with new features - some big and some small. Some features, like Spring Expression Language, are bigger and take some time to learn and use. Others are quite simple and can be incorporated quickly.
Type Conversion System
Take the new type conversion system as an example. This is a relatively small feature that can really help convert data between types. In particular, it can be used to convert String data found in Spring XML configuration files to the appropriate data type. In this way, it serves as an alternative to the PropertyEditors and CustomEditorConfigurers of past Spring versions.
To understand how the type conversion system works, say you wanted to configure Customer beans in XML. The Customer class has an ssn property of type CustomerSSN (shown below).
public class CustomerSSN {
private int area;
private int group;
private int serial;
public CustomerSSN(int a, int g, int s) {
this.area = a;
this.group = g;
this.serial = s;
}
public int getArea() {
return area;
}
public void setArea(int area) {
this.area = area;
}
public int getGroup() {
return group;
}
public void setGroup(int group) {
this.group = group;
}
public int getSerial() {
return serial;
}
public void setSerial(int serial) {
this.serial = serial;
}
}
You want to dependency inject SSN data into the Customer objects, but how do you get string data, like that shown below, into a CustomerSSN object?
<bean id="example" class="Customer">
<property name="ssn" value="456-21-7890" />
</bean>
Type conversion system to the rescue!
The Converter Interface
At the heart of this new system is the Spring Framework provided Converter interface. The Converter interface is typed to allow you to convert from any one type to another.
public interface Converter<S, T> { }
The S is the source type and T is the target type. The Converter interface is very simple. It requires implementing classes to have just one method - a convert( ) method. The convert method must take a parameter of the source type and return a reference of the target type. Here is an example implementation of Converter to convert String objects to CustomerSSN objects.
import org.springframework.core.convert.converter.Converter;
public final class SSNConverter implements Converter<String, CustomerSSN> {
public CustomerSSN convert(String source) {
int area = Integer.parseInt(source.substring(0, 3));
int group = Integer.parseInt(source.substring(4, 6));
int serial = Integer.parseInt(source.substring(7, 11));
return new CustomerSSN(area, group, serial);
}
}
A ConversionService
Behind the scenes, a ConversionService executes type conversion logic at runtime. Simply register your Converters with a Spring ConversionService bean to allow it to use your Converter. A Spring-provided default ConversionService bean converts between strings, numbers, enums, collections, maps, and other common types. You can provide configuration - through a ConversionServiceFactoryBean - to register your Converters or override the default converters with the ConversionService. In the example below, the SSNConverter is registered.
<bean id="conversionService" class=
"org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.intertech.SSNConverter" />
</list>
</property>
</bean>
That?s all there is to handling conversion! Now, your XML configuration can use SSN-formatted strings and they will be converted to CustomerSSN objects automatically. In the case below, when the example Customer bean is instantiated, it will be dependency injected with a CustomerSSN object with area property set to 456, group property set to 21 and serial property set to 7890.
<bean id="example" class="Customer">
<property name="ssn" value="456-21-7890" />
</bean>
Importantly, since the Converters are just another object, you can define them as beans and dependency inject them anywhere conversion might be needed. This makes them more versatile that the old PropertyEditors. You can learn more about the type conversion system here.
Spring 3 Class Coming Soon
I have been updating Intertech's Complete Spring class and it should be out around the holiday season. It will include sections on the new Spring 3 annotations, Spring Expression Language, REST support, improved MVC framework, and much much more. The class will now be 5 days long and packed with labs to give you real hands on experience you can take to work the next week. Go to www.intertech.com to learn more about our course offerings and stay tuned for more details on new classes coming out this fall/winter.