001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.context.support; 018 019import java.util.Set; 020 021import org.springframework.beans.factory.FactoryBean; 022import org.springframework.beans.factory.InitializingBean; 023import org.springframework.core.convert.ConversionService; 024import org.springframework.core.convert.support.ConversionServiceFactory; 025import org.springframework.core.convert.support.DefaultConversionService; 026import org.springframework.core.convert.support.GenericConversionService; 027 028/** 029 * A factory providing convenient access to a ConversionService configured with 030 * converters appropriate for most environments. Set the 031 * {@link #setConverters "converters"} property to supplement the default converters. 032 * 033 * <p>This implementation creates a {@link DefaultConversionService}. 034 * Subclasses may override {@link #createConversionService()} in order to return 035 * a {@link GenericConversionService} instance of their choosing. 036 * 037 * <p>Like all {@code FactoryBean} implementations, this class is suitable for 038 * use when configuring a Spring application context using Spring {@code <beans>} 039 * XML. When configuring the container with 040 * {@link org.springframework.context.annotation.Configuration @Configuration} 041 * classes, simply instantiate, configure and return the appropriate 042 * {@code ConversionService} object from a {@link 043 * org.springframework.context.annotation.Bean @Bean} method. 044 * 045 * @author Keith Donald 046 * @author Juergen Hoeller 047 * @author Chris Beams 048 * @since 3.0 049 */ 050public class ConversionServiceFactoryBean implements FactoryBean<ConversionService>, InitializingBean { 051 052 private Set<?> converters; 053 054 private GenericConversionService conversionService; 055 056 057 /** 058 * Configure the set of custom converter objects that should be added: 059 * implementing {@link org.springframework.core.convert.converter.Converter}, 060 * {@link org.springframework.core.convert.converter.ConverterFactory}, 061 * or {@link org.springframework.core.convert.converter.GenericConverter}. 062 */ 063 public void setConverters(Set<?> converters) { 064 this.converters = converters; 065 } 066 067 @Override 068 public void afterPropertiesSet() { 069 this.conversionService = createConversionService(); 070 ConversionServiceFactory.registerConverters(this.converters, this.conversionService); 071 } 072 073 /** 074 * Create the ConversionService instance returned by this factory bean. 075 * <p>Creates a simple {@link GenericConversionService} instance by default. 076 * Subclasses may override to customize the ConversionService instance that 077 * gets created. 078 */ 079 protected GenericConversionService createConversionService() { 080 return new DefaultConversionService(); 081 } 082 083 084 // implementing FactoryBean 085 086 @Override 087 public ConversionService getObject() { 088 return this.conversionService; 089 } 090 091 @Override 092 public Class<? extends ConversionService> getObjectType() { 093 return GenericConversionService.class; 094 } 095 096 @Override 097 public boolean isSingleton() { 098 return true; 099 } 100 101}