美文网首页
Spring Boot(一)入门篇

Spring Boot(一)入门篇

作者: 骑士跳跳 | 来源:发表于2019-05-17 18:08 被阅读0次

    一、介绍

    实际上,SpringBoot就是Spring的一套工具,用于快速构建Spring应用程序。通过“习惯优于配置”的理念,省去很多原本Spring中需要手动完成的配置。此外还内嵌Servlet(Tomcat)容器,应用可jar包运行,方便我们快速搭建项目。

    二、项目初始化

    使用官方提供的Spring Initializr工具创建SpringBoot项目,该工具有两种使用方法:

    • 访问 https://start.spring.io/ ,确定好各个选项后,下载生成的项目文件,解压并导入工程。
    • 使用Idea自带的Spring Initializr功能,新建Project,里面有Spring Initializr选项。方便快捷,推荐使用。

    三、项目结构

    项目结构
    • DemoApplication.java:启动类
    • application.properties:SprigBoot配置文件
    • DemoApplicationTests:单元测试类
    • pom.xml: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 http://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>2.1.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <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>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    相比于之前的Spring项目,SpringBoot中的pom文件还是有些不一样的:

    • 父级依赖:spring-boot-starter-parent
    • 起步依赖:spring-boot-starter-xxx
    • Maven插件:spring-boot-maven-plugin

    1、父级依赖:spring-boot-starter-parent

    存在该依赖,就说明项目是SpringBoot项目了。

    这个parent提供了以下特性:

    • 默认使用Java 8
    • 默认使用UTF-8编码
    • 依赖管理,在dependencies里的部分配置可以不用填写version信息,这些version信息会从spring-boot-dependencies里得到继承。
    • 打包支持
    • 动态识别资源
    • 识别插件配置
    • 识别不同的配置,如:application.properties 和 application.yml

    关于具体提供了哪些依赖,可以查看Maven仓库中 org\springframework\boot\spring-boot-dependencies\2.1.5.RELEASE 路径下的pom文件。

    2、起步依赖:spring-boot-starter-xxx

    starter 是 Spring Boot 的一个重要组成部分,用于限制您需要执行的手动配置依赖项数量。

    starter并非单个依赖,而是一个依赖集,为了实现某一功能所需要的全部依赖。

    所有 starter 都使用以下命名约定:spring-boot-starter-XYZ,其中 XYZ 是想要构建的应用程序类型。以下是一些流行的

    • spring-boot-starter-web 用于构建 RESTful Web 服务,它使用 Spring MVC 和 Tomcat 作为嵌入式应用程序容器。
    • spring-boot-starter-jerseyspring-boot-starter-web 的一个替代,它使用 Apache Jersey 而不是 Spring MVC。
    • spring-boot-starter-jdbc 用于建立 JDBC 连接池。它基于 Tomcat 的 JDBC 连接池实现。

    下面看看spring-boot-starter-web包含哪些依赖:

    spring-boot-starter-web

    3、Maven插件:spring-boot-maven-plugin

    Spring Boot Maven插件提供了许多方便的功能:

    • 把项目打包成一个可执行的超级JAR(uber-JAR),包括把应用程序的所有依赖打入JAR文件内,并为JAR添加一个描述文件,其中的内容能让你用java -jar来运行应用程序。
    • 搜索public static void main()方法来标记为可运行类。

    五、Controller

    同SpringMVC中一样,这里我们需要新建一个控制类类。

    @RestController
    public class HelloController {
        @RequestMapping("/hello")
        public String hello() {
            return "Hello Spring Boot!";
        }
    }
    
    • @RestController注解等价于@Controller+@ResponseBody的结合,使用这个注解的类里面的方法都以json格式输出。

    六、启动项目

    DemoApplication是SpringBoot项目的启动类,是程序的入口。包括@SpringBootApplication注解和一个main方法。

    package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
    
    • 三种启动项目方法
      1. Chapter1Application的main方法
      2. 使用命令 mvn spring-boot:run”在命令行启动该应用
      3. 运行“mvn package”进行打包时,会打包成一个可以直接运行的 JAR 文件,使用“java -jar”命令就可以直接运行。
    搭建成功

    相关文章

      网友评论

          本文标题:Spring Boot(一)入门篇

          本文链接:https://www.haomeiwen.com/subject/yizoaqtx.html