SpringBoot中SpringMvc自动配置原理:深入源码的理解
第一部分:案例解析
全面扩展 SpringMvc结论:
扩展SpringMvc 的学习 SpringMvc配置原理
如果想自定义自定义的功能 只需要写组件 交给SpringBoot 自动装备的原理
如果想要将SpringMvc 要扩展
@EnableWebMvc
public interface ViewResolver { 视图解析器的类吧他看做视图解析器
扩展SpringMvc
@EnableWebMvc 导入了一个类 所以得出结论:
所有的WebMvcConfiguration都会被作用,不止Spring自己的配置类,我们自己的配置类当然也会被调用:
第一步 自己定义一个类: MyMvcConfig 接上 WebMvcConfigurer
public class MyMvcConfig implements WebMvcConfigurer {}
第二步:自己定义一个静态内部类 ViewResolver 自定义一个视图显示器
public static class MyViewResolver implements ViewResolver{}
第三步:自定义一个视图显示器
public static class MyViewResolver implements ViewResolver{ //自定义一个视图显示器 @Bean //放入bean中 public ViewResolver myViewResolver(){ return new MyViewResolver(); } @Override public View resolveViewName(String viewName, Locale locale) throws Exception { return null; }
第四步:视图跳转的解析器
@Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/kunghu").setViewName("test"); }
第五步:控制层的控制器
package com.spring.springboot0907web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.Arrays; //在templates下的只能controller //需要模板引擎 thymeleof @Controller public class IndexController { @RequestMapping("/test") public String index(Model model){ model.addAttribute("msg","<h2>Hellow Mode SpringBoot</h2>"); model.addAttribute("msg","<h1>Hellow Mode SpringBoot</h1>"); model.addAttribute("users", Arrays.asList("小明","小王","消费")); model.addAttribute("dogs",Arrays.asList("效果","小明","王大陆","王晓蓉")); return "test"; } }
package com.spring.springboot0907web; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //深入源码 @SpringBootApplication public class Springboot0907WebApplication { public static void main(String[] args) { //调用 SpringApplication.run 来启用 SpringApplication.run(Springboot0907WebApplication.class, args); } }
运行结果分析
第二部分:@EnableWebMvc源码解读
按住ctrl键点击进去
低昂就进入上面的类
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport { private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite(); public DelegatingWebMvcConfiguration() { } }
@Autowired( required = false ) public void setConfigurers(List<WebMvcConfigurer> configurers) { //从容器中获取所有的webmvcConfigurer if (!CollectionUtils.isEmpty(configurers)) { this.configurers.addWebMvcConfigurers(configurers); } }
在项目中找到WebMvcAutoConfiguration
@AutoConfiguration( after = {DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class} ) @ConditionalOnWebApplication( type = Type.SERVLET ) @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class}) @ConditionalOnMissingBean({WebMvcConfigurationSupport.class}) @AutoConfigureOrder(-2147483638) public class WebMvcAutoConfiguration { public static final String DEFAULT_PREFIX = ""; public static final String DEFAULT_SUFFIX = ""; public static final PathPatternParser pathPatternParser = new PathPatternParser(); private static final String SERVLET_LOCATION = "/"; public WebMvcAutoConfiguration() { } }
@Configuration( proxyBeanMethods = false ) @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class}) @EnableConfigurationProperties({WebMvcProperties.class, WebProperties.class}) @Order(0) public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware { private static final Log logger = LogFactory.getLog(WebMvcConfigurer.class); private final Resources resourceProperties; private final WebMvcProperties mvcProperties; private final ListableBeanFactory beanFactory; private final ObjectProvider<HttpMessageConverters> messageConvertersProvider; private final ObjectProvider<DispatcherServletPath> dispatcherServletPath; private final ObjectProvider<ServletRegistrationBean<?>> servletRegistrations; private final WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer; private ServletContext servletContext;
https://blog.csdn.net/qq_56248592/article/details/127142599?spm=1001.2014.3001.5501