Component
The Component
class in PySpring serves as the foundational building block for creating modular, reusable functionalities. It supports lifecycle management, dependency injection, and property configuration, making it an essential entity in the PySpring framework.
Overview
The Component
class encapsulates specific functionalities and enables seamless integration with the PySpring application context. It is highly configurable, allowing developers to define scope, inject dependencies, and manage lifecycle hooks.
Features
-
Reusability and Modularity
Components are designed to be reusable and modular, encapsulating specific functionalities that can be leveraged across the application. -
Scope Management
The scope determines whether the component instance is shared (Singleton
) or created a new each time (Prototype
), which can be configured using theComponentScope
enum.Singleton
: Single shared instance throughout the application.Prototype
: A new instance is created per request.
-
Lifecycle Hooks
Components provide hooks for initialization and destruction:post_construct()
: Invoked after the component is initialized.pre_destroy()
: Invoked before the component is destroyed.
-
Dependency Injection
Components can be declare as dependencies for being injected to other components by the PySpring framework. -
Properties Injection
Properties from configuration files can be injected into components to simplify configuration management.
Configuration
Nested Config
Class
The Component
class includes a nested Config
class to define configuration options, such as:
- Allowed scope (
Singleton
orPrototype
).
Methods
Lifecycle Methods
post_construct()
:
Called after initialization to set up the component. Subclasses can override this method.pre_destroy()
:
Called before destruction for cleanup. Subclasses can override this method.finish_initialization_cycle()
:
Invokespost_construct()
and finalizes initialization.finish_destruction_cycle()
:
Invokespre_destroy()
and finalizes destruction.
Scope Management
get_scope()
:
Returns the current scope of the component (Singleton
orPrototype
).set_scope(scope: ComponentScope)
:
Sets the scope of the component.