Skip to content

java_start

klee0kai edited this page Dec 17, 2023 · 1 revision

Start

Stone is a universal DI library designed specifically for jvm. Eliminates the need to use scopes and sub-components. DI caches provided objects using: weak/soft/strong references. This allows you to configure scopes more flexibly, or ignore them altogether.

Import

The library is supplied via the jitpack repository.

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

The library itself can be imported in the general version:

dependencies {
    //stone
    implementation 'com.github.klee0kai.stone:stone_lib:1.0.3'
    annotationProcessor 'com.github.klee0kai.stone:stone_processor:1.0.3'
}

If you want to use the Android version of the library (with support for the life cycles of Android components), then you can add import of the Android library

dependencies {
    //stone
    implementation 'com.github.klee0kai.stone:android_lib:1.0.3'
}

To support working with kotlin delegates, and with the advanced functionality of providing wrappers, you can also use the library:

dependencies {
    //stone
    implementation 'com.github.klee0kai.stone:kotlin_lib:1.0.3'
}

Easy to use

As with any library, DI begins with a description of the objects it provides. All providing objects in Stone are described in modules. Each module is declared with the @Module annotation. The class contains methods for generating provided objects.

@Module
public class SevenPlanetModule {

    public Earth earth(){
        return new Earth();
    }

}

Next, add the module to the component

@Component
public interface SevenPlanetComponent {

    SevenPlanetModule planets();

}

And we use:

class App {

    static SevenPlanetComponent DI = Stone.createComponent(SevenPlanetComponent.class);
    
    public static void main(String[] args) {
        Earth earth = DI.planets().earth();
    }
    
}

The Stone library will independently generate code for you that caches your provided objects. Thus, you can use the same earth object a second time by simply calling DI.planets().earth(). In general, the Stone library provides objects as a factory, without caching. If you want to explicitly specify the caching method, use the @Provide annotation specifying the caching method.

@Module
public class SevenPlanetModule {

    @Provide(cache = Provide.CacheType.Weak)
    public Earth earth(){
        return new Earth();
    }

}

So the following methods of caching an object are available:

  • Factory - do not cache at all. Create a new object each time you call
  • Weak - cache weak links. As soon as the object is released by the last holder, the object will be destroyed when the GC is called. When calling DI.planets().earth() again, a new one will be created
  • Soft - cache soft link. An object will be destroyed only if there is an immediate lack of memory and only if it is not in use by anyone.
  • Strong - caching a strong link. The object will not be destroyed; its life cycle is equal to the life cycle of the DI component. Behavior similar to Singleton

Note that the Stone.createComponent(SevenPlanetComponent.class) method is executed through runtime reflection. If you use code obfuscation, you must directly use the scanned component - adding StoneComponent to the class name. In our case, using the library will look like this:

class App {

    static SevenPlanetComponent DI = new SevenPlanetComponentStoneComponent();

    public static void main(String[] args) {
        Earth earth = DI.planets().earth();
    }

}

Clone this wiki locally