自学内容网 自学内容网

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);
    }
}


原文地址:https://blog.csdn.net/m0_74209411/article/details/142444780

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!