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;
}
}