祗疼妳一个 发表于 2024-2-22 20:48:00

2、SpringBoot2之入门案例

2.1、创建Maven工程

2.1.1、创建空项目

https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210182709139-1647813952.png
https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210182845903-750579488.png
2.1.2、设置项目名称和路径

https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210183116227-1718827221.png
2.1.3、设置项目sdk

https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210183310980-2015685045.png
2.1.4、项目初始状态

注意:需要关闭项目再重新打开,才能看到SpringBoot-Part文件夹
https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210183713786-246755711.png
2.1.5、配置maven

https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210184028620-250111482.png
2.1.6、创建module

右击SpringBoot-Part文件夹,创建新module
https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210184443794-1774824638.png
选择maven
https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210184534052-9180915.png
配置module名称和路径
https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210184733939-978733357.png
https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210184747307-1884668679.png
module初始状态
https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210184848874-1611021230.png
3、引入依赖

3.1、添加父工程

https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210205456447-1683315416.png
   
    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.7.18</version>
    </parent>3.2、添加web启动器

https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210210035056-898643971.png
    <dependencies>
      
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
    </dependencies>4、创建启动类

https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210212038399-422435847.png
package online.liaojy;

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

/**
* @author liaojy
* @date 2023/12/10 - 21:01
*/
// @SpringBootApplication 注解标识的类,为Spring Boot应用程序的启动类
@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
      /*
      * 该方法是启动 Spring Boot 应用程序的关键步骤,
      * 它会创建应用程序上下文、创建ioc容器、自动配置及启动应用程序(web项目会启动内置的Tomcat),并处理命令行参数。
      * */
      SpringApplication.run(MainApplication.class,args);
    }

}5、创建控制器

https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210212713461-493000625.png
在SpringBoot启动类所在的包以及子包下,IOC和DI的注解会被自动扫描并生效,约定俗成、无需额外的配置指定。
package online.liaojy.controller;

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

/**
* @author liaojy
* @date 2023/12/10 - 21:22
*/
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
      return "hello,springboot2!";
    }

}6、测试效果

https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210213452634-1646537656.png
启动springboot工程
注意:通过控制台的日志可知,默认使用了8080端口,内置了Tomcat9,上下文没有内容(也即是/)
https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231210213840304-699062355.png
访问对应的控制器方法,成功返回的正确响应内容。
7、案例总结

7.1、父工程


[*]每个 springboot 工程都有一个父工程 spring-boot-starter-parent ;
[*]spring-boot-starter-parent 的父工程是 spring-boot-dependencies
[*]spring-boot-dependencies 工程是版本仲裁中心,把所有常见的jar的依赖版本都声明好了,而且不存在版本冲突,
所以 springboot 工程中的依赖不需要写版本。
https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231217221418522-982119527.png
https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231213081943704-1867015652.png
7.2、starter启动器


[*]虽然 Maven 有依赖传递的功能,但是只能根据 jar 包之间的引用关系来关联依赖,不能根据开发场景来关联相关依赖,
[*]因此 Spring Boot 提供了一种叫做 Starter 的概念,用于关联和管理具体业务场景的相关依赖和配置;
[*]Starter 包含了一组预定义的依赖项集合,并提供了一致的依赖项版本管理,确保依赖项之间的兼容性和稳定性;
[*]Starter 遵循“约定大于配置”的原则,通过默认的设置来自动配置所需的组件和功能,让开发者更专注于业务逻辑而不是繁琐的配置细节;
[*]除了 Spring Boot 会提供一系列预定义的 Starter 之外,第三方(包括我们自己)也可以自定义Starter ;
[*]官方提供的 Starter 的命名格式:spring-boot-starter-* ,第三方提供的 Starter 的命名格式:*-spring-boot-starter 。
https://img2023.cnblogs.com/blog/2052479/202312/2052479-20231217215713895-820537308.png
SpringBoot 官方提供的 Starter 的文档地址:
https://docs.spring.io/spring-boot/docs/2.7.18/reference/html/using.html#using.build-systems.starters
7.3、@SpringBootApplication注解


[*]添加在启动类上的 @SpringBootApplication 注解,实际上是一个组合注解,其作用由具体的子注解来实现;
[*]@SpringBootApplication 注解的关键子注解包括:@ComponentScan、@EnableAutoConfiguration、@SpringBootConfiguration ;
[*]@SpringBootConfiguration 子注解本身也是一个组合注解,它包含了 @Configuration 注解,作用是将被标识的类声明为配置类;
[*]@EnableAutoConfiguration 子注解的作用是自动加载其他配置类,并会根据依赖项和类路径,自动配置各种常见的Spring配置和功能;
[*]@ComponentScan 子注解的作用是扫描所在类的包及其子包中的组件,并加载进Ioc容器中,使它们可被自动注入和使用。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 2、SpringBoot2之入门案例