Working with Contexts

Separation logic within your module

A context is a logical subset of bean definitions inside a module:

val myModule = applicationContext {
        bean { MyComponentA() }
        bean { MyComponentB(get()) }

Handling components lifecycle

The primary target of a context is the ability to drop its instances, in order to suits your components life cycle. This is useful when you can’t use a factory bean definition, and must share instances for a given timelife. Thus you can provide singletons and drop them when you don’t need them anymore.

The releaseContext() function release all instances from a context. Below an Android example:

i.e: in an Android Activity:

abstract class MyCustomActivity : AppCompatActivity() {

    abstract val contextName: String

    override fun onPause() {

Context isolation

Context isolation is now enabled by default. It will allows the given visibility rules. Rootcontext is the context from the root, directly at applicationContext level.

val myModule = applicationContext {
  • beans from B can see beans from A and Root
  • beans from A can see beans from Root
  • beans from C can see beans from Root

other visibilities are blocked

Context CallBacks

You can use the registerContextCallBack() function to register a callback. It allows you to be notified when a context has been released.