精髓:
1)、SpringBoot启动会加载大量的自动配置类
2)、我们看我们需要的功能有没有SpringBoot默认写好的自动配置类;
3)、我们再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件有,我们就不需要再来配置了)
4)、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们就可以在配置文件中指定这 些属性的值;
xxxxAutoConfigurartion:自动配置类;
给容器中添加组件
//只有一个有参构造器的情况下,参数的值就会从容器中拿
public HttpEncodingAutoConfiguration(HttpEncodingProperties properties)
{
this.properties = properties;
}
@Bean //给容器中添加一个组件,这个组件的某些值需要从properties中获取
SpringMVC自动配置
- Spring MVC auto-configuration Spring Boot 自动配置好了SpringMVC
- 以下是SpringBoot对SpringMVC的默认配置:(WebMvcAutoConfiguration)
- Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans. 自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View),视图对象决定如何 渲染(转发?重定向?))
- ContentNegotiatingViewResolver:组合所有的视图解析器的; 如何定制:我们可以自己给容器中添加一个视图解析器;自动的将其组合进来;
- Support for serving static resources, including support for WebJars (see below).静态资源文件夹路 径,webjars
- Static index.html support. 静态首页访问
- Custom Favicon support (see below). favicon.ico
- 自动注册了 of Converter , GenericConverter , Formatter beans.
- Converter:转换器; public String hello(User user):
- 类型转换使用Converter Formatter 格式化器; 2017.12.17===Date;
扩展SpringMVC
springmvc.xml:<mvc:view‐controller path="/hello" view‐name=“success”/>
mvc:interceptors
mvc:interceptor
<mvc:mapping path="/hello"/>
bean>
/bean>
</mvc:interceptor>
</mvc:interceptors>
springboot:
编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型;不能标注@EnableWebMvc; 既保留了所有的自动配置,也能用我们扩展的配置;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// TODO Auto-generated method stub
registry.addViewController("/Dentmon2").setViewName("success");
}
}