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


在这里插入图片描述

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

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


SpringBoot简介

  • 一、 SpringBoot概述
    • 1.1 起步依赖
    • 1.2 辅助功能
    • 1.3 SpringBoot程序启动
  • 二、 SpringBoot基础配置
    • 2.1 配置文件格式
      • 2.1.1 自动提示功能消失解决方案
    • 2.2 yaml
      • 2.2.1 yaml语法规则
      • 2.2.2 yaml数据读取方式
        • 2.2.2.1 使用`@Value`读取单个数据,属性名引用方式:${一级属性名.二级属性名......}
        • 2.2.2.2 封装全部数据到`Environment`对象
        • 2.2.2.3 自定义对象封装指定数据
        • 2.2.2.4 自定义对象封装指定数据警告解决方案
    • 2.3 多环境启动
      • 2.3.1 yml文件多环境启动方式
      • 2.3.2 application.properties文件多环境启动
      • 2.3.3 多环境命令行启动参数设置
      • 2.3.3 多环境开发兼容问题(Maven与boot)
    • 2.4配置文件分类

一、 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
  • 参数加载优先级顺序(从上到下优先级递增)
  • 可参考官网https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config

在这里插入图片描述


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级用于系统开发阶段设置统用属性

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/88546.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

前端(十四)——DOM节点操作手册:你需要了解的一切

&#x1f642;博主&#xff1a;小猫娃来啦 &#x1f642;文章核心&#xff1a;DOM节点操作手册&#xff1a;你需要了解的一切 文章目录 前言DOM基础知识操作现有节点创建新节点遍历节点树修改节点属性和样式事件处理实践应用动态创建表格动态更新列表 前言 DOM&#xff08;文档…

Python爬虫逆向实战案例(五)——YRX竞赛题第五题

题目&#xff1a;抓取全部5页直播间热度&#xff0c;计算前5名直播间热度的加和 地址&#xff1a;https://match.yuanrenxue.cn/match/5 cookie中m值分析 首先打开开发者工具进行抓包分析&#xff0c;从抓到的包来看&#xff0c;参数传递了查询参数m与f&#xff0c;同时页面中…

C# 工厂模式

一、概述 工厂模式&#xff08;Factory Pattern&#xff09;是一种创建型设计模式&#xff0c;它提供了一种创建对象的最佳方式。在C#中&#xff0c;工厂模式通过定义一个公共接口或抽象类来创建对象&#xff0c;而具体的对象创建则由工厂类来实现。 工厂模式主要包含三个角色…

opencv-dnn

# utils_words.txt 标签文件 import osimage_types (".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff")def list_images(basePath, containsNone):# return the set of files that are validreturn list_file…

element-ui中的el-table合并单元格

描述&#xff1a; 在写项目的时候有时候会经常遇到把行和列合并起来的情况&#xff0c;因为有些数据是重复渲染的&#xff0c;不合并行列会使表格看起来非常的混乱&#xff0c;如下&#xff1a; 而我们想要的数据是下面这种情况&#xff0c;将重复的行进行合并&#xff0c;使表…

春秋云境:CVE-2022-0543(Redis 沙盒逃逸漏洞)

目录 一、i春秋题目 二、CVE-2022-0543&#xff1a;&#xff08;redis沙盒逃逸&#xff09; 漏洞介绍&#xff1a; 漏洞复现&#xff1a; 一、i春秋题目 靶标介绍&#xff1a; Redis 存在代码注入漏洞&#xff0c;攻击者可利用该漏洞远程执行代码。 进入题目&#xff1a;…

vue 展开和收起

效果图 代码块 <div><span v-for"(item,index) in showHandleList" :key"item.index"><span>{{item.emailFrom}}</span></span><span v-if"this.list.length > 4" click"showAll !showAll">{…

element-ui中的el-table的summary-method(合计)的使用

场景图片&#xff1a; 图片1&#xff1a; 图片2&#xff1a; 一&#xff1a;使用element中的方法 优点&#xff1a; 直接使用summary-method方法&#xff0c;直接&#xff0c;方便 缺点&#xff1a; 只是在表格下面添加了一行&#xff0c;如果想有多行就不行了 1&#xff1a;h…

计算机提示mfc120u.dll缺失(找不到)怎么解决

在计算机领域&#xff0c;mfc120u.dll是一个重要的动态链接库文件。它包含了Microsoft Foundation Class (MFC) 库的特定版本&#xff0c;用于支持Windows操作系统中的应用程序开发。修复mfc120u.dll可能涉及到解决与该库相关的问题或错误。这可能包括程序崩溃、运行时错误或其…

JavaWeb_LeadNews_Day7-ElasticSearch, Mongodb

JavaWeb_LeadNews_Day7-ElasticSearch, Mongodb elasticsearch安装配置 app文章搜索创建索引库app文章搜索思路分析具体实现 新增文章创建索引思路分析具体实现 MongoDB安装配置SpringBoot集成MongoDB app文章搜索记录保存搜索记录思路分析具体实现 查询搜索历史删除搜索历史 搜…

macOS - 安装 Python 及地址

文章目录 Python 官方安装包Pip3Applications - PythonMiniconda多个python环境有多种方式安装 python,比如 Python 官方包、anaconda、miniconda、brew 等 这里记录使用 Python 官方包进行安装,和 miniconda 安装方式,以及安装后 各执行文件、安装包的地址。 明确这些地址后…

linux————Keepalived—web双机热备

一、概述 Keepalived 是一个基于 VRRP 协议来实现的 LVS 服务高可用方案&#xff0c;可以解决静态路由出现的单点故障问题。 原理 在一个 LVS 服务集群中通常有主服务器&#xff08;MASTER&#xff09;和备份服务器&#xff08;BACKUP&#xff09;两种角色的服务器&#xff0c…

MySQL回表是什么?哪些情况下会回表

&#x1f3c6;作者简介&#xff0c;黑夜开发者&#xff0c;全栈领域新星创作者✌&#xff0c;CSDN博客专家&#xff0c;阿里云社区专家博主&#xff0c;2023年6月CSDN上海赛道top4。 &#x1f3c6;数年电商行业从业经验&#xff0c;历任核心研发工程师&#xff0c;项目技术负责…

PHP 安装Composer,vue前端依赖包

电脑安装Composer 官网下载&#xff1a;https://getcomposer.org/Composer-Setup.exe 后端安装&#xff1a; 检查是否安装依赖&#xff1a; 安装Composer install 或 Composer i 前端安装&#xff1a; yarn install 安装依赖

Python Pandas 处理Excel数据 制图

目录 1、饼状图 2、条形统计图 1、饼状图 import pandas as pd import matplotlib.pyplot as plt import numpy as np #from matplotlib.ticker import MaxNLocator # 解决中文乱码 plt.rcParams[font.sans-serif][SimHei] plt.rcParams[font.sans-serif]Microsoft YaHei …

【面试】一文讲清组合逻辑中的竞争与冒险

竞争的定义&#xff1a;组合逻辑电路中&#xff0c;输入信号的变化传输到电路的各级逻辑门&#xff0c;到达的时间有先后&#xff0c;也就是存在时差&#xff0c;称为竞争。 冒险的定义&#xff1a;当输入信号变化时&#xff0c;由于存在时差&#xff0c;在输出端产生错误&…

基于IDEA使用maven创建hibernate项目

1、创建maven项目 2、导入hibernate需要的jar包 <!--hibernate核心依赖--><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>5.4.1.Final</version></dependency><!--…

uni-app 编译报错 Error: pages.json解析失败,不符合 json 规范Unexpected token ‘)‘

问题 使用webstorm开发项目时&#xff0c;打开pages.json习惯性ctrlaltl把代码格式了&#xff0c;然后报错了。 接着使用HBuilder编译&#xff0c;但是一直显示在编译中&#xff0c;完全没有反映。重启编译器与重启电脑都没有用。 没管然后编译报错了。 加上逗号再运行还是报…

taro h5 formData上传图片的坑-Required request part ‘file‘ is not present

描述&#xff1a;用formData上传图片 1、生成formData const formData new FormData() formData.append(file, data) // data是file formData.append(xxx, xxx) // 添加其他参数2、用taro.request请求 Taro.request({url: xxxx,data: formData,header: {Content-Type: mult…

本地搭建CFimagehost私人图床【公网远程访问】

文章目录 1.前言2. CFImagehost网站搭建2.1 CFImagehost下载和安装2.2 CFImagehost网页测试2.3 cpolar的安装和注册 3.本地网页发布3.1 Cpolar临时数据隧道3.2 Cpolar稳定隧道&#xff08;云端设置&#xff09;3.3.Cpolar稳定隧道&#xff08;本地设置&#xff09; 4.公网访问测…
最新文章