Skip to content
LeeGod edited this page Dec 28, 2021 · 2 revisions

Autowired

Autowired is a powerful tool for you to lookup ContainerObject in a very easy way by doing field injection.

Annotation: io.fairyproject.container.Autowired

ContainerObject Field Injection

Any kind of ContainerObject will scan through all the fields and search if the field has @Autowired annotation. it will search ContainerObject by field type, and inject into the field.

Example code:

package io.fairyproject.examplePlugin;

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

@Service
public class ExampleService {

    @Autowired
    private ModuleService moduleService;

...

Static Field Injection

All static field can be injected for Autowired, if the field has @Autowired annotation. it will search ContainerObject by field type, and inject into the field.

Example code:

package io.fairyproject.examplePlugin;

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

public class ExampleClass {

    @Autowired
    public static ModuleService MODULE_SERVICE;

...

Important Note for Autowired

@Autowired field can be any access level (public, protected, private). But you shouldn't set it to final or intialize the field.

Autowired injection will be done after Component PRE_INIT.

Clone this wiki locally