SpringBoot(1) —— 概述、快速入门、快速构建

本文介绍了SpringBoot的特点,包括自动配置和起步依赖,旨在简化Spring项目的配置和依赖管理。并详细阐述了如何快速入门SpringBoot,从创建项目、配置pom.xml到编写Controller和引导类,最后启动并测试项目,实现返回'HelloSpringBoot!'的功能。此外,还讲解了如何快速构建SpringBoot工程,包括创建新模块和Controller,通过访问localhost:8080/hello2验证成功。
摘要由CSDN通过智能技术生成

一、概述

1. 缺点

1) 配置繁琐

2) 依赖繁琐

2. 功能

1) 自动配置

2) 起步依赖

起步依赖本质上是一个 Maven 项目对象模型,定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。

3) 辅助功能

提供了常见的非功能性特性,如嵌入式服务器、安全、指标等。

3. 小结

SpringBoot 提供了一种快速开发 Spring 项目的方式,而不是对 Spring 功能上的增强。

二、SpringBoot 快速入门

1. 需求

搭建 SpringBoot 工程,定义 HelloController.hello() 方法,返回 “HelloSpringBoot!"。

2. 实现步骤

  1. 创建 maven 项目
  2. 导入 SpringBoot 起步依赖
  3. 定义 Controller
  4. 编写引导类
  5. 启动测试

3. 实现

3.1 创建项目

项目名称:MySpringBoot

3.2 创建模块

创建一个 maven 模块。

项目名:springboot-helloworld

group ID:com.xuxumiao

ArtifactID:springboot-helloworld

image-20220119102949745

3.3. 配置 pom.xml

根据 spring 官方的参考文档进行操作。

pom.xml 文件中引入 springboot 的父工程:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
    </parent>

引入依赖:

    <dependencies>
        <!--web 开发的起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

3.4 创建 helloworldCrontroller 类

代码如下:

package com.xuxumiao.controller;

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

/**
 * @author: xxm
 * @Date: 2022/1/19 11:05
 */

@RestController
public class helloContoller {

    @RequestMapping(value = "/hello")
    public String hello(){
        return " hello Spring Boot !";
    }
}

3.5 创建引导类

创建一个引导类,作为 SpringBoot 项目的入口。代码如下:

package com.xuxumiao;

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

/**
 * @author: xxm
 * @Date: 2022/1/19 11:14
 */
@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class,args);
    }
}

3.6 启动项目

从引导类中运行项目。

再访问 localhost:8080/hello

成功:

image-20220119111954550

三、快速构建 SpringBoot 工程

1. 创建模块

在项目中创建一个新模块

New Module -> Spring Initializer ,设置如下:

image-20220120144452274

填写完毕后,点击下一步 -> 选择 Spring Web -> 完成。之后会自动下载一些依赖,等待下载完成后即可。

2. 创建 controller 类

代码如下:

package com.xuxumiao.springbotinit.controller;

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

/**
 * @author: xxm
 * @Date: 2022/1/20 15:31
 */
@RestController
public class HelloController {


    @RequestMapping("/hello2")
    public String hello(){
        return "hello spring boot 2!";
    }
}

3. 测试

启动项目,访问 localhost:8080/hello2

成功访问:

image-20220120153456863

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值