-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathejb-vs-cdi.yaml
More file actions
66 lines (64 loc) · 2.04 KB
/
Copy pathejb-vs-cdi.yaml
File metadata and controls
66 lines (64 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
---
id: 99
slug: "ejb-vs-cdi"
title: "EJB versus CDI"
category: "enterprise"
difficulty: "intermediate"
jdkVersion: "11"
oldLabel: "Java EE"
modernLabel: "Jakarta EE 8+"
oldApproach: "EJB"
modernApproach: "CDI Bean"
oldCode: |-
@Stateless
public class OrderEJB {
@EJB
private InventoryEJB inventory;
public void placeOrder(Order order) {
// container-managed transaction
inventory.reserve(order.getItem());
}
}
modernCode: |-
@ApplicationScoped
public class OrderService {
@Inject
private InventoryService inventory;
@Transactional
public void placeOrder(Order order) {
inventory.reserve(order.getItem());
}
}
summary: "Replace heavyweight EJBs with lightweight CDI beans for dependency injection\
\ and transactions."
explanation: "CDI (Contexts and Dependency Injection) provides the same dependency\
\ injection and transaction management as EJBs, but as plain Java classes with no\
\ container-specific interfaces or superclasses. Scopes like @ApplicationScoped\
\ and @RequestScoped control lifecycle, and @Transactional replaces mandatory EJB\
\ transaction semantics."
whyModernWins:
- icon: "🪶"
title: "Lightweight"
desc: "CDI beans are plain Java classes with no EJB-specific interfaces or descriptors."
- icon: "💉"
title: "Unified injection"
desc: "@Inject works for every managed bean, JAX-RS resources, and Jakarta EE components\
\ alike."
- icon: "🧪"
title: "Easy unit testing"
desc: "Plain classes without EJB proxy overhead are straightforward to instantiate\
\ and mock."
support:
state: "available"
description: "Widely available since Jakarta EE 8 / Java 11"
prev: "enterprise/servlet-vs-jaxrs"
next: "enterprise/jdbc-vs-jpa"
related:
- "enterprise/servlet-vs-jaxrs"
- "enterprise/jdbc-vs-jpa"
- "language/records-for-data-classes"
docs:
- title: "Jakarta CDI Specification"
href: "https://jakarta.ee/specifications/cdi/"
- title: "Jakarta Transactions — @Transactional"
href: "https://jakarta.ee/specifications/transactions/"