A, SpringMVC automatic configuration
SpringBoot automatically configured the SpringMVC. WebMvcAutoConfiguration disposed in a respective class. Official website: https://docs.spring.io/spring/docs/4.3.14.RELEASE/spring-framework-reference/htmlsingle#mvc. The following is the default configuration for SpringMVC of SpringBoot:
1, in the Inclusion of ContentNegotiatingViewResolver
and BeanNameViewResolver
Beans. Automatically configured ViewResolver (view resolver: Return worth to view objects (View) method according to decide how to render the view object (forward or redirect)).
2, ContentNegotiatingViewResolver: a combination of all of the ViewResolver; how to customize: We can add yourself to a view resolver container; automatic will come in its portfolio.
@SpringBootApplication
public class SpringBoot04WebRestfulcrudApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot04WebRestfulcrudApplication.class, args);
}
// 自定义视图解析器
@Bean
public ViewResolver myViewReolver(){
return new MyViewResolver();
}
public static class MyViewResolver implements ViewResolver{
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
return null;
}
}
}
3, Support for serving static resources, including support for WebJars (see below). Static resource folder path, webjars
4, Static index.html
Support static home visit.
. 5, the Custom Favicon
Support (See below). favicon.ico
6, the automatic registration of Converter
, GenericConverter
, Formatter
Beans. Converter: type converter; Formatter
formatter; as: 2017.12.17 converted to the Date.
@Bean
@ConditionalOnProperty(prefix = "spring.mvc", name = "date-format") //在文件中配置日期格式化的规则
public Formatter<Date> dateFormatter() {
return new DateFormatter(this.mvcProperties.getDateFormat()); //日期格式化组件
}
Add your own formatter converter, we just need to put the container.
7, Support for HttpMessageConverters
(See below). HttpMessageConverter: SpringMVC for converting the Http request and response; HttpMessageConverters
it is determined from the container; get all HttpMessageConverter. HttpMessageConverter to add your own container, the container only need to register your own components.
. 8, the Automatic Registration of MessageCodesResolver
(See below). Custom Error code generation rules.
. 9, the Automatic A use of ConfigurableWebBindingInitializer
the bean (See below). It is used to initialize WebDataBinder, such as: the request data is bound to the JavaBean; we can configure a ConfigurableWebBindingInitializer and added to the vessel to replace the default.
org.springframework.boot.autoconfigure.web
: Web all automatic scene.
Second, the expansion SpringMVC
<mvc:view-controller path="/hello" view-name="success"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean></bean>
</mvc:interceptor>
</mvc:interceptors>
Write a configuration class (@Configuration), it is WebMvcConfigurerAdapter type; can not be marked @EnableWebMvc. Not only retains all the automatic configuration, you can also use our expanded configuration;
//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
//浏览器发送 /atguigu 请求来到 success
registry.addViewController("/atguigu").setViewName("success");
}
}
Principle:
1, WebMvcAutoConfiguration is SpringMVC automatic configuration class.
2, @ Import (EnableWebMvcConfiguration.class). In doing other auto-configuration will be imported EnableWebMvcConfiguration.
@Configuration
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
//从容器中获取所有的WebMvcConfigurer
@Autowired(required = false)
public void setConfigurers(List<WebMvcConfigurer> configurers) {
if (!CollectionUtils.isEmpty(configurers)) {
this.configurers.addWebMvcConfigurers(configurers);
//一个参考实现;将所有的WebMvcConfigurer相关配置都来一起调用;
@Override
// public void addViewControllers(ViewControllerRegistry registry) {
// for (WebMvcConfigurer delegate : this.delegates) {
// delegate.addViewControllers(registry);
// }
}
}
}
3, all function with the container are WebMvcConfigurer.
4, our configuration class will be called; SpringMVC our auto-configuration and extended configuration will work.
Third, to take full control SpringMVC
SpringBoot automatic configuration of SpringMVC becomes ineffective, all of us need to configure their own; you can just add @EnableWebMvc configuration class.
//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
//浏览器发送 /atguigu 请求来到 success
registry.addViewController("/atguigu").setViewName("success");
}
}
: The principle
reason for adding @EnableWebMvc comment on the failure of the automatic configuration:
1, the core of the @ EnableWebMvc.
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
2, there is an automatic annotation configuration class WebMvcAutoConfiguration: @ConditionalOnMissingBean (WebMvcConfigurationSupport.class). This means that when there is no container assembly (WebMvcConfigurationSuppor) when the auto-configuration class (WebMvcAutoConfiguration) to take effect.
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,WebMvcConfigurerAdapter.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class) //容器中没有这个组件的时候,这个自动配置类才生效
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
3, @ EnableWebMvc WebMvcConfigurationSupport assembly will come introduced.
4, imported WebMvcConfigurationSupport SpringMVC only the most basic functions.
Fourth, modify the default configuration SpringBoot
1, SpringBoot many components in the auto-configuration when there is no container look at the user's own configuration (@ Bean, @ Component) If you have to use the user-configured, and if not, it auto-configuration; If some components can have multiple (ViewResolver) and the combination of its own default user profile is up.
2, in SpringBoot there will be a lot of xxxConfigurer help us expand configuration.
3, there will be a lot of xxxCustomizer help us to customize the configuration SpringBoot.