Created
October 22, 2014 09:12
-
-
Save mmaravich/b786014a0e9afd308d1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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