5

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.

7
  • maybe @Autowired private Environment env;? I know it's not the same, but... Commented Apr 15, 2021 at 10:32
  • @Berto99 tried already but didn't work! Commented Apr 15, 2021 at 10:34
  • have u tried by annotating @Component on the class? Commented Apr 15, 2021 at 10:36
  • @SudiptaRoy can you show me a sample code? Commented Apr 15, 2021 at 10:38
  • 1
    Please refer this stackoverflow.com/questions/24649062/… Commented Apr 15, 2021 at 10:40

2 Answers 2

10

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
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

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
@KishanSolanki One follow up question, how would you create the AppUtils object in your code?
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.
@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?
But using this approach will potentially introduce bugs if you use application-<profile>.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.
|
3

You can easily achieve this by annotating your 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.