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

【Spring Boot 自定义配置项详解】

文章目录

  • 一、配置文件
    • 1. properties配置
      • 1.1 创建配置文件
      • 1.2 添加配置项
      • 1.3 在应用中使用配置项
      • 1.4 多环境配置
    • 2. YAML配置
      • 2.1 创建配置文件
      • 2.2 添加配置项
      • 2.3 在应用中使用配置项
      • 2.4 多环境配置
  • 二、自定义配置类
    • 1. 创建配置类
    • 2. 使用配置类


一、配置文件

Spring Boot支持多种配置文件格式,包括properties和YAML。可以根据项目需求选择合适的格式。

1. properties配置

1.1 创建配置文件

在src/main/resources目录下创建一个application.properties文件(如果没有则新建)。这是Spring Boot默认读取的配置文件名。

1.2 添加配置项

# 数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=password

1.3 在应用中使用配置项

通过@Value注解或@ConfigurationProperties注解将配置值注入到Spring Bean中:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class DatabaseConfig {@Value("${spring.datasource.url}")private String dbUrl;@Value("${spring.datasource.username}")private String dbUsername;@Value("${spring.datasource.password}")private String dbPassword;// 省略getter和setter方法
}

1.4 多环境配置

可以根据不同的环境(如开发、测试、生产)创建不同的配置文件(如application-dev.propertiesapplication-test.propertiesapplication-prod.properties),并在启动应用程序时通过spring.profiles.active属性来指定使用的环境。

spring.profiles.active=dev

2. YAML配置

2.1 创建配置文件

创建一个application.yml文件,YAML格式的配置文件更易读且支持复杂结构的配置。

2.2 添加配置项

# 数据库连接配置
spring:datasource:url: jdbc:mysql://localhost:3306/mydatabaseusername: rootpassword: password

2.3 在应用中使用配置项

与properties配置类似,通过@ConfigurationProperties注解将配置值注入到Spring Bean中。

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class DatabaseConfig {private String url;private String username;private String password;// 省略getter和setter方法
}

2.4 多环境配置

同样支持多环境配置,可以使用spring.profiles.active属性来指定不同的环境。


二、自定义配置类

除了使用@Value@ConfigurationProperties注解外,还可以通过自定义配置类的方式来管理配置项。

1. 创建配置类

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;@Configuration
@ConfigurationProperties(prefix = "myapp")
public class MyAppConfig {private String apiUrl;private String apiKey;// 省略getter和setter方法
}

2. 使用配置类

直接在需要使用的地方注入该配置类即可:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class MyApiClient {private final MyAppConfig appConfig;@Autowiredpublic MyApiClient(MyAppConfig appConfig) {this.appConfig = appConfig;}public void callApi() {String apiUrl = appConfig.getApiUrl();String apiKey = appConfig.getApiKey();// 使用配置的API URL和API Key进行操作}
}
http://www.lryc.cn/news/405749.html

相关文章:

  • 电机相位接线错误导致的潜在问题
  • react中如何mock数据
  • 通过Faiss和DINOv2进行场景识别
  • 新手入门基础Java
  • 2024最新版虚拟便携空调小程序源码 支持流量主切换空调型号
  • 前端在浏览器总报错,且获取请求头中token的值为null
  • html+css前端作业 王者荣耀官网6个页面无js
  • 在windows上使用Docker部署一个简易的web程序
  • sqlalchemy使用mysql的json_extract函数查询JSON字段
  • 分类模型-逻辑回归和Fisher线性判别分析★★★★
  • JMeter介绍、安装配置以及快速入门
  • GPT LangChain experimental agent - allow dangerous code
  • 1 LableMe安装下载
  • rce漏洞-ctfshow(50-70)
  • vulntarget-a靶机-复现报告
  • 为什么 FPGA 的效率低于 ASIC?
  • 使用水星Mecury人形机器人搭建VR遥操作控制平台!
  • 【学习笔记】无人机系统(UAS)的连接、识别和跟踪(三)-架构模型和概念
  • uniapp bug解决:uniapp文件查找失败:‘uview-ui‘ at main.js:14
  • Python 爬虫(爬取百度翻译的数据)
  • 【LeetCode:2766. 重新放置石块 + 哈希表】
  • [C++]类的自动转换和强制类型转换
  • 根据鼠标所在位置获取组件拿到 “qt_scrollarea_viewport” 组件的问题
  • 深入浅出WebRTC—LossBasedBweV2
  • 就业难?誉天Linux云计算架构师涨薪班,不涨薪退学费
  • 从零开始!Jupyter Notebook的安装教程
  • FastAPI(七十)实战开发《在线课程学习系统》接口开发--留言功能开发
  • 04-数据库MySQL
  • 神经网络理论(机器学习)
  • JNI回调用中不同线程的env无法找到正确的kotlin的class