1 Context Dependency Injection 1 1 m1 Slides
1 Context Dependency Injection 1 1 m1 Slides
1)
Decouple Components with CDI
Antonio Goncalves
www.antoniogoncalves.org
@agoncal
Course Outline
Introduction
Understanding Context & Dependency Injection
Dependency Injection
Usage of Context and Dependency Injection
☁
Module Outline
What is dependency?
Object-oriented programming
Tightly coupled
Loosely Coupled Dependencies
Interface and Implementations
public interface NumberGenerator {
String generateNumber();
}
Constructor injection
Constructing dependencies programmatically by hand
Not flexible
An injector can do it
Injecting with CDI
Standard
Manages dependencies
@Inject
private NumberGenerator generator;
@Inject
private NumberGenerator generator;
@Inject
BookService service;
Injecting with CDI
public class BookService {
@Inject @ThirteenDigits
private NumberGenerator generator;
@Inject
BookService service;
Injecting with CDI
public class BookService {
@Inject @EightDigits
private NumberGenerator generator;
@Inject
BookService service;
Injecting with CDI
public class BookService {
@Inject @FiveDigits
private NumberGenerator generator;
@Inject
BookService service;
Advantages of CDI
Inject classes into others in a typesafe way
Managed environment
@Inject
Powerful
More features
Summary
Dependency
Classes that depend on others
Tight or loose
Programmatically
Use an injector