© Michael Müller 2018
Michael MüllerPractical JSF in Java EE 8 https://doi.org/10.1007/978-1-4842-3030-5_16

16. Contexts and Dependency Injection

Michael Müller1 
(1)
Brühl, Nordrhein-Westfalen, Germany
 

Sometimes you need an instance of an object with a type that might be changed or determined at runtime, and you need a reliable mechanism to perform that task. Luckily, Java EE 8 comes with Contexts and Dependency Injection (CDI), which can solve the problem.

From new() to CDI

If you write a class that needs access to an underlying object-oriented service, you need to obtain an instance of this service and call its methods. One simple approach is to create a new object of the service class:

DataService dataService = new DataService();

That approach has ...

Get Practical JSF in Java EE 8: Web Applications ​in Java for the Enterprise now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.