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 节点中添加
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
- ② 在 Maven 构建的项目的 pom.xml 中的
<build></build>
节点的<plugins></plugins>
添加
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
3.2 SpringBoot HelloWorld
3.2.1 功能需求
- 浏览器发送 hello 请求,服务器接收请求并处理,响应输出 Hello World 字符串。
3.2.2 创建 Maven 工程(jar)
- 略。
3.2.3 导入 SpringBoot 的相关依赖 jar 包的 Maven 坐标
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sunxiaping</groupId>
<artifactId>springboot</artifactId>
<version>1.0</version>
<name>springboot</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.2.4 编写主程序
- SpringBootApplication.java
package com.sunxiaping.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 主程序,用来启动SpringBoot应用
*/
@SpringBootApplication
public class HelloWorldAApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldAApplication.class, args);
}
}
3.2.5 编写相关的 Controller
- HelloController.java
package com.sunxiaping.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorld {
@RequestMapping(value = "/hello")
public String hello(){
return "Hello World";
}
}
3.2.6 运行测试
- 只需要运行 SpringbootApplication.java 即可。
3.2.7 简化部署
- 在 pom.xml 中导入插件,用于将 SpringBoot 应用打包成一个 jar 包:
<build>
<plugins>
<!-- 可以将应用打包成一个可执行的jar包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 将这个应用打成 jar 包,直接使用
java -jar
的命令进行执行。
4 HelloWorld 探究
4.1 pom 文件
4.1.1 父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
它的父项目是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
它是真正管理 SpringBoot 应用里面的所有依赖版本
可以将上面的父项目称为 SpringBoot 的版本仲裁中心。 所以我们导入依赖默认是不需要写版本号的,前提是版本仲裁中心里面有。
4.1.2 启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- spring-boot-starter:称为场景启动器。
- spring-boot-starter-web:帮我们导入 web 模块正常运行所依赖的组件。
- SpringBoot 将所有的功能场景都抽取出来,做成一个个的 starters(启动器),只需要在项目中引入这些 starter ,其相关场景的所有依赖都会导入进来。需要什么功能就导入什么场景的启动器。
4.2 主程序类(主入口类)
package com.sunxiaping.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 主程序,用来启动 SpringBoot 应用
*/
@SpringBootApplication
public class HelloWorldAApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldAApplication.class, args);
}
}
- @SpringBootApplication:SpringBoot 应用标注在某个类上,说明这个类是 SpringBoot 的主配置类,SpringBoot 就应该运行这个类的 main 方法来启动 SpringBoot 应用。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
//略
}
- @SpringBootConfiguration 注解:
- 源码如下: ```java @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration {
}
- SpringBoot 的配置类。
- 标注在某个类上,表示这是一个 Spring Boot 的配置类。
- 实际就是 @Configuration 注解,@Configuration 注解标注在某个类上,那么这个类就是配置类,配置类也是容器中一个组件。
- @EnableAutoConfiguration 注解:
- 源码如下:
```java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
Class<?>[] exclude() default {};
/**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
* @since 1.3.0
*/
String[] excludeName() default {};
}
- 开启自动配置功能。
- @Import(EnableAutoConfigurationImportSelector.class):导入那些组件的选择器。
- 会给容器中导入非常多的自动配置类(xxxAutoConfiguration)。
- 自动配置类的作用:就是给容器中导入这个场景需要的所有组件,并配置好这些组件。
- 有了自动配置类,就免去了我们手动编写配置注入功能组件等工作,其实就是调用了 getCandidateConfigurations 方法,而 getCandidateConfigurations 方法就是从 META-INF/spring.factories 中获取 EnableAutoConfiguration 指定的值,并将这些值作为自动配置类导入到容器中,自动配置类就生效了,就能帮助我们进行自动配置功能。
- @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 容器中。