HttpEncodingAutoConfiguration解读
先整体看一下HttpEncodingAutoConfiguration的源码:
//表明该类为一个配置类
@Configuration
//启动指定类的ConfigurationProperties功能,将配置文件中对应的值和HttpProperties绑定起来,并将其加入到ioc容器中
@EnableConfigurationProperties(HttpProperties.class)
//Spring底层@Conditional注解(Spring注解开发(一)组件注册),根据不同的条件,
//如果满足指定的条件,整个配置类里面的配置就会生效。判断当前应用是否是web应用,如果是,当前配置类生效
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
//判断当前项目有没有这个类CharacterEncodingFilter(SpringMVC中进行乱码解决的过滤器)
@ConditionalOnClass(CharacterEncodingFilter.class)
//判断配置文件中是否存在配置-spring.http.encoding.enabled,如果不存在,判断也是成立的。
//即配置文件中不配置pring.http.encoding.enabled=true,此项设置为默认生效
@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)
public class HttpEncodingAutoConfiguration {
//和SpringBoot的配置文件映射并保存到HttpProperties.Encoding中
private final HttpProperties.Encoding properties;
//只有一个有参构造器的情况下,参数的值就会从容器中拿
public HttpEncodingAutoConfiguration(HttpProperties properties) {
this.properties = properties.getEncoding();
}
//给容器中添加一个组件,这个组件的某些值从properties中获取
@Bean
//判断容器中有没有这个组件
@ConditionalOnMissingBean
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name());
filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
return filter;
}
//添加组件
@Bean
public LocaleCharsetMappingsCustomizer localeCharsetMappingsCustomizer() {
return new LocaleCharsetMappingsCustomizer(this.properties);
}
private static class LocaleCharsetMappingsCustomizer
implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>, Ordered {
private final HttpProperties.Encoding properties;
LocaleCharsetMappingsCustomizer(HttpProperties.Encoding properties) {
this.properties = properties;
}
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
if (this.properties.getMapping() != null) {
factory.setLocaleCharsetMappings(this.properties.getMapping());
}
}
@Override
public int getOrder() {
return 0;
}
}
}
@EnableConfigurationProperties(HttpProperties.class)是怎么将参数绑定的
点进该注解源码:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EnableConfigurationPropertiesImportSelector.class)
public @interface EnableConfigurationProperties {
....
}
该注解为容器中导入了EnableConfigurationPropertiesImportSelector类,继续向下看:
class EnableConfigurationPropertiesImportSelector implements ImportSelector {
private static final String[] IMPORTS = { ConfigurationPropertiesBeanRegistrar.class.getName(),
ConfigurationPropertiesBindingPostProcessorRegistrar.class.getName() };
@Override
public String[] selectImports(AnnotationMetadata metadata) {
return IMPORTS;
}
EnableConfigurationPropertiesImportSelector向容器中导入了ConfigurationPropertiesBeanRegistrar和ConfigurationPropertiesBindingPostProcessorRegistrar两个组件。
public class ConfigurationPropertiesBindingPostProcessor
implements BeanPostProcessor, PriorityOrdered, ApplicationContextAware, InitializingBean {
...
}
ConfigurationPropertiesBindingPostProcessor实现了BeanPostProcessor接口,在Bean创建的时候将会去执行实现方法。
在HttpProperties方法上有注解@ConfigurationProperties,当Bean被创建时(只有一个有参构造器的情况下,默认会注入该属性,即HttpProperties会被创建并添加到容器中)则会执行下面的方法:
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
ConfigurationProperties annotation = getAnnotation(bean, beanName, ConfigurationProperties.class);
if (annotation != null) {
bind(bean, beanName, annotation);
}
return bean;
}
此处执行blind方法将保存在容器中的配置文件中的值和相关的properties类绑定到一起。