Usman Khaliq
Usman Khaliq

Reputation: 363

Passing a variable to @Value annotation for reading specific property from properties file

In my Spring boot application, a specific value needs to be read from the properties file depending on a string value returned from another function. My properties file is as follows:

A=value_a
B=value_b 

A function returns either A or B, and stores it in a String variable called stringValue . I am looking for doing something along the lines of the following:

@Value(stringValue) 
String propertyValue 

However, I get the following message on my IDE:

Attribute value must be constant

I have tried to convert stringValue to a static final variable, but to no avail.

I do know that I can pass a specific key to be read from the properties file, such as the following:

@Value("${A}")
String valueOfA

My question is whether I can pass a variable to the @Value annotation?

Upvotes: 4

Views: 10811

Answers (4)

Manu Nair
Manu Nair

Reputation: 1

if your variable is another environment variable you can try in this format. @Value("${${stringvalue}}")

where stringvalue is the environment variable.

Upvotes: 0

Usman Khaliq
Usman Khaliq

Reputation: 363

Thanks for the help guys! I read the values from the properties file into a org.springframework.core.io.Resource object, and then used that to retrieve the specific value that I required. The following was how I structured my solution code:

@Value("classpath:config.properties")
private Resource propertiesfile; 

I then declared a java.util.Properties object and read the values from the Resource object into it.

Properties properties = new Properties();

        try{
            properties.load(propertiesfile.getInputStream());
        }catch(IOException e){
            logger.error("Parsing error while reading properties file",e.toString());
        }

Finally, based on the value in my stringValue variable, I read the corresponding value from the properties file

String propertyValue = properties.getProperty(stringValue); 

Upvotes: 1

Indraneel Bende
Indraneel Bende

Reputation: 3486

You can autowire Environment in your application and use it to read from the properties file as it is supposed to be at runtime.( This is assuming all files where your properties are have been added to environment).

I will be posting my answer with assumptions as you have not updated your question, with all information I requested.

So your code would be-

lets call your class where your making a call to a function called getValue to get value of stringValue- Example. Now lets assume you are making a call to the function getValue in a method in the class Example called doSomething().

          class Example{

             @Autowire
             private    Enviornment environment.

            private String propertyValue


            public void doSomething(){

            String value=getValue()// this is where u know whether 
                                              its A or B.
            propertyValue=environment.getProperty(value);

            // do whatever u want know

           }

Upvotes: 1

Alex Savitsky
Alex Savitsky

Reputation: 2371

@Value annotations are resolved during startup, when Spring context is built. Since stringValue would not be available at this time, you can't use it for injection purposes.

An exception to this scenario would be if the bean with @Value annotation is prototype-scoped, in which case a new instance of it would be created any time it's requested. Still, stringValue would need to be available to the Spring context in order to be used at injection point.

Without seeing more code, it's not possible to give you any more detailed answer.

Upvotes: 4

Related Questions