Created
August 24, 2021 14:50
-
-
Save bfreuden/3c370e1214d963b62ee1c96912d4c8dd to your computer and use it in GitHub Desktop.
Toothpick provider discovery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package bfreuden | |
| import toothpick.InjectConstructor | |
| import toothpick.ProvidesSingleton | |
| import toothpick.ktp.KTP | |
| import toothpick.ktp.binding.bind | |
| import toothpick.ktp.binding.module | |
| import javax.inject.* | |
| data class Config(val props: Map<String, String>) | |
| @ProvidesSingleton | |
| @Named("appConfig") | |
| @Singleton // removing it causes kapt failure (java.lang.reflect.InvocationTargetException (no error message)) | |
| @InjectConstructor | |
| class MyAppConfigProvider: Provider<Config> { | |
| override fun get() = Config(mapOf("dpPort" to "12")) | |
| } | |
| @InjectConstructor | |
| class MyApp(@Named("appConfig") val config: Config) | |
| fun main() { | |
| val app = KTP.openRootScope() | |
| .installModules( | |
| module { | |
| // does not work without the binding below | |
| // error: No binding was defined for class testserver.Config and name appConfig in scope class toothpick.Toothpick and its parents [] | |
| bind<Config>().withName("appConfig").toProvider(MyAppConfigProvider::class) | |
| } | |
| ) | |
| .getInstance(MyApp::class.java) | |
| println(app.config) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment