You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Evaluate Java's CDI standard on lazy injectsion #260
CDI/JSR-330 Integration Analysis for Shared Neural Network Module
Introduction
The MNIST-CNN Integration Module design outlines a Kotlin Multiplatform component (skainet-int-mnist-cnn) that provides a shared, singleton-like deep neural network (the MNIST CNN) to consuming applications. It encapsulates model loading (from a GGUF file resource) and inference, exposing a MnistCnnShared API with coroutine/Flow-based state (Loading, Ready, Running, etc.).
The question is whether this approach aligns with standard dependency injection practices, specifically JSR-330 (Javax Inject) and CDI (Contexts and Dependency Injection). In other words:
Can the idea of a shared, reusable neural network instance be implemented in compliance with JSR-330/CDI standards?
What adjustments or benefits would that bring?
Understanding CDI and JSR-330
JSR-330 (Dependency Injection for Java)
JSR-330 is a specification that defines a minimal set of annotations and interfaces:
Swap loaders or execution contexts via DI configuration
No changes required to consumers
4. Lifecycle Alignment
Use @PostConstruct / @PreDestroy
Integrates cleanly with application lifecycle
5. Improved Testability
Inject mock loaders or lightweight models
Configure different execution contexts for tests
No code changes required
Potential Adjustments for CDI Compliance
Annotate MnistCnnShared with:
@ApplicationScoped (CDI)
or @Singleton (JSR-330)
Inject dependencies using @Inject
Optionally use qualifiers if multiple loaders exist
Optionally add lifecycle callbacks
Keep core module DI-agnostic if full multiplatform purity is desired
Applications can wrap or provide the instance via DI modules
Conclusion
The concept of a shared MNIST CNN model service fits naturally into CDI and JSR-330 paradigms.
A shared neural network is a textbook example of a singleton service
The design already follows DI principles:
Abstraction
Encapsulation
Separation of concerns
CDI/JSR-330 can formalize and standardize what the module already does
Using DI:
Simplifies integration
Improves maintainability
Aligns lifecycle management with application standards
Ultimately, adopting CDI/JSR-330 does not change the design’s goals—it enhances them by embedding the module in a widely understood and proven architectural pattern.
Sources
JSR-330 Specification Highlights – Scope and Singleton usage
mkyong.com
Open Liberty Documentation – ApplicationScoped beans as singletons in CDI
openliberty.io
Medium (R. Ali) – JSR-330 Provider for lazy or controlled initialization
medium.com
Quellen
Sources:
JSR-330 Specification Highlights – Scope and Singleton usage mkyong.com
Open Liberty Documentation – ApplicationScoped beans as singletons in CDI openliberty.io
Medium (R. Ali) – JSR-330 Provider for lazy or controlled initialization medium.com
Quellenangaben
Spring 3 and JSR-330 @Inject and @nAmed example - Mkyong.com
Activity
michalharakal commentedon Dec 16, 2025
CDI/JSR-330 Integration Analysis for Shared Neural Network Module
Introduction
The MNIST-CNN Integration Module design outlines a Kotlin Multiplatform component (
skainet-int-mnist-cnn) that provides a shared, singleton-like deep neural network (the MNIST CNN) to consuming applications. It encapsulates model loading (from a GGUF file resource) and inference, exposing aMnistCnnSharedAPI with coroutine/Flow-based state (Loading,Ready,Running, etc.).The question is whether this approach aligns with standard dependency injection practices, specifically JSR-330 (Javax Inject) and CDI (Contexts and Dependency Injection). In other words:
Understanding CDI and JSR-330
JSR-330 (Dependency Injection for Java)
JSR-330 is a specification that defines a minimal set of annotations and interfaces:
@Inject,@Singleton,@Named,@QualifierProvider<T>JSR-330 does not provide an implementation by itself. Frameworks such as CDI containers, Guice, Dagger, and Spring implement these annotations.
Key points:
Dependencies annotated with
@Injectare supplied by the DI framework.By default, unscoped dependencies are often created as new instances per injection site.
To share a single instance across the application, the object is marked with a singleton scope, such as:
@Singleton(JSR-330)@ApplicationScoped(CDI)Sources:
CDI (Contexts and Dependency Injection)
CDI is the Java/Jakarta EE standard for dependency injection (Jakarta CDI, formerly JSR-299/346). CDI:
Builds on JSR-330
Adds well-defined scopes and contexts:
@ApplicationScoped@RequestScopedSupports lifecycle callbacks, producer methods, events, and interceptors
An
@ApplicationScopedbean:CDI encourages:
Shared Neural Network Module Overview
The proposed MNIST CNN integration module is designed to make a pre-trained model easy to reuse across applications.
Key Design Points
Single shared instance (
MnistCnnShared) manages the model lifecycle and avoids loading the model multiple times.Model loading abstraction via
ModelParameterLoader(fromio-core), with a default GGUF implementation (io-gguf).Coroutine-friendly API:
StateFlowto emit lifecycle states (Unloaded → Loading → Ready)run()Manual lifecycle management:
load()andunload()methodsKotlin Multiplatform support:
expect/actualLow coupling for consumers:
This design already follows core dependency injection principles by centralizing construction and configuration.
Alignment with Dependency Injection Principles
Singleton Mapping
The idea of a single shared neural network instance maps directly to a singleton-scoped bean:
@ApplicationScoped@SingletonThis ensures:
From a DI perspective,
MnistCnnSharedis a classic heavy service singleton.Decoupling and Dependency Injection
The module already abstracts model loading behind the
ModelParameterLoaderinterface.In a DI setup:
ModelParameterLoadercan be injectedDifferent implementations (GGUF, ONNX, remote loader) can be swapped without changing consumers
Selection can be handled via:
@Namedor custom qualifiersThis aligns perfectly with JSR-330’s principle of coding to interfaces.
Providing the Shared Model via DI
There are two standard DI patterns applicable here.
1. Direct Injection
Annotate
MnistCnnShared:Consumers then inject it:
The DI container:
2. Producer / Factory Bean (CDI)
In CDI, a producer method can create the instance:
Notes:
nullor partially initialized objects is discouraged (Stack Overflow guidance)However, since the module already encapsulates creation and loading, direct injection is usually cleaner.
Lifecycle and Initialization Strategies
Eager Initialization
Some DI containers eagerly create singletons:
@Singletonbeans eagerly by defaultPossible approach:
Lazy Initialization via Provider
JSR-330 provides
Provider<T>:get()is calledThis allows deferring model loading until first use.
Manual Trigger with Injection
A hybrid approach:
MnistCnnSharedload()when appropriate (e.g., when a UI screen appears)Benefits:
StateFlowState Management and Thread Safety
DI does not manage internal state or concurrency.
Key points:
The current design:
load,run, andunloadStateFlowThis is fully compatible with CDI/JSR-330.
Multiplatform Integration
JVM / Android
Works naturally with:
Can be injected into:
Example (Hilt):
Non-JVM Targets (iOS, JS)
DI is an optional integration, not a requirement.
CDI Lifecycle Considerations
Startup and Shutdown Hooks
@PostConstruct: optional eager or async initialization@PreDestroy: ideal place to callunload()Example:
This ensures proper cleanup at application shutdown.
Concurrency Considerations
Concurrency strategy remains an internal design decision.
Benefits of Using Standard DI
1. Standardized Singleton Management
@Singleton,@ApplicationScoped)2. Simpler Consumer Integration
3. Configuration Flexibility
4. Lifecycle Alignment
@PostConstruct/@PreDestroy5. Improved Testability
Potential Adjustments for CDI Compliance
Annotate
MnistCnnSharedwith:@ApplicationScoped(CDI)@Singleton(JSR-330)Inject dependencies using
@InjectOptionally use qualifiers if multiple loaders exist
Optionally add lifecycle callbacks
Keep core module DI-agnostic if full multiplatform purity is desired
Conclusion
The concept of a shared MNIST CNN model service fits naturally into CDI and JSR-330 paradigms.
A shared neural network is a textbook example of a singleton service
The design already follows DI principles:
CDI/JSR-330 can formalize and standardize what the module already does
Using DI:
Ultimately, adopting CDI/JSR-330 does not change the design’s goals—it enhances them by embedding the module in a widely understood and proven architectural pattern.
Sources
JSR-330 Specification Highlights – Scope and Singleton usage
mkyong.com
Open Liberty Documentation – ApplicationScoped beans as singletons in CDI
openliberty.io
Medium (R. Ali) – JSR-330 Provider for lazy or controlled initialization
medium.com
Quellen
Sources:
JSR-330 Specification Highlights – Scope and Singleton usage
mkyong.com
Open Liberty Documentation – ApplicationScoped beans as singletons in CDI
openliberty.io
Medium (R. Ali) – JSR-330 Provider for lazy or controlled initialization
medium.com
Quellenangaben
Spring 3 and JSR-330 @Inject and @nAmed example - Mkyong.com
https://mkyong.com/spring3/spring-3-and-jsr-330-inject-and-named-example/
Spring 3 and JSR-330 @Inject and @nAmed example - Mkyong.com
https://mkyong.com/spring3/spring-3-and-jsr-330-inject-and-named-example/
RESTful Web Services integration with CDI :: Open Liberty Docs
https://openliberty.io/docs/latest/jaxrs-integration-cdi.html
java - CDI Produce Instance Created At Runtime - Stack Overflow
https://stackoverflow.com/questions/43222650/cdi-produce-instance-created-at-runtime
Introduction to Contexts and Dependency Injection (CDI) - Quarkus
https://quarkus.io/guides/cdi
Choosing Between dagger.Lazy and javax.inject.Provider: A Developer’s Guide | by Rahil Ali | Medium
https://medium.com/@rahilali1991/choosing-between-dagger-lazy-and-javax-inject-provider-a-developers-guide-af448a237381
Choosing Between dagger.Lazy and javax.inject.Provider: A Developer’s Guide | by Rahil Ali | Medium
https://medium.com/@rahilali1991/choosing-between-dagger-lazy-and-javax-inject-provider-a-developers-guide-af448a237381
Alle Quellen