Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

java - Spring Boot - Read custom properties file using Constants class

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.

my Constant 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

//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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")

and then use Value annotation like below:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...