Spring Boot能夠很容易的建立可直接運行的獨立的基於Spring的應用程序。java
<!--父工程依賴--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent> <dependencies> <!-- web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <finalName>spring-boot-web</finalName> <plugins> <!--打包fat jar,引入該插件,能夠幫助咱們將web應用程序打成可執行jar包--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
/** * @desc: spring boot 啓動類 * @author: toby * @date: 2019/7/17 23:03 */ @SpringBootApplication public class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } }
java -jar spring-boot-web.jar
爲何只引入spring-boot-starter-parent和spring-boot-starter-web就能夠快速開發web mvc應用?web
spring-boot-web的pom.xml以下:spring
進去以下spring-boot-starter-parent的pom.xml:mvc
進去以下spring-boot-dependencies的pom.xml:maven
spring-boot-dependencies其實至關於一個對spring-boot所依賴jar包進行版本管理,全部咱們導入依賴默認是不須要寫版本的!ide
第一步:建立一個攔截器spring-boot
/** * @desc: 建立一個攔截器 * @author: toby */ @Slf4j public class TobyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { log.info("TobyInterceptor的preHandle方法"); return false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { log.info("TobyInterceptor的postHandle方法"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { log.info("TobyInterceptor的afterCompletion方法"); } }
第二步:註冊攔截器微服務
/** * @desc: WebMvc配置 * @author: toby */ @Configuration public class TobyWebMvcConfig implements WebMvcConfigurer { /** * 註冊攔截器 * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new TobyInterceptor()).addPathPatterns("/**"); } }
第一步:建立一個過濾器post
/** * @desc: 建立一個過濾器 * @author: toby */ @Slf4j
@Component public class TobyFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("TobyFilter的doFilter方法"); filterChain.doFilter(servletRequest,servletResponse); } }
第二步:註冊過濾器ui
/** * @desc: WebMvc配置 * @author: toby */ @Configuration public class TobyWebMvcConfig implements WebMvcConfigurer { @Bean public FilterRegistrationBean<TobyFilter> filterFilterRegistrationBean(TobyFilter tobyFilter){ FilterRegistrationBean<TobyFilter> filterFilterRegistrationBean = new FilterRegistrationBean<>(); List<String> uriList = new ArrayList<>(1); uriList.add("/**"); filterFilterRegistrationBean.setFilter(tobyFilter); filterFilterRegistrationBean.setEnabled(true); filterFilterRegistrationBean.setUrlPatterns(uriList); filterFilterRegistrationBean.setName("tobyFilter"); filterFilterRegistrationBean.setOrder(1); return filterFilterRegistrationBean; } }
第一步:建立一個Servlet
/** * @desc: 建立一個Servlet * @author: toby */ @Slf4j @Component public class TobyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("TobyServlet的doPost方法"); } }
第二步:註冊Servlet
/** * @desc: WebMvc配置 * @author: toby */ @Configuration public class TobyWebMvcConfig implements WebMvcConfigurer { /** * 註冊Servlet * @param tobyFilter * @return */ @Bean public ServletRegistrationBean servletRegistrationBean(TobyFilter tobyFilter){ return new ServletRegistrationBean(new TobyServlet(), "/servlet"); } }
運行結果以下:
使用@EnableWebMvc註解(不推薦使用)
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented
//導入了DelegatingWebMvcConfiguration的組件 @Import(DelegatingWebMvcConfiguration.class) public @interface EnableWebMvc { }
① DelegatingWebMvcConfiguration的繼承圖
② 再看下WebMvc的自動配置類WebMvcAutoConfiguration
@Configuration @ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
//容器中沒有WebMvcConfigurationSupport該配置文件才生生效,可是咱們使用了@EnableWebMvc導入了WebMvcConfiurationSupport,它只保證了Spring Mvc的最基本的功能 @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10) @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class }) public class WebMvcAutoConfiguration {
本文主要介紹了Spring Boot的功能特性,如何快速開始一個Spring Boot項目,以及如何擴展Spring Mvc配置,好比如何添加本身的攔截器,過濾器,和Servlet。Spring Boot是微服務的開發利器,因此要對微服務組件有深刻了解,Spring Boot的自動裝配組件是必備技能。