SpringBoot2.x基础篇:开发你的第一个SpringBoot应用程序

开发环境

SpringBoot2.x版本是基于Java8来编写的,由于内部使用到了很多新的特性,比如:lambdainterface default...,所以需要本地开发环境有java8的支持。

不仅如此,SpringBoot在构建项目时默认使用Maven方式,所以本地开发环境也需要配置Maven环境变量。

~ java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)复制代码
~ mvn -version          
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/yuqiyu/soft/apache-maven-3.6.3
Java version: 1.8.0_231, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.3", arch: "x86_64", family: "mac"复制代码

如果你更喜欢使用Gradle方式来构建项目,那么本地就应该Gradle环境变量支持。

构建工具版本限制使用如下表所示:

构建工具 版本
Maven 3.3+
Gradle 5.x 或 6.x

新的项目

创建一个新SpringBoot应用程序的方式有多种:

  1. 使用IDEA内置的Spring Initializr创建(File -> New -> Project -> Spring Initializr)
  2. 创建基础Maven项目,修改pom.xml添加spring-boot-parent
  3. 访问 start.spring.io 选择依赖后,生成项目并下载

我一般采用第一种方式,这种方式比较快捷,IDEA内部也是通过 start.spring.io 这种方式将构建完成的zip文件下载到本地然后解压,所以你需要连接互联网才可以创建项目。

新项目的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>2.2.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>org.minbox.chapter</groupId>
  <artifactId>developing-first-application</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>developing-first-application</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</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>
    </plugins>
  </build>

</project>
复制代码

新创建的应用程序会自动spring-boot-parent作为父项目,这时我们就拥有了一些默认的资源配置、默认的依赖版本、默认的插件配置等。

添加依赖

当我们需要项目支持SpringMvc时,修改pom.xml文件在添加如下依赖即可:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>复制代码

添加spring-boot-starter-web依赖主要目的是演示Hello World输出。

示例代码

要完成我们的应用程序,需要来创建一个Java文件,默认情况下Maven会编译src/main/java目录下的源代码,我们可以在该目录下创建package来进行源代码的归类,下面我们来创建一个名为HelloExample.java的示例源代码文件,内容如下所示:

package org.minbox.chapter.developing.first.application;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Hello Example
 *
 * @author 恒宇少年
 */
@RestController
public class HelloExample {

    @GetMapping
    public String hello() {
        return "hello world!";
    }
}
复制代码

运行示例

到目前为止,我们新创建的应用程序应该可以工作了,由于应用程序的parentspring-boot-parent,因此具有了可运行的内置环境支持,可以直接通过命令行的方式来运行应用程序,当我们在应用程序的根目录下输入命令:

~ developing-first-application ✗ mvn spring-boot:run复制代码

通过Maven会将相关的依赖下载到本地默认的依赖仓库(~/.m2/repository),编译通过后自动运行项目,控制台输出如下所示:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.2.4.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.222 seconds (JVM running for 6.514)


 

发布了194 篇原创文章 · 获赞 40 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Sqdmn/article/details/104415720
今日推荐