Skip to content
Cameron Carvalho edited this page Nov 11, 2024 · 2 revisions

Supervisor Loader Wiki

Welcome to the Supervisor Loader Wiki! This guide explains how the Supervisor Loader works, helping you understand how components are managed and instantiated within your plugin.

Supervisor Loader Overview

The Supervisor Loader is responsible for managing the lifecycle of components in your plugin. It caches all components and automatically creates their constructors, simplifying the dependency injection process for your project. Any parameters within a component's constructor must themselves be components that Supervisor Loader can recognize and manage.

Registering Components with Supervisor Loader

When initializing your plugin, you need to register it with Supervisor Loader. The loader will then handle all components and dependencies automatically. Below is an example of how to register a plugin and include custom components.

Example with Custom Component Registration

package com.vertmix.supervisor.core.bukkit.dummy;

import com.vertmix.supervisor.loader.SupervisorLoader;
import org.bukkit.plugin.java.JavaPlugin;

import static com.vertmix.supervisor.core.bukkit.BukkitProvider.bukkit;

public class DummyPlugin extends JavaPlugin {

    @Override
    public void onEnable() {
        SupervisorLoader.register(bukkit(this), new String("example"));
    }
}

In this example, DummyPlugin registers itself with the SupervisorLoader. Additionally, we register a String instance ("example") as a custom component. This means that you can add and manage any custom components that aren't annotated with @Component but still want them to be available for injection.

Example Without Custom Component Registration

If you do not need to register any additional components, you can simply register your plugin without specifying custom components:

package com.vertmix.supervisor.core.bukkit.dummy;

import com.vertmix.supervisor.loader.SupervisorLoader;
import org.bukkit.plugin.java.JavaPlugin;

import static com.vertmix.supervisor.core.bukkit.BukkitProvider.bukkit;

public class DummyPlugin extends JavaPlugin {

    @Override
    public void onEnable() {
        SupervisorLoader.register(bukkit(this));
    }
}

In this example, only components annotated with @Component are registered and managed by Supervisor Loader. Any non-annotated components would need to be manually registered as shown in the previous example.

How Supervisor Loader Works

Caching Components

Supervisor Loader caches all components upon registration. This means that once a component is loaded, it is kept in memory, making it readily available whenever required. This caching mechanism ensures efficient access to all registered components and allows the framework to maintain a single instance of each component, effectively implementing the Singleton pattern.

Constructor Creation and Dependency Injection

Supervisor Loader automatically creates constructors for all registered components. This involves the following:

  • When a component's constructor is called, Supervisor Loader looks for all the required parameters.
  • If a parameter is a component (either annotated with @Component or registered manually), Supervisor Loader injects it automatically.
  • All dependencies must be registered or annotated as components for this to work properly.

This makes the process of dependency injection seamless, allowing you to focus on your business logic rather than worrying about managing object creation and dependencies.

Example: Dependency Injection

Consider the following component with a dependency:

package com.vertmix.supervisor.core.bukkit.dummy.service;

import com.vertmix.supervisor.core.annotation.Component;
import com.vertmix.supervisor.core.bukkit.dummy.repository.PlayerDataRepository;

@Component
public class PlayerDataService {

    private final PlayerDataRepository playerDataRepository;

    public PlayerDataService(PlayerDataRepository playerDataRepository) {
        this.playerDataRepository = playerDataRepository;
    }

    // Business logic methods
}

In this example, PlayerDataService depends on PlayerDataRepository. When PlayerDataService is registered, Supervisor Loader will automatically inject an instance of PlayerDataRepository into the constructor. This means that you don't have to manually create or pass PlayerDataRepository—Supervisor Loader handles it for you.

Summary

The Supervisor Loader provides an efficient way to manage components in your plugin. By caching all components and managing constructor creation and dependency injection, it reduces boilerplate code and makes your plugin more maintainable. You can easily register additional custom components and let Supervisor Loader handle the instantiation and lifecycle management of all the components, ensuring that dependencies are resolved automatically.

If you need further assistance or have any questions, feel free to contact us or contribute to the project!

Contributing

If you're interested in contributing to the Supervisor project, feel free to submit pull requests or raise issues on GitHub. We welcome suggestions and improvements from the community.

License

The Supervisor project is licensed under the MIT License. See the LICENSE file for more information.

Contact

For further questions or suggestions, contact us at support@vertmix.com.

Thank you for being part of the Supervisor community!

Clone this wiki locally