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 Summary
Modifier and TypeMethodDescription@NotNull DependencyContainerbuild()Builds and returns the configured dependency container.<T> @NotNull DependencyContainer.BuilderRegisters an instance in the container with an explicit type.@NotNull DependencyContainer.BuilderRegisters an instance in the container.
-
Method Details
-
register
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@Componentclasses 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 underinstance- The instance to register- Returns:
- This builder for method chaining
- Throws:
TypeMismatchException- if instance is not
-
build
Builds and returns the configured dependency container.- Returns:
- A new
DependencyContainerinstance
-