简体   繁体   中英

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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