Context and Dependency Injection (CDI 1.
1)
Decouple Components with CDI
Antonio Goncalves
www.antoniogoncalves.org
@agoncal
Course Outline
Introduction
Understanding Context & Dependency Injection
Dependency Injection
Producers and Disposers
Interceptors, Decorators and Events
Bringing the Web Tier and Service Tier Together
Context & Dependency Injection 1.1 within Java EE 7
Audience
Usage of Context and Dependency Injection
☁
Module Outline
What is dependency?
How do we usually depend on components?
What’s wrong with the way we do?
How can CDI help?
What Is Dependency?
Object-oriented programming
Classes depending on other classes
A uses another class B
A depends on B
A cannot work without B
A is called the "dependent"
B is called the "dependency"
Strongly or loosely coupled
Strongly Coupled Dependencies
Strong Coupling Between Classes
public class IsbnGenerator {
public String generateNumber() {
return "13-84356-" + Math.abs(new Random().nextInt());
}
}
public class BookService {
private IsbnGenerator isbn = new IsbnGenerator();
public Book createBook(String title) {
return new Book(title, isbn.generateNumber());
}
}
What’s Wrong with Strong Coupling?
Only creates books with ISBN numbers
Tightly coupled
Strong coupling decreases reusability
Development speed
Code quality
Code readability
Loosely Coupled Dependencies
Interface and Implementations
public interface NumberGenerator {
String generateNumber();
}
public class IsbnGenerator implements NumberGenerator {
public String generateNumber() {
return "13-84356-" + Math.abs(new Random().nextInt());
}
}
public class IssnGenerator implements NumberGenerator {
public String generateNumber() {
return "8-" + Math.abs(new Random().nextInt());
}
}
Choosing Implementations
public class BookService {
private NumberGenerator generator;
public BookService(NumberGenerator generator) {
this.generator = generator;
}
public Book createBook(String title) {
return new Book(title, generator.generateNumber());
}
}
BookService service= new BookService(new IsbnGenerator());
BookService service= new BookService(new IssnGenerator());
What’s Wrong with Constructing by Hand?
Inversion of control design pattern
Connecting the dependencies ourselves
Constructor injection
Constructing dependencies programmatically by hand
Not flexible
An injector can do it
Injecting with CDI
Standard
Manages dependencies
Strongly typed annotations
XML configuration
Removes construction by hand
Many other features to dependency injection @
Interface and Implementations
public interface NumberGenerator {
String generateNumber();
}
public class IsbnGenerator implements NumberGenerator {
public String generateNumber() {
return "13-84356-" + Math.abs(new Random().nextInt());
}
}
public class IssnGenerator implements NumberGenerator {
public String generateNumber() {
return "8-" + Math.abs(new Random().nextInt());
}
}
Injecting with CDI
public class BookService {
@Inject
private NumberGenerator generator;
public BookService(NumberGenerator generator) {
this.generator = generator;
}
public Book createBook(String title) {
return new Book(title, generator.generateNumber());
}
}
BookService service= new BookService(new IsbnGenerator());
Injecting with CDI
public class BookService {
@Inject
private NumberGenerator generator;
public Book createBook(String title) {
return new Book(title, generator.generateNumber());
}
}
@Inject
BookService service;
Injecting with CDI
public class BookService {
@Inject @ThirteenDigits
private NumberGenerator generator;
public Book createBook(String title) {
return new Book(title, generator.generateNumber());
}
}
@Inject
BookService service;
Injecting with CDI
public class BookService {
@Inject @EightDigits
private NumberGenerator generator;
public Book createBook(String title) {
return new Book(title, generator.generateNumber());
}
}
@Inject
BookService service;
Injecting with CDI
public class BookService {
@Inject @FiveDigits
private NumberGenerator generator;
public Book createBook(String title) {
return new Book(title, generator.generateNumber());
}
}
@Inject
BookService service;
Advantages of CDI
Inject classes into others in a typesafe way
Managed environment
Don’t construct dependencies by hand
No boiler plate code
@Inject
Powerful
More features
Summary
Dependency
Classes that depend on others
Tight or loose
Strong coupling, loose coupling
Decoupling brings modularity and reusability
Programmatically
Use an injector
Context & Dependency Injection is standard