一、Spring Boot介绍

  Spring Boot可以很容易的创建可直接运行的独立的基于Spring的应用程序。

  功能特点:

  • 创建独立的Spring应用程序;
  • 直接嵌入Tomcat、Jetty等Web容器(不需要部署WAR文件);
  • 提供一些“starter(启动器)”依赖关系来简化构建配置;
  • 自动配置Spring和第三方库;
  • 提供可用于生产的功能,如运行状况检查和外部化配置等;
  • 无代码生成和XML配置要求;

二、Spring Boot快速开始

  1、创建一个maven工程

  2、导入Spring Boot相关的jar包

<!--父工程依赖-->
<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>

  3、编写启动程序

/**
* @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);
}
}

  4、自己写的@Controller @Service等注解标示的组件,必须放在启动类(WebApplication)所在的包及其子包下

  5、运行程序

java -jar spring-boot-web.jar

三、Spring Boot初探

  为什么只引入spring-boot-starter-parent和spring-boot-starter-web就可以快速开发web mvc应用?

  1、pom.xml分析

  spring-boot-web的pom.xml如下:

  进去如下spring-boot-starter-parent的pom.xml:

  进去如下spring-boot-dependencies的pom.xml:

  spring-boot-dependencies其实相当于一个对spring-boot所依赖jar包进行版本管理,所有我们导入依赖默认是不需要写版本的!

  2、spring-boot-starter-web为我项目中导入web开发需要的jar包依赖

四、Spring Boot扩展Spring Mvc配置

  1、添加拦截器

  第一步:创建一个拦截器

/**
* @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("/**");
}
}

  2、增加过滤器

  第一步:创建一个过滤器

/**
* @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);
}
}

  第二步:注册过滤器

/**
* @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;
}
}

  3、添加Servlet

  第一步:创建一个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");
}
}

  运行结果如下:

  4、如何接管Spring Boot的Mvc配置

  使用@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的自动装配组件是必备技能。

Spring Boot系列(一):Spring Boot快速开始的更多相关文章

  1. Spring框架系列(2) - Spring简单例子引入Spring要点

    上文中我们简单介绍了Spring和Spring Framework的组件,那么这些Spring Framework组件是如何配合工作的呢?本文主要承接上文,向你展示Spring Framework组件 ...

  2. Spring框架系列(6) - Spring IOC实现原理详解之IOC体系结构设计

    在对IoC有了初步的认知后,我们开始对IOC的实现原理进行深入理解.本文将帮助你站在设计者的角度去看IOC最顶层的结构设计.@pdai Spring框架系列(6) - Spring IOC实现原理详解 ...

  3. Spring框架系列(7) - Spring IOC实现原理详解之IOC初始化流程

    上文,我们看了IOC设计要点和设计结构:紧接着这篇,我们可以看下源码的实现了:Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的. ...

  4. Spring框架系列(8) - Spring IOC实现原理详解之Bean实例化(生命周期,循环依赖等)

    上文,我们看了IOC设计要点和设计结构:以及Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的:容器中存放的是Bean的定义即Be ...

  5. Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现

    前文,我们分析了Spring IOC的初始化过程和Bean的生命周期等,而Spring AOP也是基于IOC的Bean加载来实现的.本文主要介绍Spring AOP原理解析的切面实现过程(将切面类的所 ...

  6. Spring框架系列(10) - Spring AOP实现原理详解之AOP代理的创建

    上文我们介绍了Spring AOP原理解析的切面实现过程(将切面类的所有切面方法根据使用的注解生成对应Advice,并将Advice连同切入点匹配器和切面类等信息一并封装到Advisor).本文在此基 ...

  7. Spring框架系列(11) - Spring AOP实现原理详解之Cglib代理实现

    我们在前文中已经介绍了SpringAOP的切面实现和创建动态代理的过程,那么动态代理是如何工作的呢?本文主要介绍Cglib动态代理的案例和SpringAOP实现的原理.@pdai Spring框架系列 ...

  8. Spring框架系列(12) - Spring AOP实现原理详解之JDK代理实现

    上文我们学习了SpringAOP Cglib动态代理的实现,本文主要是SpringAOP JDK动态代理的案例和实现部分.@pdai Spring框架系列(12) - Spring AOP实现原理详解 ...

  9. Spring Boot系列(一) Spring Boot准备知识

    本文是学习 Spring Boot 的一些准备知识. Spring Web MVC Spring Web MVC 的两个Context 如下图所示, 基于 Servlet 的 Spring Web M ...

  10. 【Spring Boot&&Spring Cloud系列】Spring Boot初识

    项目代码地址:https://github.com/AndyFlower/Spring-Boot-Learn/tree/master/Spring-boot-helloworld 一.Spring B ...

随机推荐

  1. eclipse GIT本地库分支操作

    git分支是一个重要的知识点,平时我们开发主要结合eclipse,idea来操作,今天这贴主要以eclipse来操作git本地库分支,主要内容包括新建分支,切换分支,合并分支,冲突解决,重命名分支,删 ...

  2. 服务质量分析:腾讯会议&腾讯云Elasticsearch玩出了怎样的新操作?

    导语 | 腾讯会议于2019年12月底上线,两个月内日活突破1000万,被广泛应用于疫情防控会议.远程办公.师生远程授课等场景,为疫情期间的复工复产提供了重要的远程沟通工具.上线100天内,腾讯会议快 ...

  3. [leetcode/lintcode 题解] 一致性哈希 II · Consistent Hashing II

    [题目描述] 在 Consistent Hashing I 中我们介绍了一个比较简单的一致性哈希算法,这个简单的版本有两个缺陷: 增加一台机器之后,数据全部从其中一台机器过来,这一台机器的读负载过大, ...

  4. 羞羞的Python模块包

    目录 一.pip 二.pip常用命令 三.No module 'xxxxx' 四.写在最后   前言 写Python代码的时候,经常会遇到包的问题,但是都是遇到一次,搜索一次,解决了.下一次还是同样的 ...

  5. Shell变量的作用域:Shell全局变量、环境变量和局部变量

    Shell 变量的作用域(Scope),就是 Shell 变量的有效范围(可以使用的范围). 在不同的作用域中,同名的变量不会相互干涉,就好像 A 班有个叫小明的同学,B 班也有个叫小明的同学,虽然他 ...

  6. Spring学习之AOP的实现方式

    Spring学习之AOP的三种实现方式 一.介绍AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能 ...

  7. Vue中数组元素被替换,页面没有动态展示

    原始代码 页面没有相应goodsList替换,打印goodsList数据已经被替换: (借用https://www.cnblogs.com/belongs-to-qinghua/p/11112613. ...

  8. Python列表脚本操作符

    Python列表脚本操作符: len(列表名): 查看列表长度 # 使用 len(列表名) 方法查看列表长度 lst = [1,2,3,4] print(len(lst)) # # 注:嵌套列表算一个 ...

  9. numpy第三方库

    # 导入numpy 并赋予别名 np import numpy as np # 创建数组的常用的几种方式(列表,元组,range,arange,linspace(创建的是等差数组),zeros(全为 ...

  10. 初学用记事本运行java报错:找不到或无法加载主类解决方法,部分出错解决办法

    刚开始学习java的人第一个程序可能通过记事本运行,当中间可能会会出现各种错误,我在下面间可能解决出现的问题 1. java环境变量没有安装好 以我的win10系统为例,在装好相应的jdk后,环境变量 ...