How to Inject application context from 'app' module to 'network' module using Koin DI

applicationcontext
spring get application context in main method
autowired applicationcontext is null
autowire applicationcontext
applicationcontextaware
spring application context singleton
spring get application context singleton
spring application context load-on startup

I'm developing an application based on Koin DI (ver : 1.0.1) with 2 modules(:app and :network). I have a requirement in :network module to have "Context". Below is how I implemented:

**Module**:
val appModule = module {
    viewModel { LoginViewModel(get()) }
}

**Activity**:
private val viewModel by viewModel<LoginViewModel>()

**ViewModel**:
class LoginViewModel(val context: Context): ViewModel() {
  ...
  // Send "context" to network class in :network module
  ...
 }

Question: Is there any way we can directly send context to network class in :network module?

Both answers by @Rajat and @Andrey are correct. In fact if you look at the sources, you will see that androidContext() is just an extension function to get(), so these 2 definitions are identical:

val appModule = module {
    viewModel { LoginViewModel(get()) }
}

...

val appModule = module {
    viewModel { LoginViewModel(androidContext()) }
}

Answering your question, since get() and androidContext() are members of the module DSL object, you could do this:

val networkModule = module {
   single { Network(androidContext()) }
}

Or simply (I prefer this one for brevity):

val networkModule = module {
   single { Network(get()) }
}

Getting a Reference to the ApplicationContext in a Spring Application, Spring will automatically detect this interface and inject a reference to ApplicationContextAware { private ApplicationContext ctx; public void  Since you are not extending any of the spring classes your application is always separated from the framework. Most of the cases you will not wanted to inject the ApplicationContext as it, but will need to inject the beans defined in the ApplicationContext.

Application context is available inside a module through the function androidContext().

Spring Boot ApplicationContext, Spring Boot ApplicationContext tutorial shows how to use ApplicationContext in a Spring Boot application. Spring Boot is a popular framework for  I want methodA to use the value that is present in the application.yml file rather than mocking it. Is it possible to inject the MyConfig from the application context into MyUtility mock ? java spring-boot unit-testing mockito junit5

val appModule = module {
    viewModel { LoginViewModel(androidContext()) }
}

This should solve your problem.

Spring - Making Bean ApplicationContext aware, We can inject ApplicationContext as bean using @Autowire annotation or by implementing spring ApplicationContextAware interface. In the  Option 1: Register Using a Lambda ¶. In this option, rather than registering the email server type directly, register using a lambda expression. This allows you to resolve things from the container or use the environment to get the value. var builder = new ContainerBuilder(); builder.Register(ctx => { var address = Environment.GetEnvironmentVariable("SERVER_ADDRESS"); return new EmailServer(address); }).As<IEmailServer> ();

How to Inject Application Context to a bean in Spring?, Injecting Application Context to a bean is easy, your bean just has to implement ApplicationContextAware interface, ApplicationContextAware  @LeoDroidCoder and @GuilhE I implemented your way to provide application context everywhere but it seems like provideContext() is not getting from the generated classes as a result @AppContext lateinit var context: Context is remained uninitialized. Can you help me with a sample or can you help me in how to get the application context whereever

How to get spring application context object reference?, This can be achieved by using ApplicationContextAware interface. Here are the steps to achieve application context object: Create a new class and implement  To access the application context, we can autowire the ApplicationContext interface or implement the ApplicationContextAware. Spring Boot ApplicationContext example In the following application, we access the application context and use its methods.

Spring ApplicationContext, How to access ApplicationContext inside a java bean? To get access to ApplicationContext we should implement ApplicationContextAware interface in the  We can inject spring ApplicationContext as a bean into any bean. By doing so, we are effectively making our bean ApplicationContext aware. Our bean then programmatically can retrieve other beans by calling ApplicationContext.getBean () or retrieve any resources by calling applicationContext#getResource () Using this approach, we can solve the problem of injecting a prototype bean into singleton bean as mentioned in the last topic.

Comments
  • link is broken.