1

I am using an interface of a java bean. Most of the times the methods yielded by this interface are good enough for me.

But often enough I need to access the implementations of this interface.

The problem here is that I only have a String key to access a specific property of a implemented java bean.

Problem

In my runtime I have following String variable with the value successPage. I then have an interface Content. This interface does not yield a method called getSuccessPage() but one of its implementations Formular does.

What I would like to do

I want to use some kind of dynamic resolving of those methods. See this pseudo code:

Content content;
String foundValue = propertyResolver.getDynamicValue("successPage", content, String.class)
// this should ultimately call Formular.getSuccessPage() and return it's value

As you can see I do not have to tell the method what type the concrete implementation would have. Because I am not able to explicitly tell the runtime what type of implementation is actually used.


I already heard of reflection and used it by myself. But I also heard of Spring PropertyResolver classes. I tried to use them but was not able to understand their API and search results were not able to clear my sights.

Community
  • 1
  • 1
xetra11
  • 7,671
  • 14
  • 84
  • 159

1 Answers1

1

I'd use apache commons PropertyUtils.

String foundValue = PropertyUtils.getProperty(content, "successPage").toString();
Ahmad Shahwan
  • 1,662
  • 18
  • 29