3 Context Dependency Injection 1 1 m3 Slides
3 Context Dependency Injection 1 1 m3 Slides
Antonio Goncalves
www.antoniogoncalves.org
@agoncal
Module Outline
Basic dependency injection
Qualifiers
Advanced qualifiers
Veto
Alternatives
Basic Dependency Injection
Objects
Strong typing
Depending on a Class
The IsbnGenerator Class
public class IsbnGenerator {
@Inject
private IsbnGenerator generator;
}
Injection Point on Field
public class BookService {
}
Injection Point on Constructor
public class BookService {
Injection Point
private IsbnGenerator generator;
@Inject
public BookService(IsbnGenerator generator) {
this.generator = generator;
}
}
Injection Point on Setter
public class BookService {
Injection Point
private IsbnGenerator generator;
@Inject
public void setGenerator(IsbnGenerator generator) {
this.generator = generator;
}
}
Deployment Descriptor
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
</beans>
beans.xml
Deployment Descriptor
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="none">
</beans>
beans.xml
Deployment Descriptor
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="annotated">
</beans>
beans.xml
Depending on One Implementation
Depending on One Implementation
Interface and One Implementation
public interface NumberGenerator {
String generateNumber();
}
@Inject
private NumberGenerator generator;
@ThirteenDigits
public class IsbnGenerator implements NumberGenerator {
public String generateNumber() {
return "13-84356-" + Math.abs(new Random().nextInt());
}
}
@EightDigits
public class IssnGenerator implements NumberGenerator {
public String generateNumber() {
return "8-" + Math.abs(new Random().nextInt());
}
}
Injecting the IsbnGenerator
public class BookService {
@Inject
private NumberGenerator generator;
@Inject @ThirteenDigits
private NumberGenerator generator;
@Inject @EightDigits
private NumberGenerator generator;
…
Qualifier with Members
@Target({FIELD, TYPE, METHOD})
@Retention(RUNTIME)
@Qualifier
public @interface Generator {
NumberOfDigits numberOfDigits();
boolean printed();
@EightDigits @Electronic
public class EIssnGenerator implements NumberGenerator {
...
}
@ThirteenDigits @Print
public class PIsbnGenerator implements NumberGenerator {
...
}
@EightDigits @Print
public class PIssnGenerator implements NumberGenerator {
...
}
Injecting a Bean with Multiple Qualifiers
public class BookService {
bean-discovery-mode="annotated"
bean-discovery-mode="all"
bean-discovery-mode="none"
Veto a bean
Veto a package
@Vetoed
</beans>
beans.xml
Veto
One Bean Is Vetoed
public class BookService {
@Inject @Default
private NumberGenerator generator;
@Vetoed
public class IsbnGenerator implements NumberGenerator {
public String generateNumber() {...}
}
@Vetoed
package com.pluralsight.injection;
import javax.enterprise.inject.Vetoed;
Alternatives
Alternatives at deployment time
@Alternative
beans.xml
Default Alternative
@Alternative Implementation
public interface NumberGenerator {
String generateNumber();
}
@Default
public class IsbnGenerator implements NumberGenerator {
public String generateNumber() {
return "13-84356-" + Math.abs(new Random().nextInt());
}
}
@Default
public class MockGenerator implements NumberGenerator {
public String generateNumber() {
return "mock";
}
}
@Alternative Implementation
public interface NumberGenerator {
String generateNumber();
}
@Alternative
public class MockGenerator implements NumberGenerator {
public String generateNumber() {
return "mock";
}
}
Injecting the Alternative
public class BookService {
@Inject
private NumberGenerator generator;
<alternatives>
<class>com.pluralsight.injection.MockGenerator</class>
</alternatives>
</beans>
Alternative for a Given Qualifier
Summary
Dependency injection
Default injection
Qualifiers
Type safe
Vetos
Alternatives