Skip to content

Dependency injection

Dependency injection provided by this framework is a more lightweight alternative to dedicated frameworks, quite similarly to Spring (which is supported) or CDI using Weld.

Rather than you having to construct objects, you may only request them, the framework will then construct it by providing the dependencies required for your service, wherever they may come from.

This avoids having to pass objects everywhere, allowing a more effective decoupling, and allows switching implementations in a completely transparent manner.

Example

ConnectionSupplier is an interfaced service (an interface that, when implemented, enables the service to be retrieved as such interface).

You can create an implementation of this interface, per database, enabling you to switch your database, for example, using a configuration file, without changing anything else in your code.

2.X Migration

All singletons / classes with static methods were moved as services, including:

  • Components
  • EventWaiter
  • Localization

If you were using ExtensionsBuilder#registerConstructorParameter(Class<T>, ConstructorParameterSupplier<T>) to get objects in commands, it is basically the same, except in a much more complete framework, and without having to declare everything with this method.

TagDatabase.java
public class TagDatabase { /* */ }
TagCommand.java
public class TagCommand {
    private final TagDatabase tagDatabase;

    public TagCommand(TagDatabase tagDatabase) {
        this.tagDatabase = tagDatabase;
    }
}
Builder
final var tagDatabase = new TagDatabase(/* */);

CommandsBuilder.newBuilder()
    .registerConstructorParameter(TagDatabase.class, clazz -> tagDatabase)
    // Further configuration
    .build();
TagDatabase.kt
class TagDatabase { /* */ }
TagCommand.kt
class TagCommand(private val tagDatabase: TagDatabase) {
    /* */
}
Builder
val tagDatabase = TagDatabase(/* */);

CommandsBuilder.newBuilder()
    .registerConstructorParameter(TagDatabase::class.java) { tagDatabase }
    // Further configuration
    .build();
TagDatabase.java
@BService //Makes this class injectable, can also pull other services in its constructor
public class TagDatabase { /* */ }
TagCommand.java
@Command
public class TagCommand {
    private final TagDatabase tagDatabase;

    public TagCommand(TagDatabase tagDatabase) {
        this.tagDatabase = tagDatabase;
    }

    /* */
}

No specific builder code required!

TagDatabase.kt
@BService //Makes this class injectable, can also pull other services in its constructor
class TagDatabase { /* */ }
TagCommand.kt
@Command
class TagCommand(private val tagDatabase: TagDatabase) {
    /* */
}

No specific builder code required!