lundi 14 janvier 2019

Calling a repository from string in the Java Spring framework

Is there a way to invoke an interface from a string?

String repName = "package." + modelName + "Repository";

Class<?> repo = Class.forName(repName);
Object obj = repo.getDeclaredConstructor().newInstance();

Method setNameMethod = obj.getClass().getMethod("findById", int.class);
setNameMethod.invoke(obj, 7); // passing 7

This should work for normal methods/classes, but how can I call an interface dynamically?





Aucun commentaire:

Enregistrer un commentaire