Scala: how to instantiate an object using java.util.Serviceloader

53 views Asked by At

I have a use case that requires me to create instances of an object from JSON string representations. I am following the recommendation for the Scala factory pattern, which is to use a Singleton Object with an apply method that takes the JSON as input parameter and returns the object. I have a further requirement to instantiate these factories using java.util.Serviceloader, which uses Class.newInstance internally to create instances. However the default constructor for objects is private. How can I get around this.

object Factory{ def apply(input: String): MyWidget }

Need to discover and instantiate factory objects using serviceloader

0

There are 0 answers