简体   繁体   中英

Load static variables values from properties file

I have a class that stores the different endpoints for an API. The class looks like the following:

public class APIEndpoints {

    public static String LOG_IN =  "/api/auth/login";
    public static String LOG_OUT= "/api/auth/logout";
    public static String GET_INSTANCE ="/api/{objectID}/instances?offset={offset}&limit{limit}";

    public static String getInstance(String reportID, int offSet, int limit){
            return GET_INSTANCE.replace("{reportID}",reportID)
                                      .replace("{offset}", String.valueOf(offSet))
                                      .replace("{limit}", String.valueOf(limit));
    }
}

I would like that the endpoints URLs ("api/auth/login" for example), are loaded from a file, like a endpoints.properties.

I'm using SpringBoot, but it does not allow to inject values on static variables.

What would be the 'most' elegant solution to solve this? How would you approach it?

Thank you.

You can access it by using @Value Annotation as below

@Value("${your.property.name}")
private String property;

and in your endpoints.properties file you have to define It like

your.property.name=propertyValue

The question is already answered: link

Static @Value fields are not recommended, but here is how you can do it:

@Value("${url}")
public void setUrl(String url) {
    APIEndpoints.url = url;
}  

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