第一个 Spring Boot 应用程序

概述

这里我们使用 Intellij IDEA 来新建一个 Spring Boot 项目。

打开 IDEA -> New Project -> Spring Initializr

在这里插入图片描述

填写项目信息

在这里插入图片描述

选择 Spring Boot 版本及 Web 开发所需的依赖

在这里插入图片描述

保存项目到指定目录在这里插入图片描述

工程目录结构

创建完成后的工程目录结构如下:

│  .gitignore
│  pom.xml
│
│
└─src
    ├─main
    │  ├─java
    │  │  └─com
    │  │      └─hz
    │  │          └─hello
    │  │              └─spring
    │  │                  └─boot
    │  │                          HelloSpringBootApplication.java
    │  │
    │  └─resources
    │      │  application.properties
    │      │
    │      ├─static
    │      └─templates
    └─test
        └─java
            └─com
                └─hz
                    └─hello
                        └─spring
                            └─boot
                                    HelloSpringBootApplicationTests.java

  • .gitignore:Git 过滤配置文件
  • pom.xml:Maven 的依赖管理配置文件
  • HelloSpringBootApplication.java:程序入口
  • resources:资源文件目录
    • static: 静态资源文件目录
    • templates:模板资源文件目录
    • application.properties:Spring Boot 的配置文件,实际开发中会替换成 YAML 语言配置(application.yml)

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.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hz</groupId>
    <artifactId>hello-spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-spring-boot</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>

  • parent:继承了 Spring Boot 的 Parent,表示我们是一个 Spring Boot 工程
  • spring-boot-starter-web:包含了spring-boot-starter还自动帮我们开启了 Web 支持

功能演示

我们创建一个 Controller 来演示一下 Spring Boot 的神奇功能

package com.hz.hello.spring.boot.controller;

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

@RestController
public class HelloController {
    @GetMapping("/")
    public String sayHi(){
        return "hi,Spring Boot";
    }
}

启动 HelloSpringBootApplicationmain() 方法,浏览器访问 http://localhost:8080 可以看到:

hi,Spring Boot

神奇之处

  • 没有配置 web.xml
  • 没有配置 application.xml,Spring Boot 帮你配置了
  • 没有配置 application-mvc.xml,Spring Boot 帮你配置了
  • 没有配置 Tomcat,Spring Boot 内嵌了 Tomcat 容器
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值