1 简介

  • SpringBoot 简化 Spring 应用开发,约定大于配置,去繁从简,just run 就能创建一个独立的、产品级别的应用。
  • 背景:J2EE 笨重的开发、繁多的配置、低下的开发效率、复杂的部署流程、第三方技术集成难度大。
  • 解决:
    • “Spring全家桶”时代。
    • SpringBoot —> J2EE一站式解决方案。
    • SpringCloud —> 分布式整体解决方案。
  • 优点:
    • 快速创建独立运行的 Spring 项目以及与主流框架集成。
    • 使用嵌入式的 Servlet 容器,应用无需打成 war 包。
    • starters 自动依赖和版本控制。
    • 大量的自动配置,简化开发,也可修改默认值。
    • 无需配置 XML ,无代码生成,开箱即用。
    • 准生产环境的运行时应用监控。
    • 和云计算的天然集成。

2 微服务

  • 2014 年,Martin Flower。
  • 微服务,是一种架构风格。
  • 一个应用应该是一组小型服务。可以通过 HTTP 的方式进行互通。
  • 每一个功能元素最终都是一个可独立替换和独立升级的软件单元。

3 入门案例

3.1 环境准备

  • JDK 1.8
  • Maven 3.x
  • IDEA 最新版
  • SpringBoot 1.5.9.RELEASE

  • maven 设置 JDK 的版本:

  • ① 在 Maven 的 conf/settings.xml 中的 profiles 节点中添加
  1. <profile>
  2. <id>jdk-1.8</id>
  3. <activation>
  4. <activeByDefault>true</activeByDefault>
  5. <jdk>1.8</jdk>
  6. </activation>
  7. <properties>
  8. <maven.compiler.source>1.8</maven.compiler.source>
  9. <maven.compiler.target>1.8</maven.compiler.target>
  10. <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
  11. </properties>
  12. </profile>
  • ② 在 Maven 构建的项目的 pom.xml 中的 <build></build> 节点的 <plugins></plugins> 添加
  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-compiler-plugin</artifactId>
  4. <version>2.3.2</version>
  5. <configuration>
  6. <source>1.8</source>
  7. <target>1.8</target>
  8. <encoding>utf-8</encoding>
  9. </configuration>
  10. </plugin>

3.2 SpringBoot HelloWorld

3.2.1 功能需求

  • 浏览器发送 hello 请求,服务器接收请求并处理,响应输出 Hello World 字符串。

3.2.2 创建 Maven 工程(jar)

  • 略。

3.2.3 导入 SpringBoot 的相关依赖 jar 包的 Maven 坐标

  • pom.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>1.5.9.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.sunxiaping</groupId>
  12. <artifactId>springboot</artifactId>
  13. <version>1.0</version>
  14. <name>springboot</name>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-test</artifactId>
  23. <scope>test</scope>
  24. <exclusions>
  25. <exclusion>
  26. <groupId>org.junit.vintage</groupId>
  27. <artifactId>junit-vintage-engine</artifactId>
  28. </exclusion>
  29. </exclusions>
  30. </dependency>
  31. </dependencies>
  32. <build>
  33. <plugins>
  34. <plugin>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-maven-plugin</artifactId>
  37. </plugin>
  38. <plugin>
  39. <groupId>org.apache.maven.plugins</groupId>
  40. <artifactId>maven-compiler-plugin</artifactId>
  41. <version>2.3.2</version>
  42. <configuration>
  43. <source>1.8</source>
  44. <target>1.8</target>
  45. <encoding>utf-8</encoding>
  46. </configuration>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>

3.2.4 编写主程序

  • SpringBootApplication.java
  1. package com.sunxiaping.springboot;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * 主程序,用来启动SpringBoot应用
  6. */
  7. @SpringBootApplication
  8. public class HelloWorldAApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(HelloWorldAApplication.class, args);
  11. }
  12. }

3.2.5 编写相关的 Controller

  • HelloController.java
  1. package com.sunxiaping.springboot.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class HelloWorld {
  6. @RequestMapping(value = "/hello")
  7. public String hello(){
  8. return "Hello World";
  9. }
  10. }

3.2.6 运行测试

  • 只需要运行 SpringbootApplication.java 即可。

3.2.7 简化部署

  • 在 pom.xml 中导入插件,用于将 SpringBoot 应用打包成一个 jar 包:
  1. <build>
  2. <plugins>
  3. <!-- 可以将应用打包成一个可执行的jar包 -->
  4. <plugin>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-maven-plugin</artifactId>
  7. </plugin>
  8. </plugins>
  9. </build>
  • 将这个应用打成 jar 包,直接使用 java -jar 的命令进行执行。

4 HelloWorld 探究

4.1 pom 文件

4.1.1 父项目

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.9.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. 它的父项目是
  8. <parent>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-dependencies</artifactId>
  11. <version>1.5.9.RELEASE</version>
  12. <relativePath>../../spring-boot-dependencies</relativePath>
  13. </parent>
  14. 它是真正管理 SpringBoot 应用里面的所有依赖版本

可以将上面的父项目称为 SpringBoot 的版本仲裁中心。 所以我们导入依赖默认是不需要写版本号的,前提是版本仲裁中心里面有。

4.1.2 启动器

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  • spring-boot-starter:称为场景启动器。
  • spring-boot-starter-web:帮我们导入 web 模块正常运行所依赖的组件。
  • SpringBoot 将所有的功能场景都抽取出来,做成一个个的 starters(启动器),只需要在项目中引入这些 starter ,其相关场景的所有依赖都会导入进来。需要什么功能就导入什么场景的启动器。

4.2 主程序类(主入口类)

  1. package com.sunxiaping.springboot;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * 主程序,用来启动 SpringBoot 应用
  6. */
  7. @SpringBootApplication
  8. public class HelloWorldAApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(HelloWorldAApplication.class, args);
  11. }
  12. }
  • @SpringBootApplication:SpringBoot 应用标注在某个类上,说明这个类是 SpringBoot 的主配置类,SpringBoot 就应该运行这个类的 main 方法来启动 SpringBoot 应用。
  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Inherited
  5. @SpringBootConfiguration
  6. @EnableAutoConfiguration
  7. @ComponentScan(excludeFilters = {
  8. @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  9. @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
  10. public @interface SpringBootApplication {
  11. //略
  12. }
  • @SpringBootConfiguration 注解:
    • 源码如下: ```java @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration {

}

  1. - SpringBoot 的配置类。
  2. - 标注在某个类上,表示这是一个 Spring Boot 的配置类。
  3. - 实际就是 @Configuration 注解,@Configuration 注解标注在某个类上,那么这个类就是配置类,配置类也是容器中一个组件。
  4. - @EnableAutoConfiguration 注解:
  5. - 源码如下:
  6. ```java
  7. @Target(ElementType.TYPE)
  8. @Retention(RetentionPolicy.RUNTIME)
  9. @Documented
  10. @Inherited
  11. @AutoConfigurationPackage
  12. @Import(EnableAutoConfigurationImportSelector.class)
  13. public @interface EnableAutoConfiguration {
  14. String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
  15. /**
  16. * Exclude specific auto-configuration classes such that they will never be applied.
  17. * @return the classes to exclude
  18. */
  19. Class<?>[] exclude() default {};
  20. /**
  21. * Exclude specific auto-configuration class names such that they will never be
  22. * applied.
  23. * @return the class names to exclude
  24. * @since 1.3.0
  25. */
  26. String[] excludeName() default {};
  27. }
  • 开启自动配置功能。
  • @Import(EnableAutoConfigurationImportSelector.class):导入那些组件的选择器。

导入自动配置类.png

  1. - 会给容器中导入非常多的自动配置类(xxxAutoConfiguration)。
  2. - 自动配置类的作用:就是给容器中导入这个场景需要的所有组件,并配置好这些组件。
  3. - 有了自动配置类,就免去了我们手动编写配置注入功能组件等工作,其实就是调用了 getCandidateConfigurations 方法,而 getCandidateConfigurations 方法就是从 META-INF/spring.factories 中获取 EnableAutoConfiguration 指定的值,并将这些值作为自动配置类导入到容器中,自动配置类就生效了,就能帮助我们进行自动配置功能。

META-INF下的spring.factories.png

  • @AutoConfigurationPackage 注解:
    • 源码如下: ```java @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import(AutoConfigurationPackages.Registrar.class) public @interface AutoConfigurationPackage {

} ```

  • 自动配置包。
  • @Import 注解是 Spring 的底层注解,就是给容器中导入一个组件,@AutoConfigurationPackage 导入的组件是 AutoConfigurationPackages.Registrar.class 。
  • 将主配置类( @SpringBootApplication 标注的类)的所在包及下面所有子包里面的所有组件扫描到 Spring 容器中。