Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...