SpringBoot入门

SpringBoot

什么是Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.

简单的说Springboot是一个用于加速构建Spring应用程序的框架,是一个集成框架,或者叫做基于spring的敏捷开发框架。

Springboot让创建一个可用于生产级别的应用程序变得非常简单,大多数Springboot应用程序仅需要最少的Spring配置。

Springboot并不是提供了某种新功能,而是将原来繁琐的框架整合变得简单了,便于我们把精力专注于业务逻辑的实现。

为什么需要Spring Boot

以往开发一个基于Spring的web项目时我们需要编写大量的配置文件,非常耗时和低效且容易出错。大多数应用程序的配置信息都是一样的,在这个敏捷开发大行其道的时代,Java也需要一个能够大幅加快开发速度的框架,Spring Boot应运而生。

创建Spring-web项目过程对比

以往开发一个ssm项目需要做的事情:

  • 安装tomcat用于部署,最后产生一个war
  • 创建一个maven项目,添加各个框架所需要的依赖包,还需要考虑依赖包的版本兼容问题
  • 创建、编写一堆看起来复杂,但又重复的配置文件,整合各个框架到Spring中
  • 业务开发
  • 测试=>打包,发布到tomcat

Spring Boot:

  • 默认将tomcat打包到项目中作为默认服务器(应用程序自己保护了tomcat)
  • 创建一maven项目,或者不创建直接去官网下载一个空项目
  • 在pom中添加需要集成的框架的starter(启动器),starter会自动完成框架与Spring的整合和配置
  • 一个简单的Spring Boot配置文件,仅需要提供少数必须的配置(如jdbc参数)
  • 业务开发
  • 直接运行main方法,或者使用maven运行,或者打包为jar通过Java命令行运行
  • 需要注意的是:(Spring Boot是需要依赖构建工具的,可以是maven或者gradle)

Spring Boot的核心功能

  1. 起步依赖

    起步依赖本质上就是一个pom(项目对象模型),其中包含了某个框架/功能运行所需的基础依赖信息,当我们需要某个框架/功能时加入该框架/功能的依赖即可

  2. 自动配置

    Spring Boot会在启动时完成对各个框架的自动配置,考虑了众多因数,最终决定Spring配置应该用哪个,不该用哪个,该过程是Spring自动完成的

  3. 内置tomcat或者jetty等servlet容器

  4. 没有代码生成,不需要XML配置

  5. 尽可能自动配置Spring容器中的been

入门程序

运行方式一:通过配置主函数的方式运行。

  1. 创建基础的maven过程

  2. 在pom中继承SpringBoot的starter, 这决定了该工程是一个Spring Boot工程。注意,这不是依赖,而是继承关系,就像是继承一个class类一样,通过继承拥有父类的功能。

    在pom.xml中添加如下配置:

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.4.RELEASE</version>
        </parent>
    
  3. 在pom.xml中添加web的start依赖,告诉maven,web应用需要用到的依赖有哪些,只需要加进来就可以,Spring Boot会自动完成相应的配置。

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
  4. 编写Spring Boot引导类,为Spring Boot提供一个启动入口。

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    // 引导类不允许放在根目录下
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            // 启动Spring Boot需要指定引导类
            SpringApplication.run(Application.class);
        }
    }
    
    // 添加一个测试的控制器类
    package com.lucas.controller;
    
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @RequestMapping("test/{name}")
        public String sayHello(@PathVariable String name){
            return "hello: " + name;
        }
    }
    

运行方式二:通过通过maven插件运行

  1. 修改pom文件,添加插件

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

    spring-boot: run
    

image-20200319190206642

把maven命令spring-boot:run命令添加到IDEA快捷方式。

image-20200320005837242

image-20200319190933817

参考:

posted on 2020-03-20 01:12  lucaswangdev  阅读(135)  评论(0)    收藏  举报