001/* 002 * Copyright 2002-2017 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; 027import org.springframework.lang.Nullable; 028 029/** 030 * A factory providing convenient access to a ConversionService configured with 031 * converters appropriate for most environments. Set the 032 * {@link #setConverters "converters"} property to supplement the default converters. 033 * 034 * <p>This implementation creates a {@link DefaultConversionService}. 035 * Subclasses may override {@link #createConversionService()} in order to return 036 * a {@link GenericConversionService} instance of their choosing. 037 * 038 * <p>Like all {@code FactoryBean} implementations, this class is suitable for 039 * use when configuring a Spring application context using Spring {@code <beans>} 040 * XML. When configuring the container with 041 * {@link org.springframework.context.annotation.Configuration @Configuration} 042 * classes, simply instantiate, configure and return the appropriate 043 * {@code ConversionService} object from a {@link 044 * org.springframework.context.annotation.Bean @Bean} method. 045 * 046 * @author Keith Donald 047 * @author Juergen Hoeller 048 * @author Chris Beams 049 * @since 3.0 050 */ 051public class ConversionServiceFactoryBean implements FactoryBean<ConversionService>, InitializingBean { 052 053 @Nullable 054 private Set<?> converters; 055 056 @Nullable 057 private GenericConversionService conversionService; 058 059 060 /** 061 * Configure the set of custom converter objects that should be added: 062 * implementing {@link org.springframework.core.convert.converter.Converter}, 063 * {@link org.springframework.core.convert.converter.ConverterFactory}, 064 * or {@link org.springframework.core.convert.converter.GenericConverter}. 065 */ 066 public void setConverters(Set<?> converters) { 067 this.converters = converters; 068 } 069 070 @Override 071 public void afterPropertiesSet() { 072 this.conversionService = createConversionService(); 073 ConversionServiceFactory.registerConverters(this.converters, this.conversionService); 074 } 075 076 /** 077 * Create the ConversionService instance returned by this factory bean. 078 * <p>Creates a simple {@link GenericConversionService} instance by default. 079 * Subclasses may override to customize the ConversionService instance that 080 * gets created. 081 */ 082 protected GenericConversionService createConversionService() { 083 return new DefaultConversionService(); 084 } 085 086 087 // implementing FactoryBean 088 089 @Override 090 @Nullable 091 public ConversionService getObject() { 092 return this.conversionService; 093 } 094 095 @Override 096 public Class<? extends ConversionService> getObjectType() { 097 return GenericConversionService.class; 098 } 099 100 @Override 101 public boolean isSingleton() { 102 return true; 103 } 104 105}