SpringBoot-读取配置文件

目录

一、常见位置

二、ideal自定义文件位置

三、启动时指定配置文件

四、通过@PropertySource指定

4.1 读取自定义yml格式配置文件

4.1.1 自定义加载yml类

4.1.2 指定@PropertySource的factory

五、类型安全注入


一、常见位置

在SpringBoot中常见存放application配置文件有4种地方,分别是:

1、项目根目录下创建【config】目录,并存放配置文件

2、在项目根目录下直接创建配置文件

3、在【resources】目录下创建【config】目录,并存放配置文件

4、在【resources】中直接创建配置文件

这四个位置的加载顺序权限,依次减低,即先读取1顺序往下。如图:

二、ideal自定义文件位置

在ideal中可以直接设置自定义配置文件目录。如图:

编辑启动项,在【Environment variables】中添加【spring.config.location=classpath:/】或【spring.config.additional-location=classpath:/】或【spring.config.name=java】,在【resources】下创建目录并添加配置文件。

spring.config.location:表示自己重新定义配置文件的位置,项目启动时就按照定义的位置去查找配置文件,这种定义方式会覆盖掉默认的四个位置

spring.config.additional-location:表示在四个位置的基础上,再添加几个位置,新添加的位置的优先级大于原本的位置.

spring.config.name:直接指定【resources】下配置文件,且自定义文件名称。

三、启动时指定配置文件

即在项目打成jar包后,使用命令进行指定配置文件。

java -jar myproject.jar --spring.config.name=app

app即配置文件的名称,且可以不带上后缀名称

四、通过@PropertySource指定

SpringBoot中使用了@PropertySource来代替Spring中xml中的

< context:property-placeholder location="classpath:xxxxx.properties"/>

@Component
@PropertySource("classpath:xxxx.properties") //指定resources下的xxxx.properties文件
public class User{
	@Value("${userName}")
	private String userName;
}

注:yml配置文件不支持该注解,只能使用application.properties中使用

4.1 读取自定义yml格式配置文件

        @PropertySource注解是不能直接读取.yml上的配置信息,如果要支持,就需要实现自定义配置。

4.1.1 自定义加载yml类

/**
 * @author: jiangjs
 * @description:
 * @date: 2022/12/29 14:46
 **/
public class DefinedYmlConfigFactory extends DefaultPropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        String sourceName = Objects.nonNull(name) ? name: resource.getResource().getFilename();
        return !resource.getResource().exists() ? new PropertiesPropertySource(sourceName, new Properties()) :
                sourceName.endsWith(".yml") || sourceName.endsWith(".yaml") ?
                        new PropertiesPropertySource(sourceName, this.loadYamlConfig(resource)) :
                        super.createPropertySource(name,resource);
    }

    private Properties loadYamlConfig(EncodedResource resource){
        YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
        factoryBean.setResources(resource.getResource());
        factoryBean.afterPropertiesSet();
        return factoryBean.getObject();
    }
}

4.1.2 指定@PropertySource的factory

@Data
@PropertySource(value = "classpath:user.yml",factory = DefinedYmlConfigFactory.class)
@Component
public class UserInfo {
    @Value("${userinfo.user_name}")
    private String userName;
    @Value("${userinfo.login_name}")
    private String loginName;
    @Value("${userinfo.gender}")
    private String gender;
}

五、类型安全注入

多属性时,使用@Value获取值比较麻烦,工作量大,因此可以采用@ConfigurationProperties(),用以减轻工作,减少工作量。

@Component
@PropertySource("classpath:xxxx.properties")
@ConfigurationProperties(prefix = "xxxx")
public  class Book {
	private String userName;
}

注:代码中配置以“xxxx”为前缀的属性,而属性名必须与配置文件中的属性名保持一致,也可以采用骆驼命名方式。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值