用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/aibot88/sec_skill_store --skill java-cdi命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
Guides the creation of agile user stories and Gherkin feature files. Use when the user wants to create a user story, write acceptance criteria, define Gherkin scenarios, or author BDD feature files. This should trigger for requests such as Create a user story; Write a user story; I need to write a user story. Part of cursor-rules-java project
Guía técnica completa para integrar 250+ servicios externos con agentes IA usando Composio. Cubre instalación, autenticación OAuth, gestión de herramientas, triggers y flujos multi-servicio.
Facilitates conversational discovery to create Architectural Decision Records (ADRs) for non-functional requirements using the ISO/IEC 25010:2023 quality model. Use when the user wants to document quality attributes, NFR decisions, security/performance/scalability architecture, or design systems with measurable quality criteria. This should trigger for requests such as Create ADR for Non-functional requirements; Document Non-functional requirements; Capture Non-functional requirements; Generate Non-functional requirements in an ADR. Part of cursor-rules-java project
基于 SOC 职业分类
正在显示 SKILL.md
| name | java-cdi |
| description | Core CDI patterns including constructor injection, scopes, producers, and container configuration |
| user-invocable | false |
| allowed-tools | Read, Edit, Write, Bash, Grep, Glob |
Core CDI (Contexts and Dependency Injection) standards applicable to any CDI container. This skill covers dependency injection patterns, scopes, and producer methods.
This skill applies to Jakarta CDI projects:
jakarta.inject:jakarta.inject-apijakarta.enterprise:jakarta.enterprise.cdi-api// CDI Core
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
// CDI Scopes
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.RequestScoped;
import jakarta.enterprise.context.SessionScoped;
import jakarta.enterprise.context.Dependent;
// CDI Producers and Optional Dependencies
import jakarta.enterprise.inject.Produces;
import jakarta.enterprise.inject.Instance;
// Quarkus Configuration
import org.eclipse.microprofile.config.inject.ConfigProperty;
REQUIRED: Always use constructor injection instead of field injection.
For foundational constructor injection principles (immutability, testability, fail-fast behavior), see pm-dev-java:java-core skill.
When a CDI bean has exactly one constructor, CDI automatically treats it as the injection point - no @Inject needed:
@ApplicationScoped
public class OrderService {
private final PaymentService paymentService;
private final InventoryService inventoryService;
// No @Inject needed - only one constructor
public OrderService(PaymentService paymentService,
InventoryService inventoryService) {
this.paymentService = paymentService;
this.inventoryService = inventoryService;
}
}
When a CDI bean has multiple constructors, you MUST explicitly mark the injection constructor with @Inject:
@ApplicationScoped
public class ConfigurableService {
private final DatabaseService databaseService;
private final String configValue;
public ConfigurableService() {
this.databaseService = null;
this.configValue = "default";
}
@Inject // REQUIRED - multiple constructors exist
public ConfigurableService(DatabaseService databaseService,
@ConfigProperty(name = "app.config") String configValue) {
this.databaseService = databaseService;
this.configValue = configValue;
}
}
// ❌ Field Injection - FORBIDDEN
@Inject
private UserService userService;
// ❌ Setter Injection - FORBIDDEN
@Inject
public void setUserService(UserService userService) {
this.userService = userService;
}
| Scope | Lifecycle | Use Case |
|---|---|---|
@ApplicationScoped | Single instance per application | Stateless services, most business logic |
@RequestScoped | New instance per HTTP request | Request-specific data |
@SessionScoped | New instance per HTTP session | User session data |
@Dependent | New instance per injection | Helpers, utilities |
@Singleton | Single instance (eager init) | Use sparingly, prefer @ApplicationScoped |
@ApplicationScoped
public class UserService { } // Singleton across application
@RequestScoped
public class RequestContext { } // New instance per HTTP request
Use Instance<T> when a dependency might not be available:
@ApplicationScoped
public class NotificationService {
private final EmailService emailService;
private final SmsService smsService; // May be null
public NotificationService(EmailService emailService,
Instance<SmsService> smsServiceInstance) {
this.emailService = emailService;
this.smsService = smsServiceInstance.isResolvable() ?
smsServiceInstance.get() : null;
}
public void sendNotification(String message) {
emailService.send(message); // Always available
if (smsService != null) {
smsService.send(message); // Optional
}
}
}
CRITICAL: CDI has strict rules about producer methods returning null.
| Scope | Can Return Null? | Reason |
|---|---|---|
@Dependent | ✅ Yes | No proxy needed |
@RequestScoped | ❌ No | Proxy requires target object |
@SessionScoped | ❌ No | Proxy requires target object |
@ApplicationScoped | ❌ No | Proxy requires target object |
@ApplicationScoped
public class ServletObjectsProducer {
@Produces
@Dependent // ✅ REQUIRED for null returns
public HttpServletRequest produceHttpServletRequest() {
return getHttpServletRequest().orElse(null); // Safe with @Dependent
}
}
// ❌ ILLEGAL - will throw IllegalProductException
@Produces
@RequestScoped
public SomeService createService() {
return null; // CDI will throw exception at runtime
}
// ✅ CORRECT - Use Null Object pattern
@Produces
@RequestScoped
public NotificationService createNotificationService() {
return notificationEnabled ?
new EmailNotificationService() :
new NoOpNotificationService(); // Never null
}
AVOID: Returning Optional<T> from producer methods - goes against CDI design philosophy.
| Problem | Exception | Solution |
|---|---|---|
| Missing dependency | UnsatisfiedResolutionException | Ensure dependency is a CDI bean with appropriate scope |
| Multiple implementations | AmbiguousResolutionException | Use @Named or custom qualifiers |
| Circular dependencies | DeploymentException | Refactor architecture or use Instance<T> for lazy init |
// Disambiguate with @Named
@ApplicationScoped
public class PaymentService {
public PaymentService(@Named("primary") PaymentGateway gateway) {
// Uses specifically qualified implementation
}
}
pm-dev-java:java-cdi-quarkus - Quarkus-specific CDI patterns, container/Docker config, securitypm-dev-java:java-core - Core Java patternspm-dev-java:junit-core - CDI testing patterns