SpringBoot概述&SpringBoot基础配置&yml的使用&多环境启动


在这里插入图片描述

🐌个人主页: 🐌 叶落闲庭
💨我的专栏:💨
c语言
数据结构
javaEE
操作系统

石可破也,而不可夺坚;丹可磨也,而不可夺赤。


一、 SpringBoot概述

1.1 起步依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.14</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot_quick_start</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_quick_start</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

  • 根据spring-boot-starter-web可以得到它对应的配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.7.14</version>
  </parent>
  <artifactId>spring-boot-starter-parent</artifactId>
  <packaging>pom</packaging>
</project>
  • 这个配置类又继承了dependencies
      1. 各种properties信息:
 <properties>
     <activemq.version>5.16.6</activemq.version>
 ...
</properties>
    • 2.各种依赖管理:
<dependencies>
<dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-amqp</artifactId>
        <version>${activemq.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-blueprint</artifactId>
        <version>${activemq.version}</version>
      </dependency>
      ...
</dependencies>
  • starter
    • SpringBoot中常见项目名称,定义了当前项目使用的所有项目目标,已达到减少依赖配置的目的
  • parent
    • 所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
    • spring-boot-starter-parent(2.5.0)与spring-boot-starter-parent(2.4.6)共计57处坐标版本不同
  • 实际开发
    • 使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供
    • 如发生坐标错误,再指定version(要小心版本冲突)

1.2 辅助功能

  • 内置服务器,根据spring-boot-starter-web依赖,可以内置一个tomcat服务器
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 内置了tomcat服务器
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.7.14</version>
      <scope>compile</scope>
</dependency>

1.3 SpringBoot程序启动

  • 启动方式
@SpringBootApplication
public class SpringbootQuickStartApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootQuickStartApplication.class, args);
    }
}
  • SpringBoot在创建项目时,采用jar的打包方式
  • SpringBoot的引导类是项目的入口,运行main方法就可以启动项目
  • 将tomcat服务器换为jetty服务器:
  • 在原有的服务器启动类下使用exclusion排除掉Tomcat依赖:
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
     <exclusions>
         <exclusion>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-tomcat</artifactId>
         </exclusion>
     </exclusions>
</dependency>
  • 添加Jetty服务器的依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
  • 启动SpringBoot项目

在这里插入图片描述


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

二、 SpringBoot基础配置

2.1 配置文件格式

  • SpringBoot提供了多种属性配置方式(选用不同的配置文件)
  • 配置文件名必须是application开头的,否则可能不生效
    • application.properties
server.port=80
    • application.yml
server:
  port: 81
    • application.yaml
server:
  port: 82

注意:port:后一定要加空格

  • 当这三个配置文件都配置时,默认使用顺序是application.properties优先级最高,其次是application.yml,最低优先级是application.yaml,一般写项目时用的配置文件是application.yml

2.1.1 自动提示功能消失解决方案

在使用以.yml.yaml为后缀名的配置文件时,可能会出现自动提示功能消失的问题
解决步骤如下:


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


2.2 yaml

  • YAML (YAML Ain’t Markup Language),一种数据序列化格式
  • 优点:
    • 容易阅读
    • 容易与脚本语言交互
    • 以数据为核心,重数据轻格式
  • YAML文件扩展名
    • .yml(主流)
    • .yaml

2.2.1 yaml语法规则

  • 大小写敏感
  • 属性层级关系使用多行描述,每行结尾使用冒号结束
  • 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
  • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
  • #表示注释

2.2.2 yaml数据读取方式

2.2.2.1 使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名…}
lesson: SpringBoot

server:
  port: 80

books:
  name:
  subject:
    - Java
    - 操作系统
    - 网络
@RestController
@RequestMapping("/books")
public class BookController {

    @Value("${lesson}")
    private String lesson;

    @Value("${books.subject[0]}")
    private String subject_0;
}
2.2.2.2 封装全部数据到Environment对象
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private Environment environment;

    @Autowired
    private Books books;
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {
        System.out.println(lesson);
        System.out.println(subject_0);
        System.out.println(environment.getProperty("lesson"));
        System.out.println(environment.getProperty("books"));
        System.out.println(books);
        return "hello,spring boot!";
    }
}
2.2.2.3 自定义对象封装指定数据
@Component
@ConfigurationProperties(prefix = "books")
public class Books {
    private String name;
    private String[] subject[];
}
2.2.2.4 自定义对象封装指定数据警告解决方案

在使用自定义对象封装指定数据时,可能会遇到警告信息:


在这里插入图片描述


  • 在pom.xml文件中添加如下依赖即可:
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-configuration-processor</artifactId>
     <optional>true</optional>
</dependency>

2.3 多环境启动

2.3.1 yml文件多环境启动方式

# 设置启用的环境
spring:
  profiles:
    active: pro
---
# 开发
spring:
  profiles: dev
server:
  port: 80
---
# 生产
spring:
  profiles: pro
server:
  port: 81
---
# 测试
spring:
  profiles: test
server:
  port: 82

2.3.2 application.properties文件多环境启动

    • 主启动配置文件application.properties
      • spring.profiles.active=dev
    • 环境分类配置文件application-dev.properties
      • server.port=8080
    • 环境分类配置文件application-pro.properties
    • server.port=8081
    • 环境分类配置文件application-test.properties
    • server.port=8082

2.3.3 多环境命令行启动参数设置

  • 带参数启动SpringBoot
java -jar springboot.jar --spring.profiles.active=test
  • 可通过命令行修改参数
  • 修改端口:
java -jar springboot.jar --spring.profiles.active=test --server.port=88

在这里插入图片描述


2.3.3 多环境开发兼容问题(Maven与boot)

  • Maven中设置多环境属性
    <!--开发环境-->
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
            <prfile.active>dev</prfile.active>
            </properties>
        </profile>
        <!--生产环境-->
        <profile>
            <id>pro</id>
            <properties>
                <prfile.active>pro</prfile.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!--测试环境-->
        <profile>
            <id>test</id>
            <properties>
                <prfile.active>test</prfile.active>
            </properties>
        </profile>
    </profiles>
  • SpringBoot中引用Maven属性
# 设置启用的环境
spring:
  profiles:
    active: ${prfile.active}

---
# 开发
spring:
  profiles: dev
server:
  port: 80
---
# 生产
spring:
  profiles: pro
server:
  port: 81
---
# 测试
spring:
  profiles: test
server:
  port: 82
  • Maven指令执行完毕后,生成了对应的包,其中类参与编译,但是配置文件并没有编译,而是复制到包中
  • 解决思路:对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符
<build>
      <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.4配置文件分类

  • SpringBoot中4级配置文件
    • 1级:file:config/appication.yml(最高)
    • 2级:file:application.yml
    • 3级:classpath:config/application.yml
    • 4级:classpath:application.yml (最低)
  • 作用:
    • 1级与2级留做系统打包后设置通用属性
    • 3级与4级用于系统开发阶段设置统用属性
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶落闲庭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值