2. springboot基础配置

本文详细介绍了SpringBoot的基础配置,包括配置文件格式、YAML语法、多环境配置以及配置文件加载顺序。特别地,讲解了如何在Java程序中读取YAML数据,如何实现多环境启动,并探讨了配置文件的分类和优先级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 配置文件格式

1.1 以修改服务器端口为例

SpringBoot提供了多种属性配置方式

  • application.properties
server.port=80
  • application.yml
server:
  port: 81
  • application.yaml
server:
  port: 82

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

image-20221227233642751 image-20221227233957752

1.3 SpringBoot配置文件加载顺序

  • application.properties > application.yml > application.yaml

2. yaml

2.1 yaml语法规则

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

2.2 yaml数组数据

  • 数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔
name: 坤坤

idol:
  name: 坤坤
  age: 18
  specialities:
    --- rap

2.3 在java程序中读取yaml中的数据

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

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



    @Value("${idol.specialities[0]}")
    private String specialities;

2.4 封装全部数据到Environment对象

2.5 自定义对象封装指定数据

2.5.1 yml中的数据
idol:
  name: 坤坤
  age: 18
  specialities:
    --- rap
2.5.2 定义成bean,使用@ConfigurationProperties指定前缀
@Data
@Component
@ConfigurationProperties(prefix = "idol")
public class Idol {
    
    private String name;
    private int age;
    private String[] specialities;
    
}

2.5.3 在需要的地方直接注入使用
    @Autowired
    private Idol idol;

   

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

    @Value("${idol.specialities[0]}")
    private String specialities;


    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {
        System.out.println("id ==> " + id);
        System.out.println("姓名:" + name);
        System.out.println("特长:"+ specialities);

        System.out.println("练习时常俩年半的idol:"+ idol);
        return "hello , spring boot! ";
    }
2.5.4 浏览器访问web工程,后台打印结果

image-20221228001158245

3. 多环境开发配置

3.1 yaml文件多环境启动

#启动指定环境
spring:
  profiles:
    active: test

---
#生产环境
spring:
  profiles: pro
server:
  port: 8080

---
#开发环境
spring:
  profiles: dev
server:
  port: 8081
---

#测试环境
spring:
  profiles: test
server:
  port: 8083

比如说现在启动指定了test环境,看看tomcat启动显示的端口

image-20221229112026714

3.2 properties文件多环境启动

主启动配置文件 application.properties

spring.profiles.active=pro
环境分类配置文件 application-pro.properties
server.port=80
环境分类配置文件 application-dev.properties
server.port=81
环境分类配置文件 application-test.properties
server.port=82

4. 多环境启动命令格式

  • 带参数启动SpringBoot
java –jar springboot.jar --spring.profiles.active=test
java –jar springboot.jar --server.port=88
java –jar springboot.jar --server.port=88 --spring.profiles.active=test

5. 多环境开发控制

Maven与SpringBoot多环境兼容(步骤)

①:Maven中设置多环境属性

<profiles>
    <profile>
        <id>dev_env</id>
        <properties>
            <profile.active>dev</profile.active>
        </properties>
        
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
  
    <profile>
        <id>pro_env</id>
        <properties>
            <profile.active>pro</profile.active>
        </properties>
    </profile>
    
    <profile>
        <id>test_env</id>
        <properties>
            <profile.active>test</profile.active>
        </properties>
    </profile>
</profiles>

②:SpringBoot中引用Maven属性

image-20221229131202622

③:执行Maven打包指令

  • Maven指令执行完毕后,生成了对应的包,其中类参与编译,但是配置文件并没有编译,而是复制到包中

    image-20221229131309427

解决思路:对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符

④:对资源文件开启对默认占位符的解析

<build>
    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
                <encoding>utf-8</encoding>
                <useDefaultDelimiters>true</useDefaultDelimiters>
            </configuration>
        </plugin>
    </plugins>
</build>
  • Maven打包加载到属性,打包顺利通过
  • image-20221229131341948

6. 配置文件分类

  • SpringBoot中4级配置文件

    1级: file :config/application.yml 【最高】

    2级: file :application.yml

    3级:classpath:config/application.yml

    4级:classpath:application.yml 【最低】

  • 作用:
    1级与2级留做系统打包后设置通用属性
    3级与4级用于系统开发阶段设置通用属性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

糖分你俩颗~~~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值