Skip to content

Instantly share code, notes, and snippets.

@mmaravich
Created October 22, 2014 09:12
Show Gist options
  • Save mmaravich/b786014a0e9afd308d1e to your computer and use it in GitHub Desktop.
Save mmaravich/b786014a0e9afd308d1e to your computer and use it in GitHub Desktop.
import java.util.Set;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.stereotype.Component;
@Component
public class CustomConvertersRegistrationBean implements ApplicationListener<ContextRefreshedEvent>, ApplicationContextAware {
private @Autowired Set<Converter<?, ?>> converters;
private @Autowired ConversionService conversionService;
private ApplicationContext rootApplicationContext;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
// Run registration only once - on root context refresh
if (!rootApplicationContext.equals(event.getApplicationContext()))
return;
GenericConversionService gcs = (GenericConversionService) conversionService;
for (Converter<?, ?> converter : converters) {
gcs.addConverter(converter);
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.rootApplicationContext = applicationContext;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment