繁体   English   中英

Spring Boot - 使用 Constants 类读取自定义属性文件

[英]Spring Boot - Read custom properties file using Constants class

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

我的常量类

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 注释读取值。

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

@PropertySource("classpath:AppProps.properties")

然后使用如下所示的Value注释:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM