Notes-SpringBoot配置文件-《Java笔记》

admin 2025-10-19 04:50:15 编程 来源:ZONE.CI 全球网 0 阅读模式

Java SpringBoot

1、SpringBoot加载配置文件的值

A.application.yml

  1. fcant:
  2. check:
  3. code: 123456

B.取配置文件值的类

  1. 在类上添加@Component组件注解
  2. 在属性上添加@Value注解用于读取配置文件的值并赋值给对应的属性 ```java package com.fcant.springbootexpand.property;

import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;

/**

  • ValueProperty
  • encoding:UTF-8 *
  • @author Fcant
  • @date 9:23 2019/12/5 */ @Component @Data public class ValueProperty { @Value(“${fcant.check.code}”) private String check_code; }
  1. <a name="dSBay"></a>
  2. ## 2、读取配置文件的值赋给JavaBean属性
  3. <a name="ujFju"></a>
  4. ### A.赋值的JavaBean
  5. 在类上添加组件注解`@Component`以及配置属性注解`@ConfigurationProperties(prefix = "fcant")`用于读取配置文件,并指定前缀
  6. ```java
  7. package com.fcant.springbootexpand.property;
  8. import lombok.Data;
  9. import org.springframework.boot.context.properties.ConfigurationProperties;
  10. import org.springframework.stereotype.Component;
  11. /**
  12. * BeanProperty
  13. * <p>
  14. * encoding:UTF-8
  15. *
  16. * @author Fcant
  17. * @date 9:45 2019/12/5
  18. */
  19. @Data
  20. @Component
  21. @ConfigurationProperties(prefix = "fcant")
  22. public class BeanProperty {
  23. private String tel;
  24. private boolean exist;
  25. }

B.application.yml

  1. fcant:
  2. tel: 17826260016
  3. exist: True

3、加载外部配置文件读取值

A.新建自定义的配置文件fcant.properties(不能为YMAL文件)

  1. fcant.property=Fcant

B.读取外部配置文件的Java类-PropertySourceProperty.java

为该类添加@Component组件注解,以及加载读取外部配置文件的@PropertySource("classpath:fcant.properties")注解,并为之指定类路径的配置文件名称;并在对应的属性上面添加@Value注解

  1. package com.fcant.springbootexpand.property;
  2. import lombok.Data;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.PropertySource;
  5. import org.springframework.stereotype.Component;
  6. /**
  7. * PropertySourceProperty
  8. * <p>
  9. * encoding:UTF-8
  10. *
  11. * @author Fcant
  12. * @date 9:53 2019/12/5
  13. */
  14. @Data
  15. @Component
  16. @PropertySource("classpath:fcant.properties")
  17. public class PropertySourceProperty {
  18. @Value("${fcant.property}")
  19. private String property;
  20. }

C.配置文件没有给变量赋值的在Value注解中定义默认值

给配置文件的KEY定义默认值的在KEY后面加英文冒号和默认值

  1. package com.fcant.springbootexpand.property;
  2. import lombok.Data;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.PropertySource;
  5. import org.springframework.stereotype.Component;
  6. /**
  7. * PropertySourceProperty
  8. * <p>
  9. * encoding:UTF-8
  10. *
  11. * @author Fcant
  12. * @date 9:53 2019/12/5
  13. */
  14. @Data
  15. @Component
  16. @PropertySource("classpath:fcant.properties")
  17. public class PropertySourceProperty {
  18. @Value("${fcant.property:default}")
  19. private String property;
  20. }

4、多环境配置文件以及其加载使用

A.建立多环境配置文件,命名为application-{dev}.yml

image.png

B.在主配置文件中指定加载的文件

  1. spring:
  2. profiles:
  3. active: dev

C.激活使用的配置文件会打印日志

  1. 2019-12-07 16:37:36.170 INFO 15804 --- [ restartedMain] c.f.s.SpringBootExpandApplication : The following profiles are active: dev

D.可以通过命令行参数指定加载的配置文件

5、配置文件加载顺序

  • 命令行参数
  • 当前环境变量或者系统变量中的“SPRING_APPLICATION_JSON”属性
  • ServletConfig 初始化参数
  • ServletContext 初始化参数
  • java:comp/env”中的JNDI属性
  • Java系统变量 “System.getProperties()
  • 操作系统环境变量
  • Jar包外指定的 application-{profile}.properties
  • Jar包内指定的 application-{profile}.properties
  • Jar包外的 application.properties
  • Jar包内的application.properties
  • @PropertySource引入的配置
  • 默认配置属性 “SpringApplication.setDefaultProperties
以太坊cppgolang区别 编程

以太坊cppgolang区别

以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
progolang 编程

progolang

Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
golangn个发送者 编程

golangn个发送者

Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
golang技能图谱 编程

golang技能图谱

从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
评论:0   参与:  0