【问题标题】:Spring Boot - Read custom properties file using Constants classSpring Boot - 使用常量类读取自定义属性文件
【发布时间】:2020-07-09 16:26:17
【问题描述】:

我有一个常量类,其中所有常量都已定义。常量的值位于我的自定义属性文件中,而属性文件位于包中。我正在尝试使用我的常量类读取自定义属性文件中的值。

我的常量类

package com.example.demo.properties

public interface AppConstant {
//my custom constants
String REQUEST_SUCCESS = "REQUEST_SUCCESS";
String INTERNAL_ERROR = "INTERNAL_ERROR";
String REQUEST_FAIL = "REQUEST_FAIL";
}

AppProps.properties

//values for constants in properties file
//package com.example.demo.properties
REQUEST_SUCCESS = Request was successful
INTERNAL_ERROR = Internal Error
REQUEST_FAIL = Request Failed due to error

我正在尝试使用 java.util.Properties 读取属性文件。有人可以告诉我如何使用 Spring Boot 注释读取该值。

【问题讨论】:

  • Spring boot也有@value注解,
  • @emotionlessbananas @Value 应该在常量类中使用吗?
  • 恕我直言,常量接口非常老派。为什么需要它。

标签: java spring spring-boot


【解决方案1】:

AppProps.properties 文件添加到resources 文件夹,然后将下面的注释添加到您想要使用属性的类上方

@PropertySource("classpath:AppProps.properties")

然后像下面这样使用Value注解:

@Value("${REQUEST_SUCCESS}")
private String requestSuccess;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-26
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 2020-02-14
    • 1970-01-01
    相关资源
    最近更新 更多