Springboot加载自定义yml文件配置的方法

本文详细介绍了在SpringBoot 2.1.5.RELEASE中加载自定义yml配置文件的三种方法:1) 使用@PropertySource + @Value,2) 结合@ConfigurationProperties + @PropertySource + @Value,3) 利用YamlPropertiesFactoryBean + @Value。每种方法都有对应的配置类和测试用例,并展示了各自的输出结果。需要注意的是,不论哪种方式,所有@Value注解的参数都应在yml文件中定义,否则会引发异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文讲解到的知识点是基于Springboot 2.1.5.RELEASE的版本。

首先先在resources目录下创建一个测试用的yml配置文件,内容如下:

PS:注意yml文件中key: value的冒号后面是有一个空格的

system:
  user:
    name: zjhuang
    password: 123456
    age: 25

一. @PropertySource + @Value

  • 配置参数类代码
  1. @PropertySource: 为了告知springboot加载自定义的yml配置文件,springboot默认会自动加载application.yml文件,如果参数信息直接写在这个文件里,则不需要显式加载。
  2. @Value: 指定目标属性在yml’文件中的全限定名。
  3. @Component: 作用是将当前类实例化到spring容器中,相当于xml配置文件中的<bean id="" class=""/>
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource(value = "classpath:test.yml")
public class YamlProperties {

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

    // Setter...
    // Getter...
    // toString...
}
  • 测试用例代码:

后面的两种方式也共用这个测试用例代码,下面不再列出。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestYamlLoader {

    @Autowired
    private YamlProperties yamlProperties;

    @Test
    public void test() {
        System.out.println(yamlProperties.toString());
    }
}
  • 输出结果:
YamlProperties{name='zjhuang', password='123456', age=25}

二. @ConfigurationProperties + @PropertySource + @Value

  • 配置参数类代码:

添加了@ConfigurationProperties注解,并且设置了prefix前缀属性的值,这样@Value只需要指定参数名称,省略了前缀路径。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource(value = "classpath:test.yml")
@ConfigurationProperties(prefix = "system.user")
public class YamlProperties {

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

    // Setter...
    // Getter...
    // toString...
}
  • 输出结果:
YamlProperties{name='zjhuang', password='123456', age=25}

三. YamlPropertiesFactoryBean + @Value

  • PropertySourcesPlaceholderConfigurer类代码
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

@Component
public class PropertySourcesPlaceholderConfigurerBean {
    @Bean
    public PropertySourcesPlaceholderConfigurer yaml() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("test.yml"));
        configurer.setProperties(yaml.getObject());
        return configurer;
    }
}
  • 配置参数类代码

这第三种方法去掉了@PropertySource 注解,改为定义一个PropertySourcesPlaceholderConfigurer类型的@Bean来加载配置文件信息。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

@Component
public class YamlProperties {
    
    @Value("${system.user.name}")
    private String name;
    @Value("${system.user.password}")
    private String password;
    @Value("${system.user.age}")
    private int age;

    // Setter...
    // Getter...
    // toString...

}
  • 输出结果:
YamlProperties{name='zjhuang', password='123456', age=25}

总结
以上就是springboot加载自定义yml配置信息的三种方法,大家按照自己喜好选择使用即可;第三种方法好像只能用于加载一个yml文件的情况,第一和第二种方法可以实现加载多个yml文件,因为@PropertySources的value参数是支持数组赋值的。

@PropertySource(value = {"classpath:test1.yml", "classpath:test2.yml"})

另外需要提一点的是,不管是加载几个yml文件,@Value修饰的所有参数都必须在yml文件中定义齐全,yml缺少配置的话会抛出无法解析占位符的异常。

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'age' in value "${age}"
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值