关于自动配置Formatter
和Converter
,可以看下WebMvcAutoConfiguration
类中的定义:
@Override
public void addFormatters(FormatterRegistry registry) {
for (Converter<?, ?> converter : getBeansOfType(Converter.class)) {
registry.addConverter(converter);
}
for (GenericConverter converter : getBeansOfType(GenericConverter.class)) {
registry.addConverter(converter);
}
for (Formatter<?> formatter : getBeansOfType(Formatter.class)) {
registry.addFormatter(formatter);
}
}
private <T> Collection<T> getBeansOfType(Class<T> type) {
return this.beanFactory.getBeansOfType(type).values();
}
从代码中可以看出,只要定义了Convert
,GenericConvert
和Formatter
接口的实现类的Bean,这些Bean
就会自动注册到Spring MVC
中。
公众号ID:longjiazuoA

未经允许不得转载:人生设计师 » Spring Boot的Web配置(四):自动配置的Formatter和Converter