news 2026/7/6 12:31:17

springboot 快速体验

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
springboot 快速体验

场景:浏览器发送**/hello**请求,返回"Hello,Spring Boot 3!"

开发步骤

  1. 创建Maven工程
  2. 添加依赖(springboot父工程依赖 , web启动器依赖)
  3. 编写启动引导类(springboot项目运行的入口)
  4. 编写处理器Controller
  5. 启动项目

项目目录

父工程 pom

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!-- SpringBoot可以帮我们方便的管理项目依赖, SpringBoot提供了一个名为 spring-boot-starter-parent 的工程,里面已经对各种常用依赖的版本进行了管理, 我们的项目必须以这个项目为父工程, 这样我们就不用操心依赖的版本问题了,需要什么依赖,直接引入坐标(不需要添加版本)即可 --><!--所有springboot项目,都必须继承自 spring-boot-starter-parent--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.0.5</version></parent><groupId>com.atguigu</groupId><artifactId>springboot-part</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>boot-quick</module></modules><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- SpringBoot提供了许多预定义的Starter,如: spring-boot-starter-web用于构建Web应用程序, spring-boot-starter-data-jpa用于使用JPA进行数据库访问, spring-boot-starter-security用于安全认证和授权, ...等等 使用Starter非常简单,只需要在项目的构建文件(例如Maven的pom.xml)中添加所需的Starter依赖, SpringBoot会自动处理依赖管理和配置。 springboot提供的全部启动器地址: [https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters](https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters) springboot启动器的命名规范: 官方提供的:命名为:`spring-boot-starter-*` 第三方提供:命名为:`*-spring-boot-starter` --><!--web开发的场景启动器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>

子工程pom

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.atguigu</groupId><artifactId>springboot-part</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>boot-quick</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties></project>

启动类 MainApplication

packagecom.atguigu;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;/** * @SpringBootApplication * 用于标识一个 SpringBoot应用程序 的入口类。 * 作用:将三个常用注解组合在一起,简化了配置的过程。 * * 具体而言,@SpringBootApplication 注解包含以下三个注解的功能: * @Configuration: * 将该类,标识为应用程序的配置类,它允许使用Java代码定义和配置Bean。 * * @EnableAutoConfiguration: * 启用 SpringBoot 的自动配置机制, * 它根据项目的依赖项,自动配置Spring应用程序的行为, * 自动配置,根据类路径、注解、配置属性等条件,来决定要使用的功能和配置。 * * @ComponentScan * 自动扫描并加载应用程序中的组件, * 如:控制器、服务、存储库等。 * 它默认扫描,@SpringBootApplication注解所在类的包、及其子包中的组件。 * * 使用 @SpringBootApplication注解,可以将上述三个注解的功能集中在一个注解上, * 简化了配置文件的编写和组件的加载和扫描过程, * 它是Spring Boot应用程序的入口点,标识了应用程序的主类, * 并告诉Spring Boot在启动时应如何配置和加载应用程序。 */@SpringBootApplicationpublicclassMainApplication{publicstaticvoidmain(String[]args){/* * SpringApplication.run() 是启动 SpringBoot应用程序的关键步骤,它创建应用程序上下文、 * 自动配置应用程序、启动应用程序,并处理命令行参数,使应用程序能够运行和提供所需的功能。 * */SpringApplication.run(MainApplication.class,args);}}

controller

packagecom.atguigu.controller;importcom.atguigu.properties.DataSourceConfigurationProperties;importcom.atguigu.properties.DataSourceProperties;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;@RestControllerpublicclassHelloController{@AutowiredprivateDataSourcePropertiesdataSourceProperties;@AutowiredprivateDataSourceConfigurationPropertiesdataSourceConfigurationProperties;@GetMapping("hello")publicStringhello(){System.out.println("dataSourceProperties= "+dataSourceProperties);return"Hello-World";}@GetMapping("hello2")publicStringhello2(){System.out.println("dataSourceConfigurationProperties= "+dataSourceConfigurationProperties);return"Hello-World - 2";}}

resources / application.properties

server.port=80spring.jdbc.datasource.driverClassName=com.mysql.cj.jdbc.driver spring.jdbc.datasource.url=jdbc:mysql:///mybatis-examplespring.jdbc.datasource.username=root spring.jdbc.datasource.password=root

resources / application.yaml

server:port:80spring:jdbc:datasource:driverClassName:com.mysql.jdbc.Driverurl:jdbc:mysql:///mybatis-exampleusername:root password:root

启动项目

http://localhost/hello

http://localhost/hello2

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/1 23:44:54

从规则引擎到神经网络:AI验布技术发展历程中的三次算法革命

AI验布技术今日的辉煌&#xff0c;并非一日之功。其核心检测算法的演进&#xff0c;深刻反映了整个计算机视觉与人工智能领域的发展脉络。每一次算法范式的更迭&#xff0c;都大幅提升了验布系统的能力边界&#xff0c;最终塑造了我们今天所见的高效智能质检系统。这段历史可以…

作者头像 李华
网站建设 2026/7/3 14:40:17

景区充电桩远程监控智慧运维系统方案

自驾游越来越成为很多人短途出行的重要方式&#xff0c;景区充电桩作为配套基础设施&#xff0c;其布局完善度、运行稳定性直接影响游客体验、景区服务品质及绿色文旅形象。当前&#xff0c;景区充电桩求受旅游季节性影响&#xff0c;十分容易出现旺季高负荷、淡季长期闲置的问…

作者头像 李华
网站建设 2026/7/1 16:15:05

Moltbot(Clawdbot) 专属轻量服务器

Moltbot/Clawdbot 专属轻量服务器搭建指南需求分析与规划搭建专属轻量服务器需明确用途、性能需求及预算。Moltbot/Clawdbot通常用于自动化任务或数据处理&#xff0c;需评估以下因素&#xff1a;计算需求&#xff1a;根据任务复杂度选择CPU核心数&#xff08;如2-4核&#xff…

作者头像 李华
网站建设 2026/7/2 0:58:45

3.1 MySQL性能监控体系搭建:从零开始构建完整的监控平台

3.1 MySQL性能监控体系搭建:从零开始构建完整的监控平台 📚 学习目标 通过本节学习,你将掌握: ✅ 监控体系的三层架构(系统级、数据库级、应用级) ✅ Prometheus + Grafana监控平台的搭建方法 ✅ MySQL关键性能指标的监控和告警 ✅ 监控数据的采集、存储和可视化 ✅ 生…

作者头像 李华