springboot情操陶冶-web配置(二)

直接编写一个Controller层的代码,返回格式为json

package com.example.demo.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** * @author nanco * ------------- * ------------- * @create 2018/9/4 **/ @Controller @RequestMapping("/boot") @ResponseBody public class DemoController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public Map<String, String> helloWorld() { Map<String, String> result = new HashMap<>(); result.put("springboot", "hello world"); return result; } }

运行之后,客户端工具HTTP访问链接:9001/demoWeb/boot/hello便可得到以下的简单结果

{"springboot":"hello world"} 源码剖析

我们都知道springmvc最核心的组件便是DispatcherServlet,其本质是个Servlet组件,也包含了处理前端请求的逻辑,具体的可参照SpringMVC源码情操陶冶-DispatcherServlet。本文则讲解Springboot创建DispatcherServlet以及MVC配置的过程

DispatcherServletAutoConfiguration

首先需要配置DispatcherServlet组件,分为几个步骤来看

No.1 脑头注解了解下

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @Configuration @ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnClass(DispatcherServlet.class) @AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class) @EnableConfigurationProperties(ServerProperties.class) public class DispatcherServletAutoConfiguration { }

由以上的注解可得知,其需要在ServletWebServerFactoryAutoConfiguration类注入至bean工厂后方可继续,这就和前文关联起来了。

No.2 DispatcherServletConfiguration内部类

@Configuration @Conditional(DefaultDispatcherServletCondition.class) @ConditionalOnClass(ServletRegistration.class) @EnableConfigurationProperties(WebMvcProperties.class) protected static class DispatcherServletConfiguration { // 引入了spring.mvc为开头的配置 private final WebMvcProperties webMvcProperties; private final ServerProperties serverProperties; public DispatcherServletConfiguration(WebMvcProperties webMvcProperties, ServerProperties serverProperties) { this.webMvcProperties = webMvcProperties; this.serverProperties = serverProperties; } // 直接创建DispatcherServlet并注入至bean工厂 @Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) public DispatcherServlet dispatcherServlet() { DispatcherServlet dispatcherServlet = new DispatcherServlet(); // 对应spring.mvc.dispatch-options-request dispatcherServlet.setDispatchOptionsRequest( this.webMvcProperties.isDispatchOptionsRequest()); // 对应spring.mvc.dispatch-trace-request dispatcherServlet.setDispatchTraceRequest( this.webMvcProperties.isDispatchTraceRequest()); // 对应spring.mvc.throw-exception-if-no-handler-found dispatcherServlet.setThrowExceptionIfNoHandlerFound( this.webMvcProperties.isThrowExceptionIfNoHandlerFound()); return dispatcherServlet; } // 创建名为multipartResolver的用于文件请求 @Bean @ConditionalOnBean(MultipartResolver.class) @ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) public MultipartResolver multipartResolver(MultipartResolver resolver) { // Detect if the user has created a MultipartResolver but named it incorrectly return resolver; } // 获取server.servlet.path表明DispatcherServlet的拦截路径 @Bean public DispatcherServletPathProvider mainDispatcherServletPathProvider() { return () -> DispatcherServletConfiguration.this.serverProperties.getServlet() .getPath(); } }

很简单,就是创建了DispatcherServlet,那么如何被注入至tomcat的servlet集合中呢

N0.3 DispatcherServletRegistrationConfiguration内部类

@Configuration @Conditional(DispatcherServletRegistrationCondition.class) @ConditionalOnClass(ServletRegistration.class) @EnableConfigurationProperties(WebMvcProperties.class) @Import(DispatcherServletConfiguration.class) protected static class DispatcherServletRegistrationConfiguration { private final ServerProperties serverProperties; private final WebMvcProperties webMvcProperties; private final MultipartConfigElement multipartConfig; public DispatcherServletRegistrationConfiguration( ServerProperties serverProperties, WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfigProvider) { this.serverProperties = serverProperties; this.webMvcProperties = webMvcProperties; this.multipartConfig = multipartConfigProvider.getIfAvailable(); } // 对DispatcherServlet注入至tomcat等容器中 @Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME) @ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) public ServletRegistrationBean<DispatcherServlet> dispatcherServletRegistration( DispatcherServlet dispatcherServlet) { // 同server.servlet.path,默认为/ ServletRegistrationBean<DispatcherServlet> registration = new ServletRegistrationBean<>( dispatcherServlet, this.serverProperties.getServlet().getServletMapping()); registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); // 读取spring.mvc.servlet.load-on-startup,默认为-1 registration.setLoadOnStartup( this.webMvcProperties.getServlet().getLoadOnStartup()); if (this.multipartConfig != null) { registration.setMultipartConfig(this.multipartConfig); } return registration; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wppzdz.html