• SpringBoot(一):SpringBoot快速入门


    目录

    一、入门案例

    1、入门案例开发步骤 

    2、基于SpringBoot官网创建项目

    3、SpringBoot项目快速启动

    二、SpringBoot概述

    1、sring和springboot对比

    2、springboot起步依赖 

    3、springboot辅助功能


     

    一、入门案例

    • SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程

    1、入门案例开发步骤 

    (1)创建新模块,选择Spring初始化,并配置模块相关基础信息  

    4b418820891f425a80c0c38058733431.png

    (2)选择当前模块需要使用的技术集

    965d6dd57c5e4aa3b7fd664349700b3d.png

    (3)开发控制器类  

    1. @RestController
    2. @RequestMapping("/books")
    3. public class BookController {
    4. @GetMapping("/{id}")
    5. public String getById(@PathVariable Integer id) {
    6. System.out.println(id);
    7. return "hello , spring boot! ";
    8. }
    9. }

    (4)运行自动生成的Application类

    8b5f63ca01ad41d5af0b6feed92763f0.png

    -最简SpringBoot程序所包含的基础文件

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.5.0version>
    9. parent>
    10. <groupId>com.itheimagroupId>
    11. <artifactId>springboot-01-quickstartartifactId>
    12. <version>0.0.1-SNAPSHOTversion>
    13. <dependencies>
    14. <dependency>
    15. <groupId>org.springframework.bootgroupId>
    16. <artifactId>spring-boot-starter-webartifactId>
    17. dependency>
    18. dependencies>
    19. project>
    1. @SpringBootApplication
    2. public class Application {
    3. public static void main(String[] args) {
    4. SpringApplication.run(Application.class, args);
    5. }
    6. }

    -Spring程序与SpringBoot程序对比 

    e372529f48284cca8afcab2543d6793b.png

    **基于idea开发SpringBoot程序需要确保联网且能够加载到程序框架结构**

    2、基于SpringBoot官网创建项目

    https://start.spring.io/

    08c3b1d7ad3648109ff3d27f7fb21267.png

    3、SpringBoot项目快速启动

    1、对SpringBoot项目打包(执行Maven构建指令package)

    2、执行启动指令

    java -jar springboot_01_quickstart.jar	# 项目的名称根据实际情况修改

    注意事项:

    jar支持命令行启动需要依赖maven插件支持,请确认打包时是否具有SpringBoot对应的maven插件

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>org.springframework.bootgroupId>
    5. <artifactId>spring-boot-maven-pluginartifactId>
    6. plugin>
    7. plugins>
    8. build>

    二、SpringBoot概述

    1、sring和springboot对比

    (1)SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程

    (2)Spring程序缺点

            配置繁琐

            依赖设置繁琐

    (3)SpringBoot程序优点

            自动配置

            起步依赖(简化依赖配置)

            辅助功能(内置服务器,……)

    2、springboot起步依赖 

     (1)starter

        SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.5.0version>
    9. parent>
    10. <groupId>com.itheimagroupId>
    11. <artifactId>springboot-01-quickstartartifactId>
    12. <version>0.0.1-SNAPSHOTversion>
    13. <dependencies>
    14. <dependency>
    15. <groupId>org.springframework.bootgroupId>
    16. <artifactId>spring-boot-starter-webartifactId>
    17. dependency>
    18. dependencies>
    19. project>
    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>org.springframework.bootgroupId>
    6. <artifactId>spring-boot-dependenciesartifactId>
    7. <version>2.5.0version>
    8. <packaging>pompackaging>
    9. <properties>
    10. <servlet-api.version>4.0.1servlet-api.version>
    11. ...
    12. properties>
    13. project>

    (2)parent

            所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
            spring-boot-starter-parent(2.5.0)与 spring-boot-starter-parent(2.4.6)共计57处坐标版本不同

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    5. <modelVersion>4.0.0modelVersion>
    6. <parent>
    7. <groupId>org.springframework.bootgroupId>
    8. <artifactId>spring-boot-dependenciesartifactId>
    9. <version>2.5.0version>
    10. parent>
    11. <artifactId>spring-boot-starter-parentartifactId>
    12. <packaging>pompackaging>
    13. ...
    14. project>

     (3)实际开发

            使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供

            如发生坐标错误,再指定version(要小心版本冲突)

    1. <dependency>
    2. <groupId>junitgroupId>
    3. <artifactId>junitartifactId>
    4. <version>${junit.version}version>
    5. dependency>
    6. <dependency>
    7. <groupId>javax.servletgroupId>
    8. <artifactId>javax.servlet-apiartifactId>
    9. <version>${servlet-api.version}version>
    10. dependency>
    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <parent>
    5. <groupId>org.springframework.bootgroupId>
    6. <artifactId>spring-boot-starter-parentartifactId>
    7. <version>2.5.0version>
    8. parent>
    9. <dependencies>
    10. <dependency>
    11. <groupId>org.springframework.bootgroupId>
    12. <artifactId>spring-boot-starter-webartifactId>
    13. dependency>
    14. <dependency>
    15. <groupId>org.springframework.bootgroupId>
    16. <artifactId>spring-boot-starter-testartifactId>
    17. <scope>testscope>
    18. dependency>
    19. dependencies>
    20. project>

    3、springboot辅助功能

    SpringBoot程序启动

    1. @SpringBootApplication
    2. public class Springboot01QuickstartApplication {
    3. public static void main(String[] args) {
    4. SpringApplication.run(Springboot01QuickstartApplication.class, args);
    5. }
    6. }
    • SpringBoot在创建项目时,采用jar的打包方式

    • SpringBoot的引导类是项目的入口,运行main方法就可以启动项目

    • 使用maven依赖管理变更起步依赖项

    • Jetty比Tomcat更轻量级,可扩展性更强(相较于Tomcat),谷歌应用引擎(GAE)已经全面切换为Jetty

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starter-webartifactId>
    5. <exclusions>
    6. <exclusion>
    7. <groupId>org.springframework.bootgroupId>
    8. <artifactId>spring-boot-starter-tomcatartifactId>
    9. exclusion>
    10. exclusions>
    11. dependency>
    12. <dependency>
    13. <groupId>org.springframework.bootgroupId>
    14. <artifactId>spring-boot-starter-jettyartifactId>
    15. dependency>
    16. dependencies>

     

     

  • 相关阅读:
    jmeter文件下载接口处理
    两横一纵 | 寅家科技发布10年新征程战略
    RabbitMQ3.10.7高级特性
    海外转仓系统应用案例解读:如何高效快速解决海外仓补货需求
    Hadoop2.0 YARN cloudra4.4.0安装配置
    2023年【安全员-B证】新版试题及安全员-B证免费试题
    说说React的事件机制?
    c++视觉处理---图像重映射
    木聚糖-氨基|Xylan-NH2|木聚糖-聚乙二醇-氨基|氨基-PEG-木聚糖
    saltstack学习1入门基础
  • 原文地址:https://blog.csdn.net/weixin_43162044/article/details/127757018