0

I have my application.properties:

test.md5.params=something1,something4

In my java class I am getting this particular value : and need to create same strings as present in the property file, such as

 public String calculate(RequestClass request)
       {
       List<String> params= //I am getting the values from application.prop  
        **(above part id done)**
       

My Question is below :: now in my params list I have [something1,something4] so I need to concatenate both the String values like below:

       String finalString=request.getSomething1()+request.getSomething4();
       return finalString;
       }

My Question is how to do this dynamically and in my properties file I might receive "n" of something values. Note : I need to make the code such that my class remains constant, if in future I am adding 10 more values in properties files, my final string should be returning like

   String finalString=request.getSomething1()+request.getSomething4()+....all the values.;
Didier L
  • 18,905
  • 10
  • 61
  • 103
Tech Geek
  • 437
  • 1
  • 4
  • 19
  • using `@Value` annotation with SPEL https://stackoverflow.com/a/12580260/9050514 – deadshot Jul 28 '21 at 09:51
  • Does this answer your question? [Reading a List from properties file and load with spring annotation @Value](https://stackoverflow.com/questions/12576156/reading-a-list-from-properties-file-and-load-with-spring-annotation-value) – deadshot Jul 28 '21 at 09:53
  • That is done @deadshot , please read my question again. – Tech Geek Jul 28 '21 at 09:55
  • from params how are you getting `request.getSomethingN()`, I don't see any correlation between these two thing. – Gaurav kumar Singh Jul 28 '21 at 09:57
  • yes, like param is asset_id for ex, so I need to get the value from request : request.getAssetId(); – Tech Geek Jul 28 '21 at 09:59
  • @TechGeek you don't need list in that case `@Value("#{'${test.md5.params}'.replace(',', '')}")` – deadshot Jul 28 '21 at 11:28
  • 1
    What is `RequestClass`? Can't you have a `getValue(String)` method in it? If you change the configuration in your properties, won't you also need to update that class? (side note: I think the Spring part is irrelevant here, you should remove it from the question as it is confusing people, as seen in the comments and the duplicate proposal) – Didier L Jul 28 '21 at 13:54

1 Answers1

0

Through reflection this is possible, below is one implementation.

public String calculate(RequestClass request) throws InvocationTargetException, IllegalAccessException {
    List<String> params = Arrays.asList("something1", "something4");

    // Do your logic to get the method Names from params, below is an simple example - paramsUpdated
    List<String> paramsUpdated = Arrays.asList("getSomething1", "getSomething4");

    // Reflection to get the methods of request class
    Method[] methods = request.getClass().getMethods();
    StringBuilder sb = new StringBuilder();

    for (String param : paramsUpdated) {
        for (Method method : methods) {
            if (param.equals(method.getName())) { 
                sb.append(method.invoke(request));
            }
        }

    }

    return sb.toString();

}