Overriding Bindings based on Services loaded at runtime.

24 views
Skip to first unread message

Jayadev Chandrasekhar

unread,
Sep 19, 2016, 6:53:56 PM9/19/16
to scaldi

Hello Everybody, 

I am trying to load a list of scaldi.Module classes from a jar (exposed as a service), and want to use those Modules to override existing ones in my application at runtime. Is there a way to initialize with an arbitrary list of Modules files loaded at run time. [Context: Moving an existing scala application from Guice, where we used to use the override functionality in Guice to do the same]

Ex:
val overrideModules: ServiceLoader[Module] = ServiceLoader.load(classOf[Module])   //This is fine it gets List of overrides for the Module
val listOfOverrideModules: List[Module] = convertToList(overrideModules)

implicit val appModule: MutableInjectorAggregation = listOfOverrideModules ++
    new SpecificModule ++
    new AnotherSpecificModule
    ....

That of course doesn't work currently, but was trying to figure out what would the right translation of this requirement.  Another option I have is to create a providerFunction during the binding that can do a looking for Overridden services exposed in jars to be used at runtime.

Any precedence , ideas ?
Jayadev

Jayadev Chandrasekhar

unread,
Sep 20, 2016, 3:54:26 PM9/20/16
to scaldi

Found a way, did not look at the obvious, saw that MutableInjectorAggregation has a constructor which takes a List[Injectors] which could be used.

Barys Ilyushonak

unread,
Dec 2, 2016, 5:23:15 AM12/2/16
to scaldi
Hi Jayadev,

that was exactly done in scaldi-play. e.g. https://github.com/scaldi/scaldi-play/blob/master/src/main/scala/scaldi/play/ScaldiBuilder.scala#L143

вторник, 20 сентября 2016 г., 0:53:56 UTC+2 пользователь Jayadev Chandrasekhar написал:
Reply all
Reply to author
Forward
0 new messages