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

SpringBoot 配置⽂件

Spring Boot 提供了一种非常灵活的方式来配置应用程序,这主要是通过使用属性文件来实现的。最常用的配置文件是 application.propertiesapplication.yml,它们通常位于项目的 src/main/resources 目录中。下面,我将介绍如何使用这些配置文件来自定义 Spring Boot 应用程序的行为。\

一、设置配置文件

1. application.properties 和 application.yml

Spring Boot 支持两种类型的配置文件格式:.properties.yml。选择哪一种取决于个人或团队的偏好:

.properties 文件格式:

server.port=8080
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass

.yml 文件格式: 

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost/test
    username: dbuser
    password: dbpass
2. 配置文件的优先级

Spring Boot 允许你在不同位置定义配置文件,并且这些配置文件有一定的优先级顺序。例如,位于 /config 子目录中的配置文件优先级高于位于类路径根目录(/resources/classes)中的配置文件。此外,环境变量和命令行参数可以覆盖这些文件中的属性。

二、Profile-specific 配置

Spring Boot 允许你为不同的环境定义不同的配置,这是通过使用 profile-specific 的配置文件来实现的。例如,你可以有以下文件:

  • application-dev.properties
  • application-test.properties
  • application-prod.properties
1.可以在 application.properties 中设置激活的配置文件:
spring.profiles.active=dev
2.在运行应用时通过命令行参数指定:
java -jar myapp.jar --spring.profiles.active=prod
3.Maven Profiles 的使用

可以在 pom.xml 文件中定义多个 profiles,每个 profile 都可以指定不同的配置参数,包括资源文件的过滤和替换,以及其他构建时的特定设置。这里是一个简单的例子,展示如何使用 Maven Profiles 来管理不同环境的配置:

<project>
    ...
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <activated.profile>dev</activated.profile>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                        <includes>
                            <include>**/application-${activated.profile}.properties</include>
                        </includes>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <activated.profile>prod</activated.profile>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                        <includes>
                            <include>**/application-${activated.profile}.properties</include>
                        </includes>
                    </resource>
                </resources>
            </build>
        </profile>
    </profiles>
    ...
</project>

在这个例子中,我们定义了两个 profiles:devprod。每个 profile 都设置了一个属性 activated.profile,这个属性被用来在资源过滤过程中选择合适的配置文件。

激活 Maven Profiles

你可以在构建时通过命令行参数来激活特定的 Maven profile,从而实现不同环境的配置加载:

mvn package -P dev

三、获取配置文件

Spring Boot 项目中获取配置文件中设置的值是一个常见的需求。Spring Boot 提供了多种方法来读取和使用配置文件(如 application.propertiesapplication.yml)中定义的属性值。这里有几种常用的方法:

1. 使用 @Value 注解

你可以使用 @Value 注解来自动注入配置文件中的属性到 Spring Bean 的字段中。这种方法简单直接,适用于获取单个值。

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

@Component
public class MyComponent {

    @Value("${some.property}")
    private String propertyValue;

    // 使用 propertyValue
}

在这个例子中,some.property 是在 application.properties 中定义的一个属性,其值将被注入到 propertyValue 字段中。

2. 使用 ConfigurationProperties 注解

当你需要读取一组相关的配置或者整个配置文件时,可以使用 @ConfigurationProperties 注解。这种方法允许将配置文件中的属性绑定到一个类的字段上。

首先,你需要定义一个配置属性类:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {

    private String name;
    private int timeout;

    // 省略 getter 和 setter 方法
}

然后在 application.properties 文件中定义相应的属性:

app.name=MyApp
app.timeout=5000
3.使用 @Configuration@Bean

另一种高级用法是通过配置类和 @Bean 方法直接从环境中获取配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Value;

@Configuration
public class AppConfig {

    @Bean
    public MyBean myBean(@Value("${some.property}") String propertyValue) {
        return new MyBean(propertyValue);
    }
}


http://www.kler.cn/news/315930.html

相关文章:

  • 【数据结构与算法】LeetCode:二分查找
  • MATLAB给一段数据加宽频噪声的方法(随机噪声+带通滤波器)
  • 【Go】Go 环境下载与安装教程(Windows系统)
  • 九、成功版--windows上安装artifactory配置postgressql
  • [Redis][环境配置]详细讲解
  • Spark-累加器源码分析
  • JS执行机制(同步和异步)
  • 深度学习入门:探索神经网络、感知器与损失函数
  • html实现TAB选项卡切换
  • LLMs之OCR:llm_aided_ocr(基于LLM辅助的OCR项目)的简介、安装和使用方法、案例应用之详细攻略
  • Python之一些列表的练习题
  • Spring Boot入门:构建你的首个Spring Boot应用
  • Mybatis-plus进阶篇(二)
  • 【JUC并发编程系列】深入理解Java并发机制:线程局部变量的奥秘与最佳实践(五、ThreadLocal原理、对象之间的引用)
  • 数据结构 ——— 常见的时间复杂度计算例题(最终篇)
  • Linux驱动开发 ——架构体系
  • 求最大公约数
  • CSS 布局三大样式简单学习
  • 【解密 Kotlin 扩展函数】命名参数和默认值(十三)
  • 【深入Java枚举类:不仅仅是常量的容器】
  • 数据结构——串的模式匹配算法(BF算法和KMP算法)
  • 设计模式-装饰者模式
  • VMware虚拟机经常性卡死,打开运行一段时间后卡死,CPU占比增至100%
  • 电脑网络怎么弄动态ip :步骤详解与优势探讨
  • Tomcat系列漏洞复现
  • AI时代最好的编程语言应该选择谁?
  • vue h5 蓝牙连接 webBluetooth API
  • MySQL 中删除重复的数据并只保留一条
  • C#实现指南:将文件夹与exe合并为一个exe
  • vscode 环境搭建