springboot 快速体验

📅 发布时间:2026/7/12 13:26:33 👁️ 浏览次数:
springboot 快速体验
场景浏览器发送**/hello**请求返回Hello,Spring Boot 3!开发步骤创建Maven工程添加依赖(springboot父工程依赖 , web启动器依赖)编写启动引导类(springboot项目运行的入口)编写处理器Controller启动项目项目目录父工程 pom?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersion!-- SpringBoot可以帮我们方便的管理项目依赖 SpringBoot提供了一个名为 spring-boot-starter-parent 的工程里面已经对各种常用依赖的版本进行了管理 我们的项目必须以这个项目为父工程 这样我们就不用操心依赖的版本问题了需要什么依赖直接引入坐标(不需要添加版本)即可 --!--所有springboot项目都必须继承自 spring-boot-starter-parent--parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.0.5/version/parentgroupIdcom.atguigu/groupIdartifactIdspringboot-part/artifactIdversion1.0-SNAPSHOT/versionpackagingpom/packagingmodulesmoduleboot-quick/module/modulespropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependencies!-- 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开发的场景启动器--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependencies/project子工程pom?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.atguigu/groupIdartifactIdspringboot-part/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdboot-quick/artifactIdpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties/project启动类 MainApplicationpackagecom.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);}}controllerpackagecom.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);returnHello-World;}GetMapping(hello2)publicStringhello2(){System.out.println(dataSourceConfigurationProperties dataSourceConfigurationProperties);returnHello-World - 2;}}resources / application.propertiesserver.port80spring.jdbc.datasource.driverClassNamecom.mysql.cj.jdbc.driver spring.jdbc.datasource.urljdbc:mysql:///mybatis-examplespring.jdbc.datasource.usernameroot spring.jdbc.datasource.passwordrootresources / application.yamlserver:port:80spring:jdbc:datasource:driverClassName:com.mysql.jdbc.Driverurl:jdbc:mysql:///mybatis-exampleusername:root password:root启动项目http://localhost/hellohttp://localhost/hello2