forked from DerekYRC/mini-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
342 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
61 changes: 61 additions & 0 deletions
61
src/main/java/org/springframework/context/support/ConversionServiceFactoryBean.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.springframework.context.support; | ||
|
||
import org.springframework.beans.factory.FactoryBean; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.core.convert.ConversionService; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.core.convert.converter.ConverterFactory; | ||
import org.springframework.core.convert.converter.ConverterRegistry; | ||
import org.springframework.core.convert.converter.GenericConverter; | ||
import org.springframework.core.convert.support.DefaultConversionService; | ||
import org.springframework.core.convert.support.GenericConversionService; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* @author derekyi | ||
* @date 2021/1/17 | ||
*/ | ||
public class ConversionServiceFactoryBean implements FactoryBean<ConversionService>, InitializingBean { | ||
|
||
private Set<?> converters; | ||
|
||
private GenericConversionService conversionService; | ||
|
||
@Override | ||
public void afterPropertiesSet() throws Exception { | ||
conversionService = new DefaultConversionService(); | ||
registerConverters(converters, conversionService); | ||
} | ||
|
||
private void registerConverters(Set<?> converters, ConverterRegistry registry) { | ||
if (converters != null) { | ||
for (Object converter : converters) { | ||
if (converter instanceof GenericConverter) { | ||
registry.addConverter((GenericConverter) converter); | ||
} else if (converter instanceof Converter<?, ?>) { | ||
registry.addConverter((Converter<?, ?>) converter); | ||
} else if (converter instanceof ConverterFactory<?, ?>) { | ||
registry.addConverterFactory((ConverterFactory<?, ?>) converter); | ||
} else { | ||
throw new IllegalArgumentException("Each converter object must implement one of the " + | ||
"Converter, ConverterFactory, or GenericConverter interfaces"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public ConversionService getObject() throws Exception { | ||
return conversionService; | ||
} | ||
|
||
@Override | ||
public boolean isSingleton() { | ||
return true; | ||
} | ||
|
||
public void setConverters(Set<?> converters) { | ||
this.converters = converters; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.