SpringBoot3如何读取配置文件application.properties的属性值

Spring Boot 项目中,可以通过以下几种方式读取 application.properties 文件中的配置属性值:


1. 使用 @Value 注解读取单个属性值

@Value 注解可以直接用于注入 application.properties 文件中的属性值。

示例

假设在 application.properties 中定义了以下配置项:

app.name=MyApp
app.version=1.0.0

可以在任意 @Component@Service@Controller 等注解标注的类中,通过 @Value 注解获取这些值:

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

@Component
public class AppConfig {

    @Value("${app.name}")
    private String appName;

    @Value("${app.version}")
    private String appVersion;

    public void printConfig() {
        System.out.println("App Name: " + appName);
        System.out.println("App Version: " + appVersion);
    }
}

注意${app.name} 表示从配置文件中读取 app.name 属性值。


2. 使用 @ConfigurationProperties 注解读取配置前缀

@ConfigurationProperties 注解允许批量读取具有相同前缀的属性,并将其注入到一个 Java 类中。这种方式适用于管理大量的配置项。

示例

假设在 application.properties 中定义了一组 app 前缀的属性:

app.name=MyApp
app.version=1.0.0
app.description=A sample application

可以创建一个配置类,并使用 @ConfigurationProperties 注解将 app 前缀的属性注入其中:

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

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

    private String name;
    private String version;
    private String description;

    // Getter and Setter methods

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

在其他类中可以通过注入 AppConfigProperties 类来获取配置值:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppService {

    private final AppConfigProperties appConfigProperties;

    @Autowired
    public AppService(AppConfigProperties appConfigProperties) {
        this.appConfigProperties = appConfigProperties;
    }

    public void printAppInfo() {
        System.out.println("App Name: " + appConfigProperties.getName());
        System.out.println("App Version: " + appConfigProperties.getVersion());
        System.out.println("App Description: " + appConfigProperties.getDescription());
    }
}

注意@ConfigurationProperties 注解类必须是一个 @Component,并且需要在 application.properties 中启用属性绑定支持,通常在 Spring Boot 中默认已启用。


3. 使用 Environment 读取属性

Environment 接口提供了动态获取属性值的方式,适合用于在运行时读取配置文件中的属性。

示例
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class EnvironmentConfig {

    private final Environment environment;

    @Autowired
    public EnvironmentConfig(Environment environment) {
        this.environment = environment;
    }

    public void printEnvConfig() {
        String appName = environment.getProperty("app.name");
        String appVersion = environment.getProperty("app.version");

        System.out.println("App Name: " + appName);
        System.out.println("App Version: " + appVersion);
    }
}

注意Environment 接口适合在需要动态获取配置或配置项不确定的场景中使用。


4. 使用 @PropertySource 加载自定义配置文件

除了 application.properties,也可以定义自定义配置文件,并使用 @PropertySource 注解加载其中的属性。

示例
  1. 创建自定义配置文件 custom-config.properties,内容如下:

    custom.property1=value1
    custom.property2=value2
    
  2. 使用 @PropertySource 注解加载自定义配置文件:

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    @Configuration
    @PropertySource("classpath:custom-config.properties")
    public class CustomConfig {
    
        @Value("${custom.property1}")
        private String property1;
    
        @Value("${custom.property2}")
        private String property2;
    
        public void printCustomConfig() {
            System.out.println("Custom Property 1: " + property1);
            System.out.println("Custom Property 2: " + property2);
        }
    }
    

注意:确保 custom-config.properties 文件位于 classpath 下(如 src/main/resources 目录)。


总结

以上是 Spring Boot 中读取 application.properties 文件属性值的几种常见方式:

  1. @Value 注解:直接读取单个属性值。
  2. @ConfigurationProperties 注解:批量读取具有相同前缀的属性。
  3. Environment 接口:动态读取属性,适合运行时获取。
  4. @PropertySource 注解:加载自定义配置文件的属性值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WiFiMing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值