快速体验Spring Boot了解使用、运行和打包 | SpringBoot 2.7.2学习系列 ...

打印 上一主题 下一主题

主题 852|帖子 852|积分 2556

SpringBoot 2.7.2 学习系列,本节内容快速体验Spring Boot,带大家了解它的基本使用、运行和打包。
Spring Boot 基于 Spring 框架,底层离不开 IoC、AoP 等核心思想。Spring 4.0 提供了基于 Java Config 的开发方式,Spring Boot 应运而生,可以简化 Spring 应用开发过程,同时也可以快速方便的集成第三方框架,如 MyBatis、Redis 等。
0 版本说明


  • 开发工具 IDEA 版本:2021.2
  • Maven 版本: 3.6.3
  • Spring Boot 版本:2.7.2
  • JDK 版本:JDK 8
  • MySQL 版本:MySQL 8
说明,当前 Spring Boot 2.x 最新稳定版为 2.7.2 ,JDK 8 需要以上版本、Maven 需要 3.5 以上版本。(本想基于 Spring Boot 3.x,但 3.x 需要 Java 17,优雅哥电脑还只是 JDK 8)
1 创建 Spring Boot 应用

1.1 创建工程

1)打开 idea,新建一个 Maven 项目,点击“Next”

2)输入 Name、GroupId、ArtifactId,点击“Finish”

1.2 设置 IDEA

在 IDEA 的 Preferences 中设置 JDK、Maven
1)设置 Maven

2)设置 JDK

在 Module Settings 中设置 JDK

首先设置 SDKs:

接着在 “Project” 中选择设置的 SDK

最后在 “Modules” 中选择 Language Level:

2 添加 Spring Boot 支持

2.1 添加依赖

1)在 pom.xml 中添加 Spring Boot 依赖。
在烂大街的文章博客中,都是通过 parent 的方式继承 spring-boot-starter-parent,如下:
  1.     <parent>
  2.         <groupId>org.springframework.boot</groupId>
  3.         <artifactId>spring-boot-starter-parent</artifactId>
  4.         <version>2.7.2</version>
  5.     </parent>
复制代码
这种方式几乎都用于在 demo 编写中,在大型项目或中大型企业中很少见到这么使用的。因为每个 module 只能有一个 parent ,而在企业开发中,微服务有多个服务,多个服务一般会继承自一个统一的 module,便于版本控制、通用功能等。如果在每个服务中都让spring-boot-starter-parent 占据了 parent 节点,那如何继承统一的 parent module 呢?
或许有人会说,在 parent module 中继承 spring-boot-starter-parent。没错,确实可以这样。但除了服务,还会有一些公共模块(如对参数校验、通用响应、分布式 Redis 锁、Spring Doc等通用模块)也继承自这个parent module,这样一来,这些公共module也被迫添加了压根没有使用 spring boot starter 依赖。
我的做法是通过 spring-boot-dependencies 来实现:

  • 在 properties 中定义 spring-boot-dependencies 版本号
  • 在依赖管理 dependencyManagement 中,通过 pom 的方式、scope 为 import 添加 spring-boot-dependencies
  • 在依赖dependencies中添加需要使用到的依赖。
pom.xml 文件如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <groupId>com.yygnb.demo</groupId>
  7.     <artifactId>hero-springboot-demo</artifactId>
  8.     <version>1.0-SNAPSHOT</version>
  9.     <properties>
  10.         <maven.compiler.source>8</maven.compiler.source>
  11.         <maven.compiler.target>8</maven.compiler.target>
  12.         
  13.         <spring-boot-dependencies.version>2.7.2</spring-boot-dependencies.version>
  14.     </properties>
  15.     <dependencies>
  16.         <dependency>
  17.             <groupId>org.springframework.boot</groupId>
  18.             <artifactId>spring-boot-starter-web</artifactId>
  19.         </dependency>
  20.     </dependencies>
  21.     <dependencyManagement>
  22.         <dependencies>
  23.             <dependency>
  24.                 <groupId>org.springframework.boot</groupId>
  25.                 <artifactId>spring-boot-dependencies</artifactId>
  26.                 <version>${spring-boot-dependencies.version}</version>
  27.                 <type>pom</type>
  28.                 <scope>import</scope>
  29.             </dependency>
  30.         </dependencies>
  31.     </dependencyManagement>
  32. </project>
复制代码
2.2 创建启动类

创建启动类:com.yygnb.demo.DemoApplication
  1. @SpringBootApplication
  2. public class DemoApplication {
  3.     public static void main(String[] args) {
  4.         SpringApplication.run(DemoApplication.class, args);
  5.     }
  6. }
复制代码
3 测试运行

3.1 添加测试Controller

创建测试使用的 Controller:com.yygnb.demo.controller.DemoController
  1. @RestController
  2. @RequestMapping("demo")
  3. public class DemoController {
  4.     @GetMapping("hello")
  5.     public String hello(String msg) {
  6.         String result = "Hello Spring Boot ! " + msg;
  7.         System.out.println(result);
  8.         return result;
  9.     }
  10. }
复制代码
3.2 启动运行

运行DemoApplication 中的 main 方法即可启动应用。控制台显示如下,则应用启动成功:

在浏览器中访问:
http://localhost:8080/demo/hello?msg=FirstTest
页面上和控制台中都会显示:
  1. Hello Spring Boot ! FirstTest
复制代码
3.3 修改端口

Spring Boot 默认运行的端口为 8080,如果要修改运行的端口号,需要修改配置实现。Spring Boot 支持三种格式的配置文件:xml、yml、yaml,在项目中使用 yml格式较多。
使用 yml 格式需要注意:
  1. 1. 使用冒号分隔属性名和属性值
  2. 2. 字符大小写敏感
  3. 3. 层级之间缩进敏感(间隔两个空格)
  4. 4. 属性值前面需要加一个空格
复制代码
Spring Boot 核心配置文件名为 application。
在 src/main/resources 中创建配置文件application.yml
  1. server:
  2.   port: 9099
复制代码
该配置指定了服务运行的端口号为 9099,重启服务,将上面的访问路径中的8080修改为 9099 再次访问。
4 打包

Maven 工程打包是通过 mvn 命令进行的。在控制台中项目根目录下执行下列命令:
  1. mvn clean package
复制代码
执行该命令后,会在 target 目录下生成 hero-springboot-demo-1.0-SNAPSHOT.jar。查看该文件的大小,只有 4kb,这是因为直接打包仅仅会打包当前的代码,相关使用到的 spring boot 的依赖并不会包含进去,此时的 jar 文件是不能直接运行的。如果要让打包后的 jar 可以直接运行,需要在 pom.xml 中配置相关插件配置:
4.1 配置插件
  1. <build>
  2.     <plugins>
  3.         <plugin>
  4.             <groupId>org.springframework.boot</groupId>
  5.             <artifactId>spring-boot-maven-plugin</artifactId>
  6.             <version>${spring-boot-dependencies.version}</version>
  7.             <executions>
  8.                 <execution>
  9.                     <goals>
  10.                         <goal>repackage</goal>
  11.                     </goals>
  12.                 </execution>
  13.             </executions>
  14.         </plugin>
  15.     </plugins>
  16. </build>
复制代码
重新执行 mvn clean pacakge,会在 target 中生成两个文件:

  • hero-springboot-demo-1.0-SNAPSHOT.jar.original
  • hero-springboot-demo-1.0-SNAPSHOT.jar
前者是打包的原始文件,和配置插件前一样,仅包含当前项目的代码。后者就包含了 spring boot 有关依赖,内置 Tomcat,可以直接运行。
4.2 运行 jar

可通过 java 命令执行上面打包生成的可执行文件。在控制台中执行:
  1. java -jar target/hero-springboot-demo-1.0-SNAPSHOT.jar
复制代码
命令执行后,依旧会启动服务。
本文记录了另一种依赖 spring boot 的方式,下一篇集成MyBatis Plus实现增删改查。

今日优雅哥(youyacoder)学习结束,期待关注留言分享~~

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

梦见你的名字

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表