-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathejb-vs-cdi.json
More file actions
54 lines (54 loc) · 2.27 KB
/
ejb-vs-cdi.json
File metadata and controls
54 lines (54 loc) · 2.27 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
{
"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\npublic class OrderEJB {\n @EJB\n private InventoryEJB inventory;\n\n public void placeOrder(Order order) {\n // container-managed transaction\n inventory.reserve(order.getItem());\n }\n}",
"modernCode": "@ApplicationScoped\npublic class OrderService {\n @Inject\n private InventoryService inventory;\n\n @Transactional\n public void placeOrder(Order order) {\n inventory.reserve(order.getItem());\n }\n}",
"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/"
}
]
}