-
I have an Interface in my project.
-
End users can implement this interface and can create their concrete implementations.
-
All the users have been advised to follow singleton design pattern while creating the implementations.
-
Users would store the implementations in a properties file.
-
I use Java Reflection to load the classes as shown below.
-
The above method has been very bad for the performance and hence I have decided to move the loading part to Java ServiceLoader instead for various reason.
-
But, ServiceLoader requires a public no-args constructor to load the classes which is quite a hurdle.
-
Is there a way to achieve the class loading using Java ServiceLoader with private constructor in place, or can I make the singleton implementations safe with a public no-args constructor.
I have read the documentation, and googled the probable solutions but found nothing substantial.
How classes are loaded now:
Class klass = Class.forName(klassName);
Method method = klass.getDeclaredMethod("getInstance");
Object object = method.invoke(null);
I just want to get rid of this, properties file and move on to Java ServiceLoader like I have moved for other classes which were not singletons.