Spring Boot - 使用 Constants 类读取自定义属性文件 [英] Spring Boot - Read custom properties file using Constants class
问题描述
我有一个常量类,其中定义了所有常量.常量的值位于我的自定义属性文件中,而属性文件位于一个包中.我正在尝试使用我的 Constants 类读取自定义属性文件中的值.
I am having a constants class wherein all the Constants have been defined. The values for the constants lies in my custom properties file and the property file resides in a package. I am trying to read the value in the custom property file using my Constants class.
我的常量类
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
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 注释读取值.
I am trying to read the properties file using java.util.Properties. Can someone show me how the value can be read using Spring Boot annotations.
推荐答案
将 AppProps.properties
文件添加到 resources
文件夹,然后在类上方添加以下注释想使用属性
Add AppProps.properties
file to the resources
folder and then add below annotation to above your class that want to use properties
@PropertySource("classpath:AppProps.properties")
然后使用 Value
注释,如下所示:
and then use Value
annotation like below:
@Value("${REQUEST_SUCCESS}")
private String requestSuccess;
这篇关于Spring Boot - 使用 Constants 类读取自定义属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!