Package dev.demeng.pluginbase.di
Class DependencyInjection
java.lang.Object
dev.demeng.pluginbase.di.DependencyInjection
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic DependencyContainer.Builderbuilder()Creates a new dependency container builder.static @NotNull DependencyContainercreate()Creates a new empty dependency container.
-
Constructor Details
-
DependencyInjection
public DependencyInjection()
-
-
Method Details
-
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.Builderinstance
-
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
DependencyContainerinstance
-