S++

千线一眼

导航

SpringBoot-快速入门

官方文档

Getting Started


快速入门

1. 创建Maven项目

例如创建一个名为 springboot-helloworld 的项目

2. 配置环境

添加继承的父工程

    <!--springboot工程需要继承的父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
    </parent>

添加类路径依赖项

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

3. 编写代码

创建一个名为src/main/java/MyApplication.java的文件来包含以下代码:

import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;


@RestController
@EnableAutoConfiguration
public class MyApplication {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

4. 运行工程

编写一个可运行类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

访问 localhost:8080 就会看见hello world的字样


起步依赖分析

spring-boot-starter-parentspring-boot-starter-web

其中定义了各种技术的版本信息,组合了一套最优搭配的技术版本
在各种starter中,定义了完成该功能需要的坐标合集,其中大部分版本信息来自于父工程

posted on 2022-04-08 09:09  S++  阅读(35)  评论(0)    收藏  举报