Skip to content
This repository was archived by the owner on Sep 26, 2025. It is now read-only.

Commit 4a53760

Browse files
committed
On the Way to CDI 4.0 post.
1 parent 55bdc05 commit 4a53760

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

_config/identities.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ thj:
1313
ladt:
1414
firstName: "Ladislav"
1515
lastName: "Thon"
16+
matn:
17+
firstName: "Matej"
18+
lastName: "Novotny"
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
title: "On the Way to CDI 4.0"
3+
summary: Overview of changes coming to CDI 4.0
4+
layout: post
5+
authors: [matn]
6+
tags: [Release]
7+
desc: Overview of changes coming to CDI 4.0
8+
priority: 1
9+
change_frequency: daily
10+
---
11+
12+
As you probably know, work is well under way for projects aiming to land in Jakarta EE 10 space and CDI is no exception. In fact, the specification is close to being rounded up for its next major release hence it is the right time to summarize what is coming.
13+
14+
Here is a quick overview of impending changes; we’ll look into each of them separately:
15+
16+
* Introduction of CDI Lite
17+
* Change of default empty `beans.xml` discovery mode
18+
* Removal of long deprecated API bits
19+
* Quality of life improvements
20+
21+
== Introduction of CDI Lite
22+
23+
The split of specification to CDI Full and CDI Lite is by far the biggest change coming in 4.0 and is therefore split into several parts.
24+
25+
=== Little Bit of History
26+
27+
The http://www.cdi-spec.org/news/2020/03/09/CDI_for_the_future/[original proposal] for CDI Lite was published in March 2020 and if you browse http://www.eclipse.org/lists/cdi-dev[the CDI mailing list], you’ll see that there have been many threads on this topic since then. And those discussions, meetings and proposals have been fruitful because later on, precisely in May 2021, a https://www.eclipse.org/lists/cdi-dev/msg00361.html[community vote] took place and decided that CDI Lite is about to happen. To this date, two projects are known to be working on a build-time CDI Lite implementation: Quarkus, through its ArC subproject, and OpenDI, which is based on top of Micronaut.
28+
29+
=== What is and isn’t Lite
30+
31+
The specification was split into Lite and Full parts in a way that Lite defines a set of core features and Full then builds on top of it, expanding sets of rules and presenting additional features. This means that CDI Lite is simply a subset of CDI Full - you can use Lite APIs in a portable way in CDI Full environments and every CDI Full implementation has to also implement CDI Lite. That being said, CDI Full remains ‘runtime oriented’ as opposed to Lite implementations which are likely to focus on build-time environments even though the specification sets no limits in this regard.
32+
33+
Below is a list of CDI features that are now exclusive to CDI Full implementations:
34+
35+
* Decorators
36+
* Interception bound via `@Interceptors` annotation
37+
** Interceptors bound via interceptor binding annotations work perfectly well in CDI Lite
38+
* Specialization
39+
* Passivation
40+
* Conversation Scope
41+
** All other built-in scopes are present in CDI Lite
42+
* Portable Extensions
43+
** These are replaced by a new extension API which is described in the following paragraph
44+
45+
It is important to reiterate that existing applications are all considered to use CDI Full and as such will continue working unimpeded.
46+
47+
=== Introduction of Build Compatible Extensions
48+
49+
Portable Extensions are crucial for CDI extensibility but are also an ill fit for any build oriented framework. Therefore, an alternative extension SPI was proposed. This was covered in depth in a http://www.cdi-spec.org/news/2020/09/15/CDI_Lite_extension/[previous blog post] and has undergone many iterations since. However, its purpose stays ultimately the same - to provide an alternative for Portable Extensions that will work in all environments.
50+
51+
=== New Underlying Metamodel
52+
53+
One of the goals of CDI Lite was to shake off as much reflection as possible. Nonetheless, current metamodel (`AnnotatedType` and friends) is very reflection-heavy which is why CDI Lite comes with new metamodel abstraction. Note that CDI Full and Portable Extensions will still use the old metamodel whereas CDI Lite and Build Compatible Extensions will use the new metamodel.
54+
55+
This metamodel is currently housed inside CDI JAR but there has been some interest in using it from other Jakarta projects so it might end up being shipped as a separate artifact.
56+
57+
=== Shipping CDI 4.0 JAR
58+
59+
One of the questions on many meetings was around how to ship CDI JAR now that there is Lite and Full combined. The answer to that is that there is no clear way of achieving that without breaking existing applications through package name changes. Therefore, as a compromise, CDI will stay a single JAR containing all the APIs.
60+
61+
== Change of Default Empty `beans.xml` Discovery Mode
62+
63+
One notable change coming to CDI is the https://github.com/eclipse-ee4j/cdi/issues/500[discovery mode of bean archives containing empty `beans.xml`]. These were formerly treated as explicit bean archives (i.e. having discovery mode `all`) but beginning with CDI 4.0, they will be considered as implicit bean archives (discovery mode `annotated`).
64+
65+
In other words, if a class in such an archive is to be recognized as a bean, it needs to have at least one bean defining annotation. It can be a breaking change which is why this change also comes with a backward compatible switch - all CDI Full products have to contain an option that switches the behavior to how it worked in the past. The option is of course a temporary measure to ease transition and applications are encouraged to adapt their bean archives accordingly in the long term.
66+
67+
For those interested in the history of this decision, there was a https://www.eclipse.org/lists/cdi-dev/msg00353.html[community survey], posted via CDI mailing list, detailing the reasons and options with results visible https://docs.google.com/forms/d/e/1FAIpQLSes003JAWpieL6Rp80Js-vhuMcTril_olbnL2TBY5pM5PvVdA/viewanalytics[here].
68+
69+
=== Removal of Deprecated API Bits
70+
CDI has been around for over a decade and as such has accumulated a bunch of deprecated APIs that were carried over and over into each subsequent version. A cleanup was already well overdue, so https://github.com/eclipse-ee4j/cdi/issues/472[here goes]!
71+
72+
Note that the deprecated removed APIs all have a replacement. Therefore, no functionality is lost even though you might need to change your application accordingly. Below is a list of removals along with their suggested replacements.
73+
74+
* `@New` qualifier
75+
** Replaced by `@Dependent` beans since CDI 1.1
76+
* `Bean#isNullable()` method
77+
** Related to the above
78+
** It is safe to just remove this method from any custom implementation of Bean<T> interface as it wasn’t used
79+
* `BeanManager#fireEvent()` method
80+
** Deprecated since CDI 2.0; this method was insufficient ever since CDI introduced async events
81+
** Users should use `BeanManager.getEvent()` instead
82+
* `BeanManager#createInjectionTarget(AnnotatedType)` method
83+
** Replaced by `BeanManager#getInjectionTargetFactory(AnnotatedType)` since CDI 1.1
84+
* `BeforeBeanDiscovery#addAnnotatedType(AnnotatedType)` method
85+
** Replaced by `BeforeBeanDiscovery#addAnnotatedType(AnnotatedType, String)` since CDI 1.1
86+
* Removed the notion of a mandatory configuration switch for all containers to support different understanding of missing `beans.xml` file
87+
** Refers to the https://jakarta.ee/specifications/cdi/3.0/jakarta-cdi-spec-3.0.html#bean_archive[following specification sentence]:
88+
*** _For compatibility with Contexts and Dependency 1.0, products must contain an option to cause an archive to be ignored by the container when no beans.xml is present_.
89+
** The behavioral change exists since CDI 1.1
90+
91+
== Quality of Life Improvements
92+
93+
This chapter summarizes some of the minor changes and additions to the specification which won’t make or break your application but can, hopefully, make your life easier.
94+
95+
=== Observable Container State Events
96+
97+
Up until now, the only way to know that a CDI container has started was to listen for an event with qualifier `@Initialized(ApplicationScoped.class)`. However, this event https://github.com/eclipse-ee4j/cdi/issues/496[isn’t a true container state event]; it is a context-related event. Not to mention that in build oriented stacks, contexts can be started prior to runtime making this event useless. Similarly, container shutdown only has `@BeforeDestroyed(ApplicationScoped.class)` which was the closest call but not quite on the mark.
98+
Therefore, CDI 4.0 will fire an event with payload `jakarta.enterprise.event.Startup` and qualifier `@Any` during container startup. Users and integrators can declare observers in order to perform their early initialization tasks as soon as the CDI container is truly ready. Symmetrically, the container will fire an event with payload `jakarta.enterprise.event.Shutdown` and qualifier `@Any` during application shutdown.
99+
100+
Observers are encouraged to declare `@Priority` in a similar fashion that interceptors do - this enables ordering scheme in which, for example, integrator-defined observer methods for `Startup` event precede those defined by user application.
101+
102+
=== Programmatic Lookup Improvements
103+
104+
One of the pitfalls of programmatic lookup is that there is no easy way to browse bean metadata. Besides, iterating over all bean candidates immediately creates bean instances of all `@Dependent` beans which isn’t ideal.
105+
106+
These were the main motivations behind https://github.com/eclipse-ee4j/cdi/issues/521[introduction of the `Handle<T>` interface] - an abstraction representing a contextual reference handle and allowing browsing its `Bean<T>` metadata, retrieving contextual instance and destroying it if needed.
107+
108+
Handles can be grabbed directly from `Instance<T>` through several methods:
109+
110+
* `Handle<T> getHandle()`
111+
* `Iterable<Handle<T>> handles()`
112+
* `Stream<Handle<T>> handlesStream()`
113+
114+
=== Using `@Priority` on Stereotypes
115+
116+
Another small change is that https://github.com/eclipse-ee4j/cdi/issues/495[CDI Stereotypes can now declare `@Priority`]; any bean declaring such stereotype will be considered enabled and have given priority. A bean can declare priority explicitly in order to override the value.
117+
118+
This is a nice shorthand for test scenarios which commonly need to declare enabled alternatives which mock or stub application behavior.
119+
120+
== Conclusion
121+
122+
CDI 4.0 specification is nearing its completion and this article should provide you with an overview of changes and how to prepare for them. While a Beta or CR release will hopefully happen shortly, there is always the option to go over to https://github.com/eclipse-ee4j/cdi[CDI GH repository], check out the main branch and build it with a good old `mvn clean install`.
123+
124+
Last but not least, a compatible implementation for CDI Full will still be Weld which is now being worked on to stay on top of CDI API changes. As soon as CDI releases a Beta version, Weld will follow with its own release. So stay tuned if you want to try things out early!
125+

0 commit comments

Comments
 (0)