Close

Spring Framework - ConverterFactory Examples

Spring Framework 

This example implements a custom ConverterFactory.

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.support.DefaultConversionService;

import java.math.BigDecimal;
import java.util.Date;


public class CustomConverterFactoryExample {

public static void main (String[] args) {

AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(Config.class);

MyBean myBean = context.getBean(MyBean.class);
Entity entity = myBean.getEntity("1000", Order.class);
System.out.println(entity);

entity = myBean.getEntity("431", Customer.class);
System.out.println(entity);
}

@Configuration
public static class Config {

@Bean
public ConversionService conversionService () {
DefaultConversionService service = new DefaultConversionService();
service.addConverterFactory(new IdToEntityConverterFactory());
return service;
}

@Bean
public MyBean myBean () {
return new MyBean();
}
}

public static class MyBean {
@Autowired
private ConversionService service;

public Entity getEntity (String entityId, Class<? extends Entity> type) {
return service.convert(entityId, type);

}
}

private static class IdToEntityConverterFactory implements
ConverterFactory<String, Entity> {

@Override
public <T extends Entity> Converter<String, T> getConverter (Class<T> targetType) {
return new IdToEntityConverter<>(targetType);
}
}

private static class IdToEntityConverter<T extends Entity> implements Converter<String, T> {

private Class<T> targetType;

public IdToEntityConverter (Class<T> targetType) {
this.targetType = targetType;
}

@Override
public T convert (String id) {
return EntityService.getEntity(id, targetType);
}
}

//just a test service
private static class EntityService {

public static <T extends Entity> T getEntity (String id,
Class<T> targetType) {
if (targetType == Item.class) {
return (T) findItemById(id);
} else if (targetType == Order.class) {
return (T) findOrderById(id);
} else if (targetType == Customer.class) {
return (T) findCustomerById(id);
}
return null;
}

private static Customer findCustomerById (String id) {
return new Customer(id, "Mike");
}

private static Order findOrderById (String id) {
return new Order(id, 400, new Date());

}

private static Item findItemById (String id) {
return new Item(id, "some tiem", new BigDecimal(100));
}
}

private abstract static class Entity {
private final String id;

public Entity (String id) {
this.id = id;
}

public String getId () {
return id;
}
}

private static class Order extends Entity {
private final int quantity;
private final Date date;

private Order (String id, int quantity, Date date) {
super(id);
this.quantity = quantity;
this.date = date;
}

public int getQuantity () {
return quantity;
}

public Date getDate () {
return date;
}

@Override
public String toString () {
return "Order{" +
"quantity=" + quantity +
", date=" + date +
'}';
}
}

private static class Item extends Entity {
private final String itemName;
private final BigDecimal cost;

public Item (String id, String itemName, BigDecimal cost) {
super(id);
this.itemName = itemName;
this.cost = cost;
}

public String getItemName () {
return itemName;
}

public BigDecimal getCost () {
return cost;
}

@Override
public String toString () {
return "Item{" +
"itemName='" + itemName + '\'' +
", cost=" + cost +
'}';
}
}

private static class Customer extends Entity {
private final String name;

private Customer (String id, String name) {
super(id);
this.name = name;
}

public String getName () {
return name;
}

@Override
public String toString () {
return "Customer{" +
"name='" + name + '\'' +
'}';
}
}
}

Output

Order{quantity=400, date=Mon May 01 16:07:35 CDT 2017}
Customer{name='Mike'}




This example implements a custom ConverterFactory.

package com.logicbig.example;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class CustomConverterFactoryExample {

public static void main(String[] args) {

AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(Config.class);

ClientBean clientBean = context.getBean(ClientBean.class);
Shape shape = clientBean.getShape("10", Circle.class);
System.out.println(shape);

shape = clientBean.getShape("4", Square.class);
System.out.println(shape);
}
}

Output

Circle{radius=10}
Square{side=4}
package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;

public class ClientBean {
@Autowired
private ConversionService service;

public Shape getShape(String shapeString, Class<? extends Shape> type) {
return service.convert(shapeString, type);
}
}
package com.logicbig.example;

public interface Shape {
}
package com.logicbig.example;

public class Circle implements Shape {
private int radius;

public Circle(int radius) {
this.radius = radius;
}

public int getRadius() {
return radius;
}

@Override
public String toString() {
return "Circle{" +
"radius=" + radius +
'}';
}
}
package com.logicbig.example;

public class Square implements Shape{
private int side;

public Square(int side) {
this.side = side;
}

public int getSide() {
return side;
}

@Override
public String toString() {
return "Square{" +
"side=" + side +
'}';
}
}
package com.logicbig.example;

import org.springframework.core.convert.converter.Converter;

public class ShapeConverter<T extends Shape> implements Converter<String, T> {

private Class<T> targetType;

public ShapeConverter(Class<T> targetType) {
this.targetType = targetType;
}

@Override
public T convert(String source) {
if(targetType==Square.class){
int side = Integer.parseInt(source);
return (T) new Square(side);
}else if(targetType==Circle.class){
int radius = Integer.parseInt(source);
return (T) new Circle(radius);
}
return null;
}
}
package com.logicbig.example;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

public class StringToShapeConverterFactory implements ConverterFactory<String, Shape> {

@Override
public <T extends Shape> Converter<String, T> getConverter(Class<T> targetType) {
return new ShapeConverter<>(targetType);
}
}
package com.logicbig.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;

@Configuration
public class Config {

@Bean
public ConversionService conversionService() {
DefaultConversionService service = new DefaultConversionService();
//registering our custom ConverterFactory
service.addConverterFactory(new StringToShapeConverterFactory());
return service;
}

@Bean
public ClientBean clientBean() {
return new ClientBean();
}
}
Original Post




See Also