fun start() { engine.start() } } fun main(args: Array) { val engine = Engine() // construct val car = Car(engine) car.start() } Car With DI (Constructor Injection) Engine
start() { engine.start() } } fun main(args: Array) { val car = Car() car.engine = Engine() // construct car.start() } With DI (Field Injection) Car Engine
object ServiceLocator { fun getEngine() : Engine = Engine() } fun main(args: Array) { val car = Car() car.start() } Service Locator Engine Car Service Locator
Hilt public final class ExampleApplication_HiltComponents { @Component(modules = { ActivityRetainedCBuilderModule.class, ... }) @Singleton public abstract static class SingletonC implements SingletonComponent { } ... } // -------------------------------------------------------------------------------- // Generated by Dagger public final class DaggerExampleApplication_HiltComponents_SingletonC { // ઓࢿ ஶపց private static final class SingletonCImpl extends ExampleApplication_HiltComponents.SingletonC { public void injectExampleApplication(ExampleApplication exampleApplication) {} } ... } @HiltAndroidApp - ઓࢿ ஶపցܳ ࢤࢿೠ.
fun provideBar(): Bar = Bar() } // -------------------------------------------------------------------------------- public final class ExampleApplication_HiltComponents { @Component( modules = { ApplicationContextModule.class, ActivityRetainedCBuilderModule.class, FooModule.class, } ) @Singleton public abstract static class SingletonC implements HiltWrapper_ActivityRetainedComponentManager_ActivityRetainedComponentBuilderEntryPoint, SingletonComponent, GeneratedComponent { } }
{ @Provides fun provideBar(): Bar = Bar() } // -------------------------------------------------------------------------------- public final class FooModule_ProvideBarFactory implements Factory<Bar> { public static Bar provideBar() { return Preconditions.checkNotNullFromProvides(FooModule.INSTANCE.provideBar()); } }
@Inject lateinit var bar: Bar } @Module @InstallIn(SingletonComponent::class) object FooModule { @Provides fun provideBar(): Bar = Bar() } Hilt_ExampleApplication.java ExampleApplication_GeneratedInjector.java ExampleApplication_MembersInjector.java
} // -------------------------------------------------------------------------------- public final class ExampleApplication_HiltComponents { @Component(modules = { ... }) @Singleton public abstract static class SingletonC implements HiltWrapper_ActivityRetainedComponentManager_ActivityRetainedComponentBuilderEntryPoint, SingletonComponent, GeneratedComponent, ExampleApplication_GeneratedInjector { } } // -------------------------------------------------------------------------------- public final class DaggerExampleApplication_HiltComponents_SingletonC { private static final class SingletonCImpl extends SingletonC { public void injectExampleApplication(ExampleApplication instance) { } }
bar: Bar } // -------------------------------------------------------------------------------- public final class ExampleApplication_MembersInjector implements MembersInjector<ExampleApplication> { public static void injectBar(ExampleApplication instance, Bar bar) { instance.bar = bar; } } // -------------------------------------------------------------------------------- public final class DaggerExampleApplication_HiltComponents_SingletonC { private static final class SingletonCImpl extends SingletonC { public void injectExampleApplication(ExampleApplication instance) { ExampleApplication_MembersInjector.injectBar( instance, FooModule_ProvideBarFactory.provideBar()); } } ࢤࢿػ Injector ېझܳ ా೧, উ٘۽٘ ېझী ઓࢿ ੑػ.
{ @Inject lateinit var bar: Bar override fun attachBaseContext(base: Context) { super.attachBaseContext(bar.newContext(base)) } } Link: h tt ps://stackove rf low.com/a/64162926