Interface DependencyContainer.Builder

Enclosing interface:
DependencyContainer

public static interface DependencyContainer.Builder
Builder for creating and configuring a DependencyContainer.

Example usage:


 DatabaseConnection db = new DatabaseConnection(plugin);
 CacheService cache = new CacheService();

 DependencyContainer container = DependencyContainer.builder()
     .register(plugin)  // Register plugin instance
     .register(db)      // Register database
     .register(cache)   // Register cache
     .build();

 // Classes with @Component are auto-created
 PlayerDataManager manager = container.getInstance(PlayerDataManager.class);
 
  • Method Details

    • register

      @NotNull @NotNull DependencyContainer.Builder register(@NotNull @NotNull Object instance)
      Registers an instance in the container.

      The instance will be stored and can be retrieved later via DependencyContainer.getInstance(Class). It will also be injected into @Component classes that depend on it.

      Parameters:
      instance - The instance to register
      Returns:
      This builder for method chaining
    • register

      @NotNull <T> @NotNull DependencyContainer.Builder register(@NotNull @NotNull Class<T> type, @NotNull T instance)
      Registers an instance in the container with an explicit type.

      This allows registering instances by their interface or abstract class rather than just their concrete implementation type.

      Example:

      
       DatabaseConnection db = new MySQLConnection(plugin);
       CacheService cache = new RedisCacheService();
      
       DependencyContainer container = DependencyContainer.builder()
           .register(plugin)  // Concrete type
           .register(DatabaseConnection.class, db)  // Interface
           .register(CacheService.class, cache)     // Interface
           .build();
       
      Type Parameters:
      T - The type parameter
      Parameters:
      type - The type to register the instance under
      instance - The instance to register
      Returns:
      This builder for method chaining
      Throws:
      TypeMismatchException - if instance is not
    • build

      @NotNull @NotNull DependencyContainer build()
      Builds and returns the configured dependency container.
      Returns:
      A new DependencyContainer instance