当前位置: 首页 > news >正文

SpringBoot的配置文件

一、 Springboot的热部署

        spring为开发者提供了一个名为spring-boot-devtools的模块来使Spring Boot应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用。

        需要我们在POM.XML文件中引入依赖

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

        修改java代码或者配置文件模板后可以通过ctrl+f9来实施热部署。

二、配置文件

        SpringBoot使用全局的配置文件,配置文件的名称是固定不变的,但是有两种格式他们分别是

application.yamlapplicatioin.properties

        二者是互补的优先级是 application.properties>application.yaml

         配置文件的作用:修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好;

 三、YAML语法

        1. 缩进:YAML使用空格进行缩进,而不是制表符。缩进表示层级关系,通常使用两个或四个空格。缩进必须一致,否则会导致解析错误。

        2. k:(空格)v:表示一对键值对(空格必须有;

        3.  普通的值(数字,字符串,布尔):K:V 直接来写,字符串默认不加单引号或者双引号。(对于加了双引号的字符串打印出来 例如"hello \n"会换行,但是单引号就不会出现这种情况)

                                

        4.  对象、Map(属性和值)(键值对)对象还是使用K:V 的方式

person:   #对象名email: exampleex@ample.com  #属性:(空格)属性值last-name: "hello \n"age: 18lists:lisizhaoliudog:name: 花花age: 12

                用- 值表示数组中的一个元素

三、 配置文件注入

        我们可以导入配置文件处理器,以后编写配置就有提示了

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring‐boot‐configuration‐processor</artifactId><optional>true</optional>
</dependency>
#application.yaml
person:email: exampleex@ample.comlast-name: "hello \n"age: 18lists:lisizhaoliudog:name: 花花age: 12
server:port: 8081

        @Value获取值和@ConfigurationProperties获取值比较

4.1 @value注入值

package com.qcby.springboot0720.entity;public class Dog {private  String name;private  Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +'}';}
}
package com.qcby.springboot0720.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component//@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {@Value("张三")
//    @Value("${person.last-name}")private String lastName;private Integer age;
//    @Email(message = "邮箱格式无效")private String email;private Boolean boss;
//    @Value("2025/11/11")private Date birthday;private Map<String,Object> maps;private List<Object> lists;private Dog dog;@Overridepublic String toString() {return "Person{" +"lastName='" + lastName + '\'' +", age=" + age +", email='" + email + '\'' +", boss=" + boss +", birthday=" + birthday +", maps=" + maps +", lists=" + lists +", dog=" + dog +'}';}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Boolean getBoss() {return boss;}public void setBoss(Boolean boss) {this.boss = boss;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Map<String, Object> getMaps() {return maps;}public void setMaps(Map<String, Object> maps) {this.maps = maps;}public List<Object> getLists() {return lists;}public void setLists(List<Object> lists) {this.lists = lists;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}
}

        测试类

package com.qcby.springboot0720; import com.qcby.springboot0720.entity.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonTest { @Autowiredprivate Person person;@Testpublic void testPersonInjection() {System.out.println(person.toString());}
}

 将先有的注释改为非注释

@ConfigurationProperties(prefix = "person")

从配置文件前缀是person中拿数据

 三、配置文件占位符

1、随机数

${random.value}${random.int}${random.long}

${random.int(10)}${random.int[1024,65536]}

2、占位符获取之前配置的值,如果没有可以是用:指定默认值 

person.last‐name=张三${random.uuid}

person.age=${random.int}

person.birth=2017/12/15

person.boss=false

person.maps.k1=v1

person.maps.k2=14

person.lists=a,b,c

person.dog.name=${person.hello:hello}_dog

person.dog.age=15

四、Profile

1、多Profile文件

        我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml

默认使用application.properties的配置;

2yml支持多文档块方式

3、激活指定profile 

  1. 在配置文件中指定 spring.profiles.active=dev
  2. 命令行:
  3. java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
  4. 可以直接在测试的时候,配置传入命令行参数
  5. 虚拟机参数;
  6. -Dspring.profiles.active=dev
http://www.dtcms.com/a/288871.html

相关文章:

  • 【micro:bit】从入门到放弃(四):高级指令:函数、数组、文本、高级LED、引脚配置
  • UE蒙太奇和动画序列有什么区别?
  • 早期SD模型控制生成方法
  • Dev-C++——winAPI贪吃蛇小游戏
  • 智能制造之物料详解
  • 物联网安装调试-温湿度传感器
  • 数学专业转行做大数据容易吗?需要补什么?
  • 高性能熔断限流实现:Spring Cloud Gateway 在电商系统的实战优化
  • 本地部署 Claude 大语言模型的完整实践指南
  • 从磁记录到数据中心:磁盘原理与服务器架构的完整技术链路
  • 【C++基础】面试高频考点解析:extern “C“ 的链接陷阱与真题实战
  • 【原创】微信小程序添加TDesign组件
  • 网络安全初级(前端页面的编写分析)
  • 手写tomcat
  • JAVA学习-练习试用Java实现“贝叶斯网络:实现一个简单的贝叶斯网络用于因果推理”
  • Transient Storage
  • Redis通用常见命令(含面试题)
  • [硬件电路-51]:晶体管既可以用于模拟电路芯片,也可以用于数字电路芯片,晶体管应用在这两个领域的相同点和本质区别?
  • Linux之dpkg--命令的用法
  • Apache基础配置
  • DOM型XSS破坏
  • gcc 和 g++ 的区别
  • Pycaita二次开发基础代码解析:材料属性管理与焊点坐标导出实战指南
  • Go实现用户登录小程序
  • WPF学习笔记(28)Interaction.Triggers的意义与使用方式
  • Java设计模式之行为型模式(备忘录模式)应用场景分析
  • HTML和CSS快速入门
  • 【算法】二分查找经典例题
  • 2025最新 PostgreSQL17 安装及配置(Windows原生版)
  • 大语言模型调用方式与函数调用