4

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?

5
  • whats d name of d bean which contains the variable stringValue? Jan 17, 2018 at 18:02
  • could u post more code, including the class where u r using @Value and if ur function which returns the value is in another class, the code of that class too. Jan 17, 2018 at 18:04
  • 1
    mkyong.com/spring3/spring-el-method-invocation-example This link will help u get an idea. Jan 17, 2018 at 18:06
  • I have annotated my class where I am using the @Value annotation with the @PropertySource("classpath:config.properties") annotation because I want to read the values from the config.properties file. Jan 17, 2018 at 18:11
  • thats fine. could u post more code? including the classes as i mentioned above. Jan 17, 2018 at 18:15

4 Answers 4

4

@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.

1

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

           }
1

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); 
0

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

where stringvalue is the environment variable.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.