如何读取配置文件

1. 读取application.properties或application.yml格式

以application.yml配置文件为例

user:
  id: 1

1.1 使用@Value注解

在类字段或方法参数上使用@Value("${property.name}")来注入属性值。

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@Slf4j
public class DemoTests {
    @Value("${user.id}")
    private Integer id;

    @Test
    public void testGetProperties() {
        log.info("id: {}", id);
    }
}

1.2 使用@ConfigurationProperties注解

  1. 需要创建一个实现类,且实体类中的属性名和配置文件当中key的名字必须要一致
  2. 需要将实体类交给Spring的IOC容器管理,成为IOC容器当中的bean对象
  3. 在实体类上添加@ConfigurationProperties注解,并通过prefix属性来指定配置参数项的前缀

 1.2.3 导入依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

 1.2.2 代码实现

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

@Data
@Component
@ConfigurationProperties(prefix = "user")
public class DemoConfig {
    private String id;
}
import com.demo.config.DemoConfig;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@Slf4j
public class DemoTests {
    @Autowired
    private DemoConfig config;

    @Test
    public void testGetProperties() {
        log.info("id: {}", config.getId());
    }
}

@Value 和@ConfigurationProperties异同:

  • 相同点:都是用来注入外部配置的属性的。

  • 不同点:

    • @Value注解只能一个一个的进行外部属性的注入。
    • @ConfigurationProperties可以批量的将外部的属性配置注入到bean对象的属性中。

 1.3 通过Environment接口

创建一个Java Bean并用@ConfigurationProperties注解绑定配置文件中的属性到Bean的字段。

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;

@SpringBootTest
@Slf4j
public class DemoTests {
    @Autowired
    private Environment environment;

    @Test
    public void testGetProperties() {
        log.info("id: {}", environment.getProperty("user.id"));
    }
}

 2. 读取自定义的properties文件

如果需要从特定的外部properties文件加载属性,可以使用@PropertySource,但这在Spring Boot中不常用,因为它更倾向于从application.properties或yaml文件中加载。

以config.properties文件为例:

user.id=1

 使用@PropertySource读取后,就可以按照读取application.properties或application.yml格式读取配置文件了,这里以@ConfigurationProperties读取配置文件为例。

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Data
@Component
@PropertySource("classpath:config.properties")
@ConfigurationProperties(prefix = "user")
public class DemoConfig {
    private String id;
}
import com.demo.config.DemoConfig;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@Slf4j
public class DemoTests {
    @Autowired
    private DemoCofig config;

    @Test
    public void testGetProperties() {
        log.info("id: {}", config.getId());
    }
}

 

 3. 读取自定义的yml文件

在较早的 Spring Boot 版本中,如果需要从 YAML 文件中读取属性并注入到 @Value 注解或者 Bean 中,可以使用 PropertySourcesPlaceholderConfigurer 和 YamlPropertiesFactoryBean 来实现。但请注意,在 Spring Boot 2.x 及更高版本中,Spring Boot 自动支持 YAML 配置文件,通常不需要这种手动配置。
不过,如果你确实需要使用上述代码来处理 YAML 文件,那么以下是如何使用:

  • 创建一个配置类(如 DemoConfig)并添加一个 @Bean 方法来实例化 PropertySourcesPlaceholderConfigurer。 
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

@Configuration
public class DemoConfig {
    @Bean
    public PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
        factoryBean.setResources(new ClassPathResource("config.yml"));
        configurer.setProperties(factoryBean.getObject());
        return configurer;
    }
}
  • 在 YAML 文件(例如 src/main/resources/config.yml)中定义你的配置项:
user:
  id: 1
  •  按照读取application.properties或application.yml格式读取配置文件了,这里以@ConfigurationProperties读取配置文件为例。
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@Slf4j
public class DemoTests {
    @Value("${user.id}")
    private String id;

    @Test
    public void testGetProperties() {
        log.info("id: {}", id);
    }
}
  • 19
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值