Spring 3 has such a nice feature as type conversion. It provides a converter SPI(Converter<S, T>) to be used to implement differenet conversion logic.
The subclass of Converter type allow to define one-way conversion(only from S to T), so if I want a conversion also to be performed from T to S I need to define another converter class that implement Converter<T, S>. If I have many classes which are subject to conversion, i need to define many converters.
Is there any posibility to define two-way conversion logic(from S to T and from T to S) in one converter? and how it will be used?
PS. now I'm using my converters via ConversionServiceFactoryBean defining/injecting them in configuration file
You are correct, if you want to use the org.springframework.core.convert.converter.Converter interface directly, you'll need to implement two converters, one for each direction.
But spring 3 has a couple of other options:
If your conversion is not object-to-object but rather object-to-string (and back), then you can implement a org.springframework.format.Formatter instead. Formatters get registered as GenericConverters (see http://static.springsource.org/spring-webflow/docs/2.3.x/reference/html/ch05s07.html#converter-upgrade-to-spring-3)
Otherwise you could implement your own org.springframework.core.convert.converter.GenericConverter, which makes it easy to create TwoWayConverter implementations using reflection.
public abstract class AbstractTwoWayConverter<S, T> implements GenericConverter {
private Class<S> classOfS;
private Class<T> classOfT;
protected AbstractTwoWayConverter() {
Type typeA = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
Type typeB = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
this.classOfS = (Class) typeA;
this.classOfT = (Class) typeB;
}
public Set<ConvertiblePair> getConvertibleTypes() {
Set<ConvertiblePair> convertiblePairs = new HashSet<ConvertiblePair>();
convertiblePairs.add(new ConvertiblePair(classOfS, classOfT));
convertiblePairs.add(new ConvertiblePair(classOfT, classOfS));
return convertiblePairs;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (classOfS.equals(sourceType.getType())) {
return this.convert((S) source);
} else {
return this.convertBack((T) source);
}
}
protected abstract T convert(S source);
protected abstract S convertBack(T target);
}
/**
* converter to convert between a userId and user.
* this class can be registered like so:
* conversionService.addConverter(new UserIdConverter (userDao));
*/
public class UserIdConverter extends AbstractTwoWayConverter<String, User> {
private final UserDao userDao;
#Autowired
public UserIdConverter(UserDao userDao) {
this.userDao = userDao;
}
#Override
protected User convert(String userId) {
return userDao.load(userId);
}
#Override
protected String convertBack(User target) {
return target.getUserId();
}
}
Spring has just such an interface for this purpose: TwoWayConverter.
see the following:
http://static.springsource.org/spring-webflow/docs/2.0.x/javadoc-api/org/springframework/binding/convert/converters/TwoWayConverter.html
You can use Spring Formatter to format object of type T to String and vice versa.
package org.springframework.format;
public interface Formatter<T> extends Printer<T>, Parser<T> {
}
Using this interface you can achieve the same as the Barry Pitman says but with less code and this is the preferable way by the Spring documentation if you waht to format to String and vice versa. So the Barry's UserIdConverter class is going to look like this:
public class UserIdConverter implements Formatter<User> {
private final UserDao userDao;
#Autowired
public UserIdConverter(UserDao userDao) {
this.userDao = userDao;
}
#Override
public User parse(String userId, Locale locale) {
return userDao.load(userId);
}
#Override
public String print(User target, Locale locale) {
return target.getUserId();
}
}
To register this Formatter you should include this in your XML config:
...
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" >
<property name="formatters">
<set>
<bean class="com.x.UserIdConverter"/>
</set>
</property>
</bean>
...
! NOTE that this class can be used only for formatting from some type T to String and vice versa. You can not format from type T to some other type T1 for example. If you have this case you should go with the Spring GenericConverter and use the Barry Pitman answer:
public abstract class AbstractTwoWayConverter<S, T> implements GenericConverter {
private Class<S> classOfS;
private Class<T> classOfT;
protected AbstractTwoWayConverter() {
Type typeA = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
Type typeB = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
this.classOfS = (Class) typeA;
this.classOfT = (Class) typeB;
}
public Set<ConvertiblePair> getConvertibleTypes() {
Set<ConvertiblePair> convertiblePairs = new HashSet<ConvertiblePair>();
convertiblePairs.add(new ConvertiblePair(classOfS, classOfT));
convertiblePairs.add(new ConvertiblePair(classOfT, classOfS));
return convertiblePairs;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (classOfS.equals(sourceType.getType())) {
return this.convert((S) source);
} else {
return this.convertBack((T) source);
}
}
protected abstract T convert(S source);
protected abstract S convertBack(T target);
}
/**
* converter to convert between a userId and user.
* this class can be registered like so:
* conversionService.addConverter(new UserIdConverter (userDao));
*/
public class UserIdConverter extends AbstractTwoWayConverter<String, User> {
private final UserDao userDao;
#Autowired
public UserIdConverter(UserDao userDao) {
this.userDao = userDao;
}
#Override
protected User convert(String userId) {
return userDao.load(userId);
}
#Override
protected String convertBack(User target) {
return target.getUserId();
}
}
And add to your XML config:
...
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" >
<property name="converters">
<set>
<bean class="com.x.y.UserIdConverter"/>
</set>
</property>
</bean>
...
Related
I've got an endpoint:
/api/offers/search/findByType?type=X
where X should be an Integer value (an ordinal value of my OfferType instance), whereas Spring considers X a String and will be applying its StringToEnumConverterFactory with the StringToEnum convertor.
public interface OfferRepository extends PagingAndSortingRepository<Offer, Long> {
List<Offer> findByType(#Param("type") OfferType type);
}
So I wrote a custom Converter<Integer, OfferType> which simply get a instance by the given ordinal number:
public class IntegerToOfferTypeConverter implements Converter<Integer, OfferType> {
#Override
public OfferType convert(Integer source) {
return OfferType.class.getEnumConstants()[source];
}
}
Then I registered it properly with a Configuration:
#EnableWebMvc
#Configuration
#RequiredArgsConstructor
public class GlobalMVCConfiguration extends WebMvcConfigurerAdapter {
#Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new IntegerToOfferTypeConverter());
}
}
And I was expected that all requests to findByType?type=X will pass through my converter, but they do not.
Is any way to say that all enums defined as a request parameters have to be provided as an Integer? Furthermore, is any way to say it globally, not just for a specific enum?
EDIT: I've found IntegerToEnumConverterFactory in my classpath that does all I need. And it is registered with DefaultConversionService which is a default service for conversion. How can that be applied?
EDIT2: It's such a trivial thing, I was wondering if there is a property to turn enum conversion on.
EDIT3: I tried to write a Converter<String, OfferType> after I had got String from TypeDescriptor.forObject(value), it didn't help.
EDIT4: My problem was that I had placed custom converter registration into a MVC configuration (WebMvcConfigurerAdapter with addFormatters) instead of a REST Repositories one (RepositoryRestConfigurerAdapter with configureConversionService).
Spring parses the query parameters as Strings. I believe it always uses Converter<String, ?> converters to convert from the query parameters to your repository methods parameters. It uses an enhanced converter service, since it registers its own converters such as Converter<Entity, Resource>.
Therefore you have to create a Converter<String, OfferType>, e.g.:
#Component
public class StringToOfferTypeConverter implements Converter<String, OfferType> {
#Override
public OfferType convert(String source) {
return OfferType.class.getEnumConstants()[Integer.valueOf(source)];
}
}
And then configure this converter to be used by the Spring Data REST, in a class extending RepositoryRestConfigurerAdapter:
#Configuration
public class ConverterConfiguration extends RepositoryRestConfigurerAdapter {
#Autowired
StringToOfferTypeConverter converter;
#Override
public void configureConversionService(ConfigurableConversionService conversionService) {
conversionService.addConverter(converter);
super.configureConversionService(conversionService);
}
}
I tried to add this to the basic tutorial, added a simple enum to the Person class:
public enum OfferType {
ONE, TWO;
}
#Entity
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private OfferType type;
public OfferType getType() {
return type;
}
public void setType(OfferType type) {
this.type = type;
}
}
And when I call:
http://localhost:8080/people/search/findByType?type=1
I get the result without errors:
{
"_embedded" : {
"people" : [ ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/search/findByType?type=1"
}
}
}
To implement a global Enum converter, you have to create a factory and register it in the configuration using the method: conversionService.addConverterFactory(). The code below is a modified example from the documentation:
public class StringToEnumFactory implements ConverterFactory<String, Enum> {
public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
return new StringToEnum(targetType);
}
private final class StringToEnum<T extends Enum> implements Converter<String, T> {
private Class<T> enumType;
public StringToEnum(Class<T> enumType) {
this.enumType = enumType;
}
public T convert(String source) {
Integer index = Integer.valueOf(source);
return enumType.getEnumConstants()[index];
}
}
}
I'm trying to add some extra validation logic on my REST beans using annotations. This is just an example, but the point is that the annotation is to be used on multiple REST resource objects / DTO's.
I was hoping for a solution like this:
public class Entity {
#NotNull // JSR-303
private String name;
#Phone // Custom phonenumber that has to exist in a database
private String phoneNumber;
}
#Component
public class PhoneNumberValidator implements Validator { // Spring Validator
#Autowired
private PhoneRepository repository;
public boolean supports(Class<?> clazz) {
return true;
}
public void validate(Object target, Errors errors) {
Phone annotation = // find fields with annotations by iterating over target.getClass().getFields().getAnnotation
Object fieldValue = // how do i do this? I can easily get the annotation, but now I wish to do a call to repository checking if the field value exists.
}
}
Did you try JSR 303 bean validator implementations like hibernate validator
e.g. is available here http://www.codejava.net/frameworks/spring/spring-mvc-form-validation-example-with-bean-validation-api
Maven Module A:
public interface RestValidator<A extends Annotation, T> extends ConstraintValidator<A, T>
public interface PhoneValidator extends RestValidator<PhoneNumber, String>
#Target(FIELD)
#Retention(RUNTIME)
#Constraint(validatedBy = PhoneValidator.class) // This usually doesnt work since its a interface
public #interface PhoneNumber {
// JSR-303 required fields (payload, message, group)
}
public class Person {
#PhoneNumber
private String phoneNumber;
}
Maven Module B:
#Bean
LocalValidatorFactoryBean configurationPropertiesValidator(ApplicationContext context, AutowireCapableBeanFactory factory) {
LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
factoryBean.setConstraintValidatorFactory(factory(context, factory));
return factoryBean;
}
private ConstraintValidatorFactory factory(final ApplicationContext context, final AutowireCapableBeanFactory factory) {
return new ConstraintValidatorFactory() {
#Override
public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) {
if (RestValidator.class.isAssignableFrom(key)) {
return context.getBean(key);
} else {
return factory.createBean(key);
}
}
#Override
public void releaseInstance(ConstraintValidator<?, ?> instance) {
if (!(instance instanceof RestValidator<?, ?>)) {
factory.destroyBean(instance);
}
}
};
}
#Bean
WebMvcConfigurerAdapter webMvcConfigurerAdapter(final LocalValidatorFactoryBean validatorFactoryBean) {
return new WebMvcConfigurerAdapter() { // Adds the validator to MVC
#Override
public Validator getValidator() {
return validatorFactoryBean;
}
};
}
Then I have a #Component implementation of PhoneValidator that has a Scope = Prototype.
I hate this solution, and I think Spring SHOULD look up on Interface implementations by default, but I'm sure some people that are a lot smarter than me made the decision not to.
Hy
I'm looking for a way to 'simplify'/shorten my spring configuration.
I' ve got this Generic service that looks something like:
public class GenericService<T> {
private Class<T> targetClass;
public void setTargetClass(Class<T> targetClass) {this.targetClass = targetClass;}
public void doSomething() {...}
}
and in my spring-config.xml file I have
<bean id="entity1Service" class="GenericService">
<property name="targetClass" value="model.Entity1" />
</bean>
<bean id="entity2Service" class="GenericService">
<property name="targetClass" value="model.Entity2" />
</bean>
...
I'm trying to build a factory that will build all these services for me so that I could write something like this in my spring-config.xml
<bean class="ServiceFactory">
<property name="targets">
<list>
<value>model.Entity1</value>
<value>model.Entity2</value>
</list>
</property>
</bean>
which would generate 2 beans (one named entity1Service, the other entity2Service). Get-it?
How would I start? I've looked at BeanFactory (not to be confused with FactoryBean!) but fail to see how to hookup everything up.
It would be even better if my factory could scan my packages and generate a service when it finds an entity (either through annotation or interface implementation), a little like #EnableJpaRepositories annotation in spring-data does for all JpaRepository interfaces.
Thanks for any insights, examples, pointers...
w.
I believe I've figured it out and posting my result for future references.
public class GenericBeanGenerator <T, G> implements BeanFactoryPostProcessor, BeanPostProcessor {
/**
* The type of the bean to create
*/
private Class<T> type;
/**
* The list of generic values. Can be String, Class or whatever
*/
private Iterable<G> generics;
private Map<String, G> beanNameToGeneric = new HashMap<String, G>();
public GenericBeanGenerator(Class<T> type, G[] generics) {
this.type = type;
this.generics = Arrays.asList(generics);
}
public GenericBeanGenerator(Class<T> type, Iterable<G> generics) {
this.type = type;
this.generics = generics;
}
#Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// for each 'generic' value, add a bean definition to the beanFactory with the appropriate bean name
for(G generic : generics) {
String beanName = getBeanName(generic);
beanNameToGeneric.put(beanName, generic);
((DefaultListableBeanFactory) beanFactory).registerBeanDefinition(beanName, new AnnotatedGenericBeanDefinition(type) );
}
}
#Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (type.isInstance(bean) && beanNameToGeneric.containsKey(beanName)) {
#SuppressWarnings("unchecked")
T instance = (T) bean;
initiliaze(beanName, beanNameToGeneric.get(beanName), instance);
}
return bean;
}
#Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
/**
* Convert a 'generic' value to a string in order to name the bean
*/
public String getBeanName(G generic) {
return generic.toString();
}
/**
* Initialize the bean if needed.
*/
public void initiliaze(String beanName, G generic, T instance) {
}
}
Now if I want a number of generics services that extends a class like...
class GenericService<T> {
Class<T> entityClass;
public void setEntityClass(Class<T> clazz) {
this.entityClass = clazz;
}
....
}
I could have something like this in one of my #Configuration beans
class Config {
#Bean
public static GenericBeanGenerator<GenericService, Class<?>> genericServiceGenerator() {
List<Class<?>> entities = Arrays.asList(A.class, B.class);
return new GenericBeanGenerator<GenericService, Class<?>>(GenericService.class, entities) {
#Override
public String getBeanName(Class<?> generic) {
return generic.getSimpleName() + "Service";
}
#Override
public void initiliaze(String beanName, Class<?> generic, GenericService instance) {
instance.setEntityClass(generic);
}
};
}
}
This would generate two beans named AService and BService for my entities A and B.
And even more powerfull: If my entities all implement an interface named Idable, I could generate all my service beans if I used something like this to scan my packages:
BeanDefinitionRegistry bdr = new SimpleBeanDefinitionRegistry();
ClassPathBeanDefinitionScanner s = new ClassPathBeanDefinitionScanner(bdr);
TypeFilter tf = new AssignableTypeFilter(Idable.class);
s.addIncludeFilter(tf);
s.scan("org.willix.model");
entities = bdr.getBeanDefinitionNames();
You can try doing it programmatically:
public class Application {
#Autowired
private ApplicationContext applicationContext;
public void loadBeans() {
NewBean newBean = applicationContext.getAutowireCapableBeanFactory().createBean(NewBean.class);
}
}
You should also be able to autowire these beans after they have been created.
Edit:
You can name these beans using an annotation in the bean's class:
#Component("NewBean1")
public class NewBean1 implements GenericService<T> {
}
And then when you autowire it, use the #Qualifier annotation
public class BeanController {
#Autowired
#Qualifier("NewBean1")
private GenericService<T> bean;
}
Is there a clean way in Spring (with no XML) to have an interface wired to an invocation handler? Currently I have to do something like this:
#Inject
private ServiceProxyCreator services;
private MyServiceInterface service;
private MyServiceInterface getService() {
if ( service == null )
service = services.createProxy( MyServiceInterface.class );
return service;
}
Where #createProxy is simply an implementation of something like this:
#SuppressWarnings( "unchecked" )
public <T> T createProxy( Class<T> type ) {
JobRpcHandler handler = new JobRpcHandler();
handler.setServiceName( type.getSimpleName() );
return (T) Proxy.newProxyInstance(
type.getClassLoader(), new Class[]{type}, handler );
}
But with all this DI functionality in Spring it seems like I should be able to do this all automatically so that I can simply do the following:
#Inject
private MyService service;
With the injection customized in some way that I don't know to create the Proxy behind the scenes without having to call #createProxy.
Any suggestions on a more elegant approach?
Take a look at FactoryBean. You can write your own this way:
public class ServiceProxyFactoryBean implements FactoryBean<Object>
private Class<T> type;
public DutySetFactoryBean(Class<?> type) {
this.type = type;
}
#Override
public synchronized Object getObject() {
JobRpcHandler handler = new JobRpcHandler();
handler.setServiceName(type.getSimpleName());
return Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, handler);
}
#Override
public Class<?> getObjectType() {
return type;
}
#Override
public boolean isSingleton() {
return true;
}
}
and use it in your configuration file:
<bean class="package.name.ServiceProxyFactoryBean">
<constructor-arg>
<value type="java.lang.Class">package.name.MyServiceInterface</value>
</constructor-arg>
</bean>
or, using Java configuration, that way:
#Bean
public ServiceProxyFactoryBean myServiceFactoryBean() {
return new ServiceProxyFactoryBean(MyServiceInterface.class);
}
#Bean
public MyServiceInterface myService() {
return (MyServiceInterface)sessionFactoryBean().getObject();
}
If you want automatically create proxies for all the annotated interfaces in a classpath, you can define your own BeanDefinitionRegistryPostProcessor. Here you must scan your classpath with ResourceLoader using the following pattern:
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
Resource[] resources = patternResolver.getResources(
"classpath*:" + packageName.replace('.', '/') + "/**/*.class");
for (Resource resource : resources) {
MetadataReader reader = metadataReaderFactory.getMetadataReader(resource);
if (!reader.getAnnotationMetadata().isAnnotated(
MyProxyAnnotation.class.getName())) {
continue;
}
Class<?> cls = Class.forName(reader.getClassMetadata().getClassName(), true,
resourceLoader.getClassLoader());
String factoryBeanName = createNewName();
BeanDefinitionBuilder bdb = BeanDefinitionBuilder.genericBeanDefinition(
ServiceProxyFactoryBean.class);
bdb.addConstructorArgValue(cls);
registry.registerBeanDefinition(factoryBeanName, bdb.getBeanDefinition());
bdb = BeanDefinitionBuilder.genericBeanDefinition(cls);
bdb.setFactoryBean(factoryBeanName, "getBean");
registry.registerBeanDefinition(createNewName(), bdb.getBeanDefinition());
}
Now, for all interfaces, annotated with MyProxyAnnotation, you have a proxy, which you can inject into your beans. For example:
#MyProxyAnnotation
public interface MyServiceInterface {
void foo();
}
And
#Component
public class MyBean {
#Autowired
private MyServiceInterface myService;
}
That's all. No configuration needed.
I am not sure this code works or even compiles. It not the final solution, just a general way you should move toward. So you should research and debug a little.
Hey, what is the best way to set a bean's property with Class value ? Regarding XML configuration. For a bean like this :
public class FilterJsonView extends MappingJacksonJsonView {
private Set<String> filteredAttributes;
private Class clazz;
public Set<String> getFilteredAttributes() {
return filteredAttributes;
}
public void setFilteredAttributes(Set<String> filteredAttributes) {
this.filteredAttributes = filteredAttributes;
}
public Class getClazz() {
return clazz;
}
public void setClazz(Class clazz) {
this.clazz = clazz;
}
}
Just inject the class name, and Spring will convert it to a Class object for you, e.g.
<bean class="com.x.y.FilterJsonView">
<property name="clazz" value="com.x.y.SomeClass"/>
</bean>
Just supply the class name. Say you want clazz to be String.class:
<bean id="beanId" class="FilterJsonView">
<property name="clazz" value="java.lang.String"/>
</bean>
Spring has a PropertyEditorSupport implementation called ClassEditor that handles the conversions.