-1

I have a file named myProperty.properties

id.apple=20
id.banana=30
id.orange=40
name=fruit

I want to access this property in a service method using Spring's @Value annotation or ExpressionParser.

Method looks like this

public int myMethod(String fruit)
{
    desiredID= "id."+fruit;
    return desiredID;
}

For accessing property name I am using @Value annotation and code looks like this

@Value("${name}")
private String name;

By this I am easily able to access the property name. But how should I access the variable property id.fruiname which fruitname is passed in a method

  • 2
    Does this answer your question? [Read properties by dynamic keys in spring boot](https://stackoverflow.com/questions/39240963/read-properties-by-dynamic-keys-in-spring-boot) – Smile Jan 20 '20 at 08:39
  • I dont want to use Environment or Property object since it is loading twice which is decreasing my project's efficiency of computation. Please give me a solution using Spring's Expression Parser – Tanish Verma Jan 20 '20 at 11:44

1 Answers1

0

You can do

@Autowired
private Environment env;

And then read the property value like

public int myMethod(String fruit)
{
    string desiredID= "id."+fruit;
    return Integer.parseInt(env.getProperty(desiredID));
}
Smile
  • 3,832
  • 3
  • 25
  • 39
  • I dont want to use Environment or Property object since it is loading twice which is decreasing my project's efficiency of computation. Please give me a solution using Spring's Expression Parser – Tanish Verma Jan 20 '20 at 12:14
  • How it is loading twice and decreasing efficiency of computation? – Smile Jan 21 '20 at 04:22