Spring Boot 读取配置文件

spring boot configration processor not found in classpath

  • 问题描述:使用springBoot时,读取自定义的资源文件时发生错误,表示读取不到资源文件
  • 结局方法:高版本springboot 去除掉了@ConfigurationProperties()中location,所以需要使用@PropertySource注解指定资源文件
    Spring Boot 读取配置文件
    此时运行测可以自动获取到资源文件中值

读取配置文件的2种方式

  • 在常规的Spring配置***入properties文件的值的方式,通过@PropertySource指明properties文件的位置,然后通过@Value注入值,在SpringBoot中只需要在application.properties定义属性,直接使用@Value注入即可
    Spring Boot 读取配置文件
    Spring Boot 读取配置文件

  • 第一种方法使用@Value注入每个配置在实际项目中显然会比较麻烦,因此Spring Boot 提供了类型安全的配置方式通过@ConfigurationProperties 将properties属性和一个Bean及其属性关联

    资源文件:auther.properties
    Spring Boot 读取配置文件

Spring Boot 读取配置文件

PS:如果将配置放在application.properties中则不需要使用@PropertiesSource 指定配置文件的位置,@SpringBootApplication注解将默认去读取application.properties。