Spring Boot 快速入门

本文介绍SpringBoot作为Spring应用的快速启动平台,通过习惯优于配置的理念简化项目配置,提高开发效率。文章涵盖SpringBoot的简介、搭建流程及热部署配置,适合初学者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Boot

Spring Boot简介

Build Anything with Spring Boot:Spring Boot is the starting point for building all Spring-based applications. Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring.

这是摘自Spring Boot官网的原文 大概理解就是: Spring Boot 是所有基于 Spring 进行开发项目的起点。而Spring Boot 的设计是为了让你的 Spring 应用程序能够更快速的运行 并且尽可能减少你的配置文件。

Spring Boot是什么

它并不是什么新的框架,只是使用了 “习惯优于配置”的方式,来减轻以往spring项目中存在大量配置文件的问题,大大提高了项目开发效率,能够让你的项目更快速的运行起来,Spring Boot 整合了所有框架,并通过Maven来管理所有的jar依赖

Spring Boot快速搭建

开始

使用idea

版本: java8 spring boot 2.0.5

创建项目 New Project->Spring Initializr->点击Next

这里写图片描述

填写你的Group 和 Artifact 点击Next
这里写图片描述

点击web选项 把右边的web选项也勾上
这里写图片描述

填写project name 和 location
这里写图片描述

点击Finish
下面就是整个工程的结构了 看上去挺清爽的,少了很多配置文件,了解下都有些什么文件:

这里写图片描述

  • SpringbootApplication 一个带有 main() 方法的类,用于启动应用程序

  • SpringbootApplicationTests 一个空的 Junit 测试了,它加载了一个使用 Spring Boot 字典配置功能的 Spring 应用程序上下文

  • application.properties 一个空的 properties 属性配置文件,可以根据需要添加配置属性

  • pom.xml Maven 构建配置文件


项目创建好了 那么接下来 先写个简单的例子来看看如何使用Spring boot 开发 _____________________________

HelloWord

创建HelloController

1.在 SpringbootApplication类的路径下创建一个 controller类

Spring Boot 会自动扫描标注有 @SpringBootApplication 所在类的 同级包以及子级包里的 Bean
入口类建议就配置在 com.yb.springboot 包下 (grounpID + arctifactID 下)

这里写图片描述

@RestController 注解: 该注解是 @Controller 和 @ResponseBody 注解的合体版


2.到SpringbootApplication类中来启动项目 右键->Run…

这里写图片描述

** Spring Boot 内置了 Tomcat 所以无需再配置 **

看后台打印的信息 Spring boot启动

3.访问测试

这里写图片描述


Spring Boot 热部署

添加pom.xml配置

  • 添加依赖包
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
    <scope>true</scope>
    </dependency>
  • 添加插件
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
    <fork>true</fork>
    </configuration>
    </plugin>

idea的配置

如果是Eclipse中,只需上面的配置就已经能够正常使用了,但idea并不会在修改源代码后自动编译启动,需要使用ctrl+F9手动make项目

  • 修改源代码
    将hello改为hello2 使用ctrl+F9手动编译代码
    在这里插入图片描述

开启idea自动make

还有种方式就是修改idea的配置,因为idea默认并不会自动编译代码,只需修改一些配置项

  • ctrl+shift+a
    输入Registry在这里插入图片描述
    在这里插入图片描述

找到 compiler.automake.allow.when.app.running项 打上勾

再使用ctrl+shift+a 输入:make project
在这里插入图片描述

找到 make project automatically 项 打上勾

在这里插入图片描述

测试

修改源码 看看是否配置成功
在修改源码后 看到后打印的日志 项目被停止后又启动了
在这里插入图片描述

在这里插入图片描述

END

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值