一、Spring Boot 入门


1. Spring Boot 简介

简化Spring应用开发的一个框架
整个 Spring 技术栈的一个大整合
J2EE 开发的一站式解决方案

1.1 使用 Spring Boot 优势

  • 使用嵌入式的 Servlet 容器,应用无需打成WAR包
  • starters 自动依赖与版本控制
  • 无需配置 XML,无代码生成,开箱即用
  • 准生成环境的运行时应用监控
  • 与云计算的天然集成

2、微服务


一个应用分成一组小型服务;可以通过 HTTP 方式进行互通;

详尽文档

3、项目创建


第一步:创建一个 maven 工程

第二步:导入Spring Boot 相关依赖

在 pom.xml 中添加

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

第三步:编写主程序(启动Springboot应用)

/**
 * SpringBoot项目启动主程序类
 * @author Juniors
 */
@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {

        //Spring 应用启动起来
        SpringApplication.run(MainApplication.class,args);
    }
}

@SpringBootApplication:用来标注一个主程序类,说明这是一个Spring Boot应用

第四步:编写相关的 Controller,Service 类

/**
 * @author Juniors
 */
@Controller
public class HelloWorld {

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "Hello Juniors!";
    }
}

第四步:运行项目

基于springboot博物馆管理系统 springboot 博客园_Spring Boot

第五步:简化部署

引入插件将该应用达成 jar 包,直接使用 java-jar 命令执行

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

打包如图:

基于springboot博物馆管理系统 springboot 博客园_spring_02


注:Spring Boot项目中已经有内置的Tomcat环境,因此即可执行。


4、Spring Boot 探究

1. POM 文件

1.1 父项目

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

它的父项目是

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath>../../spring-boot-dependencies</relativePath>
    </parent>
  • 该项目来负责真正管理Spring Boot应用里的所有依赖版本
  • Spring Boot 的版本仲裁中心
  • 以后我们导入依赖默认是不需要写版本了(没有在dependencies

1.2 导入的依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

spring-boot-starter-web:
spring-boot-starter: spring-boot场景启动器;帮我们导入web模块正常运行所依赖的组件(Tomcat,Hibernate,jackson)
Spring Boot将所有的功能场景都抽离出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter,其相关场景的所有依赖都会导进来。
例如: spring-boot-starter-thymeleaf
spring-boot-starter-rabbitmq
spring-boot-starter-mail

2、主程序类,主入口类

/**
 * SpringBoot项目启动主程序类
 * @author Juniors
 */
@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {

        //Spring 应用启动起来
        SpringApplication.run(MainApplication.class,args);
    }
}

@SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是Spring Boot的主配置类,Spring Boot 就应该运行这个类的main方法启动Spring Boot应用。

@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: 标注在某个类上,表示这是一个Spring Boot配置类
**@Configuration:**配置类上标注这个注解;配置文件—》配置类(容器类的一个组件@Component)
@EnableAutoConfiguration: 开启自动配置功能

@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

@AutoConfigurationPackage:自动配置包
@Import(AutoConfigurationPackages.Registrar.class)
Spring的底层注解@Import,给容器中导入一个组件;导入的组件由AutoConfigurationPackages.Registrar.class;
将主配置类(@SpringBootApplication标注的类)所在包及下面的所有自包里面所有组件扫描到Spring容器中;
@Import(AutoConfigurationImportSelector.class):导入哪些组件的选择器;
将所有需要导入的组件以全类名的方式返回,这些组件就会添加到容器当中;

3、使用Spring Initializer 快速创建Spring Boot项目

默认生成的 Spring Boot 项目:

  • 主程序已经生成好了,我们只需要我们自己编写业务逻辑
  • resource 文件夹中的目录结构
    > static : 保存所有静态资源;js, css, images
    > templates : 保存所有的模板页面
    > application.properties : 修改默认配置