场景:浏览器发送**/hello**请求,返回"Hello,Spring Boot 3!"
开发步骤
- 创建Maven工程
- 添加依赖(springboot父工程依赖 , web启动器依赖)
- 编写启动引导类(springboot项目运行的入口)
- 编写处理器Controller
- 启动项目
项目目录
父工程 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=rootresources / application.yaml
server:port:80spring:jdbc:datasource:driverClassName:com.mysql.jdbc.Driverurl:jdbc:mysql:///mybatis-exampleusername:root password:root