Spring学习——SpringBoot基础配置

配置格式

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

  • application.properties

    server.port=80
    
  • application. yml

    server:
    	port: 81
    
  • application.yaml

    server:
       port: 82
    

SpringBoot配置文件加载顺序(了解):
application.properties > application.yml > application.yaml

yaml

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

yaml语法规则

enterprise:
  name: jihua
  age: 16
  tel: 4006184000
  likes:
    --- rap
    - 篮球
  • 大小写敏感

  • 属性层级关系使用多行描述,每行结尾使用冒号结束

  • 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)

  • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)

  • #表示注释

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

yaml数据读取

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

1

  • 封装全部数据到Environment对象

2

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

3

自定义对象封装数据警告解决方案:

在pom文件中添加如下依赖:

<dependency>
	<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

多环境开发

使用.yml配置文件

  • 使用---区分

4

5

使用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
    

多环境启动命令格式

  • 带参数启动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
    

参数加载优先级顺序

参考官方文档Core Features (spring.io)

6

Maven与SpringBoot多环境兼容

  1. Maven中设置多环境属性(pom.xml文件中)
<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>
  1. SpringBoot中引用Maven属性(properties.yml)
spring:
	profiles:
		active: ${profile.active}
---
spring:
	profiles: pro
server:
	port: 80
---
spring:
	profiles: dev
server:
	port: 81
---
spring:
	profiles: test
server:
	port: 82
  1. 对资源文件开启对默认占位符的解析
<build>
	<plugins>
		<plugin>
			<artifactId>maven-resources-plugin</artifactId>
            <configuration>
				<encoding>utf-8</encoding>
				<useDefaultDelimiters>true</useDefaultDelimiters>
            </configuration>
		</plugin>
	</plugins>
</build>

配置文件分类

  • SpringBoot中4级配置文件

    • 1级: file : config/application. yml【最高】
    • 2级: file : application.yml
    • 3级: classpath: config /application.yml
    • 4级: classpath: application.yml【最低】

    file指打包成jar包后,jar包运行目录

    classpath指未打包时的resources目录

  • 作用:

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

在SpirngBoot 2.5.0和2.4.6中,file : config目录(jar运行目录下的config目录下)下必须存在一个子目录,否则程序会报错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值