Class DependencyInjection

java.lang.Object
dev.demeng.pluginbase.di.DependencyInjection

public final class DependencyInjection extends Object
Utility class for creating and working with dependency injection containers.

This class provides convenient static methods for creating DependencyContainer instances.

Example usage:


 // Create services
 DatabaseConnection db = new MySQLConnection(plugin);
 CacheService cache = new RedisCacheService();

 // Create a container with interface-based registration
 DependencyContainer container = DependencyInjection.builder()
     .register(plugin)  // Register by concrete type
     .register(DatabaseConnection.class, db)  // Register by interface
     .register(CacheService.class, cache)     // Register by interface
     .build();

 // Classes annotated with @Component will be auto-created:
 @Component
 public class PlayerDataManager {
   private final DatabaseConnection db;
   private final CacheService cache;

   public PlayerDataManager(DatabaseConnection db, CacheService cache) {
     this.db = db;
     this.cache = cache;
   }
 }

 // Retrieve auto-created components
 PlayerDataManager manager = container.getInstance(PlayerDataManager.class);

 // Or create an empty container
 DependencyContainer container = DependencyInjection.create();
 
  • Constructor Details

    • DependencyInjection

      public DependencyInjection()
  • Method Details

    • builder

      @NotNull public static DependencyContainer.Builder builder()
      Creates a new dependency container builder.

      Use the builder to configure bindings, register types, and register singleton instances before building the container.

      Returns:
      A new DependencyContainer.Builder instance
    • create

      @NotNull public static @NotNull DependencyContainer create()
      Creates a new empty dependency container.

      The container will have no pre-configured bindings. You can register dependencies dynamically using the container's registration methods.

      Returns:
      A new DependencyContainer instance