Spring ConversionService adding Converters

后端 未结 9 778
别那么骄傲
别那么骄傲 2020-12-31 03:47

I searched for the below problem, but couldn\'t find an answer.

I want to use spring\'s conversion service by writing my custom converter that implements org.spri

9条回答
  •  攒了一身酷
    2020-12-31 04:39

    With spring > 4 it is no more necessary to implement an own derivation of the ConversionService. Initialize it in a @Configuration annotated class as follows:

    @Configuration
    public class ConversionServiceProvider
    {
        @Autowired
        private MyConverterImpl myConverter;
    
        @Bean
        public ConversionService getConversionService()
        {
            ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
            bean.setConverters( getConverters() );
            bean.afterPropertiesSet();
            ConversionService object = bean.getObject();
            return object;
        }
    
        private Set> getConverters()
        {
            Set> converters = new HashSet>();
    
            converters.add( myConverter );
            // add here more custom converters, either as spring bean references or directly instantiated
    
            return converters;
        }
    }
    

提交回复
热议问题