5 minutes estimated read time

Spring

Vital is developed with Spring (Boot) at its core, meaning that you can profit from all of its functionalities, mainly being dependency injection.

Dependency injection is a term used by a lot of plugin developers that is often misunderstood, especially in the Spring context.

It is important to know the basics of Spring dependency management and dependency injection before you start developing with Vital.
Don't worry, it's not complicated, and you will soon know why this pattern is loved by many developers.


The basics

Your plugin will always contain multiple files / classes; maybe you have a class for a single command, then a class for a listener which has all of your event handlers, etc.
Every class by-default doesn't do anything yet, especially commands and listeners.

When you're not using Vital, you manually need to let your plugin (and the server) know what classes are responsible for commands, listeners, etc.
This is what you would normally write in your main class.

MyPlugin.java
public class MyPlugin extends JavaPlugin {
  @Override
  public void onEnable() {
    getCommand("mycommand").setExecutor(new MyCommand());
    Bukkit.getPluginManager().registerEvents(new MyListener());
    // ...
  }
}

You would have to manually register every command and listener when your plugin starts up.

But sometimes you add a command, implement it fully, start your server and then realize, "Wait; why doesn't my command show up?"
You look into your command class and don't find the problem right away, you take a look into your main class and "Ahhh, I forgot to register the command...".
This can be very annoying when working on complex plugins.

Vital automatically detects all of your commands / listeners / other components and registers them when your plugin starts up.


In Spring Boot, you can add the @Component annotation to any of your classes and Spring Boot will then automatically try to create a new instance of it using new ClassThatYouAnnotated() when it starts up.
These automatically created instances of your classes are called "Spring Beans". A bean is an instance of a class that is automatically created and then managed by Spring.

Note that sometimes, you also need to access other classes to perform actions, like maybe your command needs to open an inventory, etc.

Since your inventory might be a class, you somehow have to get that class into your command class.
With Vital, you can simply define the inventory class as a constructor parameter, and Spring Boot will automatically inject that inventory into your command without you having to do anything.

MyCommand.java
// This is the annotation that makes all of this dependency injection magic work; internally, it is annotated with @Component.
@VitalCommand.Info(...)
public class MyCommand extends VitalCommand.Spigot {
  private final MyInventory myInventory;
  
  public MyCommand(MyInventory myInventory) {
    this.myInventory = myInventory;
  }
}

Spring will try to create your command using new MyCommand() but then fails and sees that your constructor needs MyInventory.
It will then check if the class MyInventory is also annotated with @Component, and if so, first creates your inventory using new MyInventory() and then automatically uses that newly created inventory to create your command.

Vital will internally grab all of your commands annotated with @VitalCommand.Info (@Component) and register them for you.
So internally the same getCommand(String).setExecutor(Command) is still happening, but for you, it is hidden.

And that's it...
All you need to know is that all classes annotated with @Component now live in Spring and are called "Spring beans" and can freely be injected into any other bean.

One important note is that once you annotate your class with @Component you should never manually create a new instance using new MyClass again, just inject it to whereever you need it.

Found an issue?

Help us improve the documentation by reporting typos, missing information, or anything confusing.

Don't worry, we will auto-fill most of the annoying fields.

Create GitHub issue

This site and the Vital framework were built with ❤️ by Mike Neumann