November 21, 2014
Hot Topics:

Spring 3 Custom Formatting, Page 2

  • December 29, 2010
  • By Jim White
  • Send Email »
  • More Articles »

Spring 3 Custom Formatting Annotations

While the formatter allows you to create a means to parse and print field values, the convenience of annotations (like those provided by @NumberFormat and @DateTimeFormat) is hard to beat. Wouldn't it be nice to have that same convenience with your custom formatter? Well, you can! The Formatter SPI allows you to create annotations for your custom formatter.

You first need to define an annotation to mark a field for custom formatting. The code in Listing 4 below defines a new annotation that you will use on CustomerSSN fields to trigger formatting.

Listing 4. SSNFormat Defines the Custom Formatting Annotation

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface SSNFormat {
}

SSNFormat defines the annotation to use on CustomerSSN fields to designate them for custom parsing and printing.

Next, implement an AnnotationFormatterFactory (org.springframework.format) rather than implementing the formatter interface. Listing 5 shows the same SSN formatting logic (from the SSNFormatter example above) baked into a new AnnotationFormatterFactory.

Listing 5. SSNAnnotationFormatterFactory Implements the AnnotationFormatterFactory Interface

public final class SSNAnnotationFormatterFactory implements
AnnotationFormatterFactory<SSNFormat> {

Set<Class<?>> getFieldTypes() {
Set<Class<?>> set = new HashSet<Class<?>>();
set.add(CustomerSSN.class);
return set;
}

public Parser<CustomerSSN> getParser(SSNFormat arg0, Class<?> arg1) {
return new SSNFormatter();
}

public Printer<CustomerSSN> getPrinter(SSNFormat annotation, Class<?> fieldType) {
return new SSNFormatter();
}
}

This factory will be registered with the FormatterRegistry.

As before, you still need to register your AnnotationFormatterFactory with the FormatterRegistry. You can again use the FormattingConversionServiceFactoryBean. However, this time register the annotation-based formatter. The FormattingConversionServiceFactoryBean has an addFormatterForAnnotation() method for this purpose.

Listing 6. MyFormattingFactory Again Adds the Custom Formatter to the Formatter Registry

public class MyFormattingFactory extends
FormattingConversionServiceFactoryBean {

public void installFormatters(FormatterRegistry registry) {
super.installFormatters(registry);
registry.addFormatterForFieldType(CustomerSSN.class,
new SSNFormatter());
}
}

Now, simply use the annotation on the Customer class to have strings in the display converted to CustomerSSN and vice versa.

Listing 7. The CustomerSSN Property Is Now Annotated

public class Customer {
...
private CustomerSSN ssn = new CustomerSSN();
...
}

public class CustomerSSN {

private int area; //first 3
private int group; //middle 2
private int serial; // last 4
...
}

The field is parsed and printed appropriately per the SSNFormatter.

Wrap Up

While the new Spring 3 formatting annotations are simple to use and very helpful, most applications call for formatting beyond simple numbers and date/times. As shown here, the new Formatter SPI in Spring 3 gives you the power to build custom formatting capability. With just a little more work, you can also create parsing/printing annotations like @NumberFormat and @DateTimeFormat.

Code Download

About the Author

Jim White is the Director of Training and partner with Intertech Training. Reach him by e-mail at jwhite@intertech.com.


Tags: annotations, Spring, SPI

Originally published on http://www.developer.com.

Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel