Search code examples
spring-bootstatic-variablesapplication.properties

how to access environment variables from single class as static & final constants


I had a utility file which has a list of final static constants that is being referred everywhere in the project. Now I need the constants to be referred from application.properties, since they are static i couldn't do @Value, any easier approach that has anything to do with the constants file wihtout impacting its usage areas

tried environment.getProperty("") but that has the same problem as @Value


Solution

  • You can to try this approach:

    @Value("${name}")
    private String name;
    
    private static String NAME_STATIC;
    
    @Value("${name}")
    public void setNameStatic(String name){
        PropertyController.NAME_STATIC = name;
    }