Let's say we have a hierarchy of multiple of classes extending a same base class: Shape. In this example we are going to create a converter factory, IdToEntityConverterFactory which will tie all subclass of Entity to a single converter StringToShapeConverterFactory. This converter will convert provided string to a shape object.
Example
package com.logicbig.example;
public interface Shape {
}
package com.logicbig.example;
public class Circle implements Shape {
private int radius;
.............
}
package com.logicbig.example;
public class Square implements Shape{
private int side;
.............
}
The converter factory implementation:
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.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;
}
}
Configuring beans and using Conversion service:
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();
}
}
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;
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);
}
}
OutputCircle{radius=10} Square{side=4}
Example ProjectDependencies and Technologies Used: - spring-context 6.1.2 (Spring Context)
Version Compatibility: 3.2.3.RELEASE - 6.1.2 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 17
- Maven 3.8.1
|