SpringBoot读取配置文件的几种方式

示例

user:
  name: zhaotian
  age: 18
  sex: 男

@Value注解

@Value注解是Spring框架提供的用于注入配置属性值的注解,它可用于类的成员变量、方法参数和构造函数参数上。

@Data
@Component
public class MyBean {

    @Value("${user.name}")
    private String name;
    @Value("${user.age}")
    private int age;
}

调用方式

@Service
public class Test {

    @Autowired
    private MyBean myBean;

    public void test(){
       String name = myBean.getName();
    }
}

静态变量static

@Data
@Component
public class MyBean {

    public static String name;
    @Value("${user.name}")
    public void setName(String name){
       this.name = name;
    }
    // 调用方式:MyBean.name;

    private static Integer age;
    @Value("${user.age}")
    public void setAge(Integer age){
       this.age = age;
    }
    public Integer getAge(){
       return age;
    }
    // 调用方式:MyBean.getAge();
}

@ConfigurationProperties注解

@ConfigurationProperties注解是 SpringBoot 提供的一种更加便捷来处理配置文件中的属性值的方式,可以通过自动绑定和类型转换等机制,将指定前缀的属性集合自动绑定到一个Bean对象上。

@Data
@Component
@ConfigurationProperties(prefix = "user")
public class MyBean {

    private String name;
    private int age;

    // 调用方式如同@Value()一样
}

静态变量static

@Data
@Component
@ConfigurationProperties(prefix = "user")
public class MyBean {

    public static String name;
    public void setName(String name){
       this.name = name;
    }
    // 调用方式:MyBean.name;

    private static Integer age;
    public void setAge(Integer age){
       this.age = age;
    }
    public Integer getAge(){
       return age;
    }
    // 调用方式:MyBean.getAge();
}

@PropertySources 注解

使用自定义的配置文件,自定义的配置文件无法被应用自动加载,需要我们手动指定加载。
@PropertySources 注解只内置了PropertySourceFactory适配器,也就是说它只能加载.properties文件。
如果你想要加载一个.yaml类型文件,则需要自行实现yaml的适配器。

使用示例

在 src/main/resources/ 目录下创建自定义配置文件 redis.properties

redis.ip=127.0.0.1
redis.port=6379

在需要使用自定义配置文件的类上添加 @PropertySource 注解。

@Data
@Configuration
@PropertySource("classpath:classpath:redis.properties")
public class RedisConfig {

    @Value("${redis.ip}")
    private String ip;

    @Value("${redis.port}")
    private String port;
}

还可以指定多个配置文件,用逗号隔开。如下:

@PropertySources({
    @PropertySource(value = "classpath:redis.properties",encoding = "utf-8"),
    @PropertySource(value = "classpath:mysql.properties",encoding = "utf-8")
})
public class TestConfig {
}

@YamlComponent注解

如果yml文件中用---分隔了多个文档,我们可以使用@YamlComponent注解将每份文档映射到一个bean上,如:

user:
  name: jack
---
user:
  name: mary
@Component("first")
@YamlComponent(value = "user.first")
public class FirstProps {
    private String name;
}

@Component("second")
@YamlComponent(value = "user.second")
public class SecondProps {
    private String name;
}

自定义读取

如果上边的几种读取配置的方式你都不喜欢,我们可以直接注入PropertySources获取所有属性的配置队列。

@Slf4j
@SpringBootTest
public class CustomTest {

    @Autowired
    private PropertySources propertySources;

    @Test
    public void customTest() {
        for (PropertySource<?> propertySource : propertySources) {
            log.info("自定义获取 配置获取 name {} ,{}", propertySource.getName(), propertySource.getSource());
        }
    }
}
posted @ 2024-02-02 16:34  司码易  阅读(56)  评论(0编辑  收藏  举报