【问题标题】:Spring boot java.util.MissingResourceException while accessing properties file访问属性文件时Spring启动java.util.MissingResourceException
【发布时间】:2017-07-05 16:37:10
【问题描述】:

在 Spring Boot 应用程序中,我有以下用于访问属性文件(errors.properties)的代码,当我访问该代码时,它会给出以下异常:

exception":"java.util.MissingResourceException","message":"Can't find bundle for base name errors.properties

errors.properties 文件位于 src/main/resources/ 下

下面是代码

@Configuration
@PropertySource("classpath:errors.properties") // tried with both the annotations
@ConfigurationProperties("classpath:errors.properties") //  tried with both the annotations
public class ApplicationProperties {

    public static String getProperty(final String key) {
        ResourceBundle bundle = ResourceBundle.getBundle("errors.properties");
        return bundle.getString(key);
    }
}   

我无法理解为什么它没有拾取资源文件夹下的 errors.properties 文件,有人可以帮我吗?

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    这个问题不是 Spring Boot 特有的,因为异常是由 ResourceBundle 引发的。
    使用 ResourceBundle.getBundle() 方法时,您不应根据documentation 指定文件的扩展名,它将自动附加。

    所以正确的用法是:

    ResourceBundle.getBundle("errors");
    

    注意:对于 Spring Boot 中的本地化,您可能应该使用 MessageSource 而不是 Java ResourceBundle。

    PropertySource 注释可能有效,否则它会在上下文启动时引发异常(因为 ignoreResourceNotFound 未设置为 false),您可以使用 @Value 注释将 error.properties 文件中的值注入任何 Spring bean。 例如

    @Configuration
    @PropertySource("classpath:error.properties")
    public class ApplicationProperties {
    
        @Value("${property.in.errors.properties}")
        private String propertyValue;
    
        @PostConstruct
        public void writeProperty() {
            System.out.println(propertyValue);
        }
    }
    

    如果您看不到属性值,请确保您的 ComponentScan 包含此配置。

    或者,您可以根据 Sudhakar 的回答将 Environment 直接注入 bean 并使用 getProperty()。

    【讨论】:

      【解决方案2】:

      这对于从属性文件中获取值可能很有用。但可能对支持 i18n 没用。

      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.context.annotation.PropertySource;
      import org.springframework.core.env.Environment;
      import org.springframework.stereotype.Component;
      
      /**
       * This class loads the property file and returns the property file values.
       *
       */
      @Component
      @Configuration
      @PropertySource("classpath:errors.properties")
      public class ApplicationProperties {
      
          @Autowired
          private Environment env;
      
          public static String getProperty(final String key) {
              return env.getProperty(key, "");
          }
      }
      

      【讨论】:

      • giving :500,"error":"Internal Server Error","exception":"java.lang.NullPointerException","message":"No message available" ,条目在属性文件,但仍然给出空指针
      • @Autowired 私有静态环境环境; env 变量正在变为 null 而不是自动装配
      • 谢谢它的工作,如果我想用环境变量加载多个属性文件怎么办?我应该在@PropertySource 注释中声明多个文件
      猜你喜欢
      • 1970-01-01
      • 2021-03-15
      • 2021-07-11
      • 2012-09-29
      • 2015-03-22
      • 2011-05-07
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      相关资源
      最近更新 更多