Skip to content
LeeGod edited this page Feb 17, 2022 · 8 revisions

Container is the Fairy dependency injection system. It will handle Lookup, Storing and Life Cycle for container object. It's inspired from Spring Framework's Bean system.

ContainerObject

Every stored instance will be recognized ContainerObject in internal system. When an instance has ben stored into Container, it can be lookup by Containers.get(Class).

A ContainerObject should only have 1 single instance.

BaseContainerObject

Types inherited BaseContainerObject:

The base ContainerObject, It's the actual ContainerObject that life cycles are fully controlled by Fairy. Life cycle of container object has 5 states:

State Description Listen
CONSTRUCT The ContainerObject instance has just been constructed from conctructor. Directly in constructor
PRE_INIT The ContainerObject pre process in ContainerContext is done, but before any controller injection (Autowired fields injection, Subscriber injection etc) @PreInitialize method
POST_INIT After PRE_INIT and all controller injection is finished, finalizing initializes. This will be called while guaranteed other ContainerObject has been loaded. @PostInitialize method
PRE_DESTROY This ContainerObject are preparing to be destroyed, most likely when plugin/application is closing or manual disabling from CotainerContext.disableObject(). This will be called while guaranteed other ContainerObject hasn't been unloaded yet. @PreDestroy method
POST_DESTROY After PRE_DESTROY and this ContainerObject has been unloaded. @PostDestroy method

You can check current ContainerObject life cycle by ContainerObject.isLifeCycle(LifeCycle). Everything inherited from can listen to life cycle in BaseContainerObject by annotated method.

import io.fairyproject.container.PostInitialize;
import io.fairyproject.container.PreInitialize;

...
    
    @PreInitialize // refer to PRE_INIT state
    public void onPreInitialize() {
        System.out.println("I have been pre initialized!");
    }

    @PostInitialize // refer to POST_INIT state
    public void onPostInitialize() {
        System.out.println("I have been post initialized!");
    }

Every ways/annotations to listen life cycle change are listed above in the states table.

Important Note for BaseContainerObject

BaseContainerObject are fully controlled under Fairy, you shouldn't need to construct it yourself.

Constructor

For BaseContainerObject, you can directly put ContainerObject type into parameter to get instance of ContainerObject directly in constructor.

Example:

import io.fairyproject.container.Service;
import io.fairyproject.module.ModuleService;

@Service
public class ExampleService {
    
    /*
        we have ModuleService in constructor
        when ContainerObject being constructed
        it will build parameters before constructing depend on parameters.
    */
    public ExampleService(ModuleService moduleService) {
        System.out.println("We get an instance of ExampleService! " + moduleService);
    }
...

If you have multiple constructors, you can use @io.fairyproject.container.ContainerConstruct to specify the constructor for fairy to construct the instance.

Example:

import io.fairyproject.container.ContainerConstruct;
import io.fairyproject.container.Service;
import io.fairyproject.module.ModuleService;

@Service
public class ExampleService {

    // Other constructor
    public ExampleService() {
        
    }
    
    // The constructor you want fairy to construct
    @ContainerConstruct
    public ExampleService(ModuleService moduleService) {
        System.out.println("We get an instance of ExampleService! " + moduleService);
    }
...

Important Note for Container Constructor

When using container constructor, you should give attention to avoid circular dependency.

SimpleContainerObject

The simplified ContainerObject, it usually this ContainerObject are not fully controlled by Fairy Container Context. it's not constructed by Fairy, the content of instance is not predictable by Fairy. Plugins and @Register will register the instance as SimpleContainerObject.

Life Cycle methods will not be taking care for SimpleContainerObject.

Clone this wiki locally