cui-java-cdi

Define CDI and Quarkus development standards for CUI projects.

1|Updated Feb 3, 2025
One-click install
npx skills add https://github.com/cuioss/cui-llm-rules --skill cui-java-cdi
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cui-java-cdi
Source: https://github.com/cuioss/cui-llm-rules/tree/main/claude/marketplace/skills/cui-java-cdi
Command: npx skills add https://github.com/cuioss/cui-llm-rules --skill cui-java-cdi

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Developing Java applications with CDI (Contexts and Dependency Injection) and Quarkus can be complex, especially when aiming for native compilation, secure containerization, and robust testing. This Skill provides a comprehensive set of standards to ensure consistent, high-quality, and performant Java development, automating best practices and reducing common pitfalls.

Core Features & Use Cases

  • CDI Best Practices: Enforces constructor injection, proper scoping, and producer method patterns for clean, testable code.
  • Secure Containerization: Guides Dockerfile creation with security hardening, non-root execution, and optimized health checks.
  • Quarkus Native Optimization: Provides patterns for @RegisterForReflection and deployment processors to achieve efficient native images.
  • Use Case: When building a new Quarkus microservice, use this skill to ensure your CDI beans are correctly injected, your Docker image is secure and optimized, and your native compilation is configured for peak performance, letting AI handle the complex configuration details.

Quick Start

Implement Dependency Injection

  1. Use constructor injection (never field injection)
  2. Make injected fields final
  3. Select appropriate scope (@ApplicationScoped for stateless)
  4. Avoid @Singleton unless eager initialization required
  5. Use Instance<T> for optional dependencies
  6. Add qualifiers for disambiguation

Create Secure Docker Image

  1. Use minimal base image (UBI, distroless)
  2. Run as non-root user
  3. Configure health checks
  4. Set resource limits
  5. Use multi-stage builds
  6. Scan for vulnerabilities

Frequently Asked Questions about cui-java-cdi

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement dependency injection in Java with CDI and Quarkus?

Dependency injection in CDI uses constructor injection with `@Inject` annotations on bean classes. Define beans with appropriate scopes (`@ApplicationScoped` for stateless services), make injected fields `final`, use qualifiers to disambiguate multiple implementations, and leverage `Instance<T>` for optional dependencies. Quarkus optimizes CDI at build time for fast startup and efficient native images.

What are CDI scopes and when should I use each one?

`@ApplicationScoped` creates singletons for stateless services, `@RequestScoped` isolates per-request state, `@SessionScoped` persists user sessions, and `@Dependent` creates new instances per injection point. Use `@ApplicationScoped` by default for microservices; avoid `@Singleton` unless you need eager initialization. Proper scoping ensures thread safety and resource efficiency in Quarkus applications.

How do I create a secure Docker image for a Quarkus microservice?

Build secure Quarkus Docker images using minimal base images (UBI or distroless), multi-stage builds to reduce size, run containers as non-root users, configure health checks, set resource limits, and scan for vulnerabilities. Quarkus native images are particularly lean and start instantly, making containerization straightforward while maintaining security hardening.

Can I compile Quarkus applications to native binaries?

Yes, Quarkus compiles Java applications to native binaries using GraalVM, eliminating the JVM startup overhead and reducing memory footprint dramatically. Use `@RegisterForReflection` annotations for classes requiring runtime reflection, configure deployment processors for native optimization, and validate reflection usage guidelines to ensure compatibility with ahead-of-time compilation.

What testing practices work best with CDI and Quarkus?

CDI and Quarkus support both unit tests and integration tests with dependency injection. Use constructor injection in test classes, configure test containers with CDI scopes, mock producers for external dependencies, and run integration tests against the full Quarkus container. Test configurations should match production standards for security, resource limits, and health checks.

Why should I use constructor injection instead of field injection in CDI?

Constructor injection makes dependencies explicit, immutable, and testable without reflection or additional frameworks. Field injection hides dependencies and complicates testing; constructor injection with `final` fields prevents accidental mutation, clarifies object requirements, and integrates naturally with Quarkus's build-time optimization and native compilation.