4

I know how I can access the application.properties values in @Service classes in Java Spring boot like below

@Service
public class AmazonClient {

    @Value("${cloud.aws.endpointUrl}")
    private String endpointUrl;
}

But I am looking for an option to access this value directly in any class (a class without @Service annotation)

e.g.

public class AppUtils {
      @Value("${cloud.aws.endpointUrl}")
      private String endpointUrl;
}

But this returns null. Any help would be appreciated. I have already read here but didn't help.

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82

2 Answers2

9

There's no "magic" way to inject values from a property file into a class that isn't a bean. You can define a static java.util.Properties field in the class, load values from the file manually when the class is loading and then work with this field:

public final class AppUtils {
    private static final Properties properties;

    static {
        properties = new Properties();

        try {
            ClassLoader classLoader = AppUtils.class.getClassLoader();
            InputStream applicationPropertiesStream = classLoader.getResourceAsStream("application.properties");
            applicationProperties.load(applicationPropertiesStream);
        } catch (Exception e) {
            // process the exception
        }
    }
}
Igor Amelin
  • 295
  • 2
  • 9
  • This worked. But everytime when any function called from AppUtils class, then I am accessing like this properties.getProperty("cloud.aws.endpointUrl") , I hope this doesn't lead to memory leak – Kishan Solanki Apr 15 '21 at 11:17
  • @KishanSolanki One follow up question, how would you create the `AppUtils` object in your code? – code_mechanic Apr 15 '21 at 11:36
  • 1. This code doesn't lead to memory leak. The `static` block where we load properties from the file runs only once when this class is loaded by JVM. So if you start your application and use this class, the last one shouldn't be loaded again. – Igor Amelin Apr 15 '21 at 13:41
  • 2. We can add static methods to get property from this `property` field, and then call this method in code: `var propertyValue = AppUtils.getSomePropertyValue()` – Igor Amelin Apr 15 '21 at 13:42
  • @IgorAmelin there seems to be an error in the code provided, shouldn't it be properties.load(applicationPropertiesStream); instead? Also are you able to update your answer and include the static method you made mention of above? – stebak Feb 23 '22 at 16:30
  • 2
    But using this approach will potentially introduce bugs if you use `application-.properties` where Spring intelligently reads properties that are overridden based on what profile is loaded. In this suggested utility, it will only ever look in the root `application.properties` file. – Steve Jan 05 '23 at 21:34
2

You can easily achievw this by annotating ur app utils class with @component annotation . spring will take care of loading properties.

But if you don't want to do that approach , then look at the link below .

https://www.baeldung.com/inject-properties-value-non-spring-class