SlideShare a Scribd company logo
1 of 53
28 August 2015
Introduction to CDI
1
1 Introduction to CDI
2 Basic Dependency Injection
3 Using Qualifiers to Distinguish Beans of the Same Type
4 Contexts
5 Producers and Disposers
6 Events
7 Interceptors
8 Decorators
9 CDI in Java SE
10 Using CDI and JPA Together
Agenda
2
Introduction to CDI
• Java EE dependency injection standard
• Can also be used in Java SE environments with
some help
• Strongly typed and type safe
• Provides context management
• Activated by default since CDI 1.1
• Use beans.xml to activate alternative beans, etc.
• Highly Extensible via CDI SPI (not covered here)
What is CDI?
3
Introduction to CDI
• Managed Beans are basic components
• Managed by the container
• Have a lifecycle
• Can be intercepted (technical concerns)
and decorated (business concerns)
• Can be injected
• Are accessible outside CDI code
CDI Beans
4
Agenda
1 Introduction to CDI
2 Basic Dependency Injection
3 Using Qualifiers to Distinguish Beans of the Same Type
4 Contexts
5 Producers and Disposers
6 Events
7 Interceptors
8 Decorators
9 CDI in Java SE
10 Using CDI and JPA Together
5
Basic Dependency Injection
public class HowdyService {
public String howdy() {
return "Howdy!";
}
}
This is a bean
6
Basic Dependency Injection
public class MyBean {
private HowdyService service;
@Inject
public MyBean(HowdyService service) {
this.service = service;
}
}
Dependency Injection in Constructors
7
Basic Dependency Injection
public class MyBean {
private HowdyService service;
@Inject
public void setService(HowdyService service) {
this.service = service;
}
}
Dependency Injection in Setter
8
Basic Dependency Injection
public class MyBean {
@Inject
private HowdyService service;
public void sayHowdy() {
display(service.howdy());
}
}
Dependency Injection in Fields
9
Basic Dependency Injection
This Works!
public class MyBean {
@Inject Service<User> userService;
@Inject Service<Staff> staffService;
}
No Type Erasure in CDI
10
1 Introduction to CDI
2 Basic Dependency Injection
3 Using Qualifiers to Distinguish Beans of the Same Type
4 Contexts
5 Producers and Disposers
6 Events
7 Interceptors
8 Decorators
9 CDI in Java SE
10 Using CDI and JPA Together
11
Using Qualifiers to Distinguish Beans of the Same Type
public interface HelloService {
public String hello();
}
public class TexanHelloService implements HelloService {
public String hello() {
return "Howdy!";
}
}
public class AustralianHelloService implements HelloService {
public String hello() {
return "G’day!";
}
}
Multiple Service Implementations
12
Using Qualifiers to Distinguish Beans of the Same Type
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, TYPE, METHOD, PARAMETER})
public @interface Texan {}
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, TYPE, METHOD, PARAMETER})
public @interface Australian {}
We Create Qualifiers
13
Using Qualifiers to Distinguish Beans of the Same Type
@Texan
public class TexanHelloService implements HelloService {
public String hello() {
return "Howdy!";
}
}
@Australian
public class AussieHelloService implements HelloService {
public String hello() {
return "G’day!";
}
}
And Add Them to Our Implementations
14
Using Qualifiers to Distinguish Beans of the Same Type
public class MyTexanBean {
@Inject @Texan HelloService service;
public void displayHello() {
display(service.hello());
}
}
public class MyAussieBean {
@Inject @Australian HelloService service;
public void displayHello() {
display(service.hello());
}
}
And Specify at our Injection Site
15
Using Qualifiers to Distinguish Beans of the Same Type
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, TYPE, METHOD, PARAMETER})
public @interface Language {
public enum Languages {
TEXAN, AUSTRALIAN
}
Languages value();
@Nonbinding String description() default "";
}
Qualifiers can have Members
16
Using Qualifiers to Distinguish Beans of the Same Type
@Language(TEXAN)
public class TexanHelloService implements HelloService {
public String hello() {
return "Howdy!";
}
}
@Language(AUSTRALIAN)
public class AussieHelloService implements HelloService {
public String hello() {
return "G’day!";
}
}
Class definition for Qualifiers with Members
17
Using Qualifiers to Distinguish Beans of the Same Type
public class MyTexanBean {
@Inject @Language(TEXAN) HelloService service;
public void displayHello() {
display(service.hello());
}
}
public class MyAussieBean {
@Inject @Language(AUSTRALIAN) HelloService service;
public void displayHello() {
display(service.hello());
}
}
Injection using Qualifiers with Members
18
Using Qualifiers to Distinguish Beans of the Same Type
public class MyTexanBean {
@Inject @Texan HelloService service;
public void displayHello() {
display(service.hello());
}
}
@Texan @Console @Secured
public class TexanHelloService implements HelloService {
public String hello() {
return "Howdy!";
}
}
Multiple Qualifiers
19
Using Qualifiers to Distinguish Beans of the Same Type
public class MyTexanBean {
@Inject @Texan @Console HelloService service;
public void displayHello() {
display(service.hello());
}
}
@Texan @Console @Secured
public class TexanHelloService implements HelloService {
public String hello() {
return "Howdy!";
}
}
Multiple Qualifiers
20
Using Qualifiers to Distinguish Beans of the Same Type
public class MyTexanBean {
@Inject @Texan @Console @Secured HelloService service;
public void displayHello() {
display(service.hello());
}
}
@Texan @Console @Secured
public class TexanHelloService implements HelloService {
public String hello() {
return "Howdy!";
}
}
Multiple Qualifiers
21
Using Qualifiers to Distinguish Beans of the Same Type
public class MyTexanBean {
@Inject @Texan @Console @Secured HelloService service;
public void displayHello() {
display(service.hello());
}
}
@Texan @Console
public class TexanHelloService implements HelloService {
public String hello() {
return "Howdy!";
}
}
Multiple Qualifiers – Won’t work
22
Using Qualifiers to Distinguish Beans of the Same Type
@Default
@Any
@Named
Reserved Qualifiers
23
Using Qualifiers to Distinguish Beans of the Same Type
public class MyBean {
@Inject @Any
Instance<HelloService> service;
public void displayHello() {
HelloService helloService =
service.select(new AnnotationLiteral<Texan>(){}).get();
display(helloService.hello());
}
}
Programmatic Bean Lookup (a.k.a. Lazy Injection)
24
Using Qualifiers to Distinguish Beans of the Same Type
@Texan @Vetoed
public class TexanHelloService implements HelloService {
public String hello() {
return "Howdy!";
}
}
package-info.java class:
@Vetoed
package cdi_experiments;
import javax.enterprise.inject.Vetoed;
Ignoring Beans and Packages with @Vetoed
25
Using Qualifiers to Distinguish Beans of the Same Type
@Texan @Alternative
public class MyHowdyYallService implements HelloService {
public String hello() {
return "Howdy y'all!";
}
}
<beans …>
<alternatives>
<class>cdi_experiments.MyHowdyYallService</class>
</alternatives>
</beans>
Specifying and using Alternative Beans
26
1 Introduction to CDI
2 Basic Dependency Injection
3 Using Qualifiers to Distinguish Beans of the Same Type
4 Contexts
5 Producers and Disposers
6 Events
7 Interceptors
8 Decorators
9 CDI in Java SE
10 Using CDI and JPA Together
27
Contexts
Help the container to choose when a bean should be intstantiated and destroyed
Enforce singleton pattern for a given context
Built-in CDI contexts:
• @Dependent (default)
• @ApplicationScoped, @SessionScoped, @RequestSCoped
• @ConversationScoped
• @Singleton
You can create your own scope
Contexts Manage Bean Lifecycle
28
Contexts
@SessionScoped
public class CartBean {
public void addItem(Item item) {
...
}
}
A shopping cart is almost always tied to a user’s session,
so using the @SessionScoped context makes the most
sense.
Choosing the Right Context
29
Contexts
@ApplicationScoped
public class CartBean {
public void addItem(Item item) {
...
}
}
BAD IDEA! Having a single cart used across all users
doesn’t make any sense.
30
1 Introduction to CDI
2 Basic Dependency Injection
3 Using Qualifiers to Distinguish Beans of the Same Type
4 Contexts
5 Producers and Disposers
6 Events
7 Interceptors
8 Decorators
9 CDI in Java SE
10 Using CDI and JPA Together
31
Producers and Disposers
@Produces
public OldRanchHand oldRanchHandProducer() {
return new OldRanchHand();
}
…
@Inject OldRanchHand ranchHand;
The return type and the injection type are identical. CDI uses this type information to inject produced
values.
To inject different values into different injection points of the same type, use qualifiers
Method producers can be used to generate complex objects
Create a Bean from any Class
32
Producers and Disposers
@Produces
public Logger produceLog(InjectionPoint injectionPoint) {
return Logger.getLogger(injectionPoint.getMember()
.getDeclaringClass().getName());
}
Getting Injection Point Information
33
Producers and Disposers
@Produces
public <K, V> Map<K, V> produceMap(InjectionPoint ip) {
if (valueIsNumber(ip.getType())) {
return new TreeMap<K, V>();
}
return new HashMap<K, V>();
}
No Type Erasure Enables Fancy Stuff
34
1 Introduction to CDI
2 Basic Dependency Injection
3 Using Qualifiers to Distinguish Beans of the Same Type
4 Contexts
5 Producers and Disposers
6 Events
7 Interceptors
8 Decorators
9 CDI in Java SE
10 Using CDI and JPA Together
35
Events
public class MyTexanBean {
@Inject Event<HelloService> helloEvent;
@Inject @Texan HelloService service;
public void displayHello() {
display(service.hello());
}
public void iSaidHowdy(HelloService helloService) {
helloEvent.fire(helloService);
}
}
public class HelloListenerBean {
public void listenForHello(@Observes HelloService hello) {
System.out.println(hello.hello() + " to you too!");
}
}
36
Events
public class HelloListenerBean {
public void listenForHello(@Observes @Texan HelloService hello) {
System.out.println(hello.hello() + " to you too!");
}
}
Qualifiers on Observers
37
Events
The producer must wait for all observers to execute before
continuing
If any observer throws an exception, all remaining observer
methods are not called and the exception is thrown by the
firing observing method
Observers have no priority in CDI 1.1, but is on the
roadmap for 2.0
Some Caveats
38
1 Introduction to CDI
2 Basic Dependency Injection
3 Using Qualifiers to Distinguish Beans of the Same Type
4 Contexts
5 Producers and Disposers
6 Events
7 Interceptors
8 Decorators
9 CDI in Java SE
10 Using CDI and JPA Together
39
Interceptors
Classes that provide technical cross-cutting functionality
are interceptors
Technical cross-cutting concerns include:
• Transactions
• Security
• Logging
Interceptors are not enabled by default, but can be enabled
and ordered in beans.xml
Interceptors can have multiple qualifiers
Interceptors
40
Interceptors
Interceptor bindings are qualifiers that instruct the CDI
container to intercept a method or all methods on a class
@InterceptorBinding
@Target({METHOD, TYPE})
@Retention(RUNTIME)
public @interface Loggable {}
Qualified Interceptor Binding
41
Interceptors
@Interceptor @Loggable
@Priority(Interceptor.Priority.APPLICATION)
public class LogInterceptor {
@AroundInvoke
public Object log(InvocationContext ic) throws Exception {
System.out.println("Entering " + ic.getMethod().getName());
try {
return ic.proceed();
} finally {
System.out.println("Exiting " + ic.getMethod().getName());
}
}
}
Interceptor Class
42
Interceptors
@Loggable
public class MyTexanBean {
@Inject @Texan HelloService service;
public void displayHello() {
display(service.hello());
}
}
Intercepting a Class or Method
43
Interceptors
@Interceptor @Loggable
public class FriendlyCowboy {
@PostConstruct
public void smile(InvocationContext invocationContext) {
//smile
}
@PreDestroy
public void tipHat(InvocationContext invocationContext) {
//tip hat
}
}
Lifecycle Interceptors
44
1 Introduction to CDI
2 Basic Dependency Injection
3 Using Qualifiers to Distinguish Beans of the Same Type
4 Contexts
5 Producers and Disposers
6 Events
7 Interceptors
8 Decorators
9 CDI in Java SE
10 Using CDI and JPA Together
45
Decorators
Classes that provide additional business functionality are decorators
Interceptors are called before decorators
Decorator classes must implement the same interface(s) as the
classes that they are decorating
Can be abstract if it only decorates some of the methods in the
interface
Enabled in the <decorators> section in the beans.xml file and can be
enabled and ordered in the beans.xml and via the @Priority
annotation
Decorators
46
Decorators
@Decorator
public abstract class HelloDec implements HelloService {
// The decorated service may be restricted with qualifiers
@Inject
@Delegate
HelloService service;
public String hello() {
return service.hello() + "-decorated";
}
}
Short Example
47
1 Introduction to CDI
2 Basic Dependency Injection
3 Using Qualifiers to Distinguish Beans of the Same Type
4 Contexts
5 Producers and Disposers
6 Events
7 Interceptors
8 Decorators
9 CDI in Java SE
10 Using CDI and JPA Together
48
CDI in Java SE
Currently possible through the DeltaSpike library
Slated for standardization in CDI 2.0
public class MainApp {
public static void main(String[] args) {
CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
cdiContainer.boot();
// Starting the application-context enables use of @ApplicationScoped beans
ContextControl contextControl = cdiContainer.getContextControl();
contextControl.startContext(ApplicationScoped.class);
// You can use CDI here
//...
cdiContainer.shutdown();
}
}
CDI – Not Just for JEE!
49
1 Introduction to CDI
2 Basic Dependency Injection
3 Using Qualifiers to Distinguish Beans of the Same Type
4 Contexts
5 Producers and Disposers
6 Events
7 Interceptors
8 Decorators
9 CDI in Java SE
10 Using CDI and JPA Together
50
Using CDI and JPA Together
CDI Beans and JPA Beans have different lifecycles
As a result, CDI Beans can’t be injected directly into JPA Beans
EntityListeners can be used to bridge CDI and JPA when needed
CDI and JPA – Friends at Last
51
Using CDI and JPA Together
@Entity
@EntityListeners(PostListener.class)
public class Post implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "CREATED_DATE")
private Date created;
@Column(name = "LAST_MODIFIED_DATE")
private Date lastModified;
public Date getCreated() { return created; }
public void setCreated(Date created) { this.created = created; }
public Date getLastModified() { return lastModified; }
public void setLastModified(Date lastModified) { this.lastModified = lastModified;}
}
The JPA Class
52
Using CDI and JPA Together
public class PostListener {
@Inject Logger log;
@PrePersist
public void prePresist(Object o) {
log.info("call prePresist");
if (o instanceof Post) {
Post post = (Post) o;
final Date created = new Date();
post.setCreated(created);
post.setLastModified(created);
}
}
@PreUpdate
public void preUpdate(Object o) {
log.info("call preUpdate");
if (o instanceof Post) {
Post post = (Post) o;
post.setLastModified(new Date());
}
}
}

More Related Content

What's hot

Functional Dependency Injection in C#
Functional Dependency Injection in C#Functional Dependency Injection in C#
Functional Dependency Injection in C#Thomas Jaskula
 
Inversion of Control and Dependency Injection
Inversion of Control and Dependency InjectionInversion of Control and Dependency Injection
Inversion of Control and Dependency InjectionDinesh Sharma
 
Dependency Injection Inversion Of Control And Unity
Dependency Injection Inversion Of Control And UnityDependency Injection Inversion Of Control And Unity
Dependency Injection Inversion Of Control And Unityrainynovember12
 
Getting started with Java 9 modules
Getting started with Java 9 modulesGetting started with Java 9 modules
Getting started with Java 9 modulesRafael Winterhalter
 
Context and Dependency Injection 2.0
Context and Dependency Injection 2.0Context and Dependency Injection 2.0
Context and Dependency Injection 2.0Brian S. Paskin
 
Java(8) The Good, The Bad and the Ugly
Java(8) The Good, The Bad and the UglyJava(8) The Good, The Bad and the Ugly
Java(8) The Good, The Bad and the UglyBrian Vermeer
 
Indic threads pune12-java ee 7 platformsimplification html5
Indic threads pune12-java ee 7 platformsimplification html5Indic threads pune12-java ee 7 platformsimplification html5
Indic threads pune12-java ee 7 platformsimplification html5IndicThreads
 
To inject or not to inject: CDI is the question
To inject or not to inject: CDI is the questionTo inject or not to inject: CDI is the question
To inject or not to inject: CDI is the questionAntonio Goncalves
 
Java8 tgtbatu javaone
Java8 tgtbatu javaoneJava8 tgtbatu javaone
Java8 tgtbatu javaoneBrian Vermeer
 
Dependency Injections in Kotlin
Dependency Injections in KotlinDependency Injections in Kotlin
Dependency Injections in KotlinEatDog
 
Invoke dynamite in Java EE with invoke dynamic
Invoke dynamite in Java EE with invoke dynamicInvoke dynamite in Java EE with invoke dynamic
Invoke dynamite in Java EE with invoke dynamicAntoine Sabot-Durand
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Raffi Khatchadourian
 
CDI do básico ao avançado
CDI do básico ao avançadoCDI do básico ao avançado
CDI do básico ao avançadoAlberto Souza
 
Parsley & Flex
Parsley & FlexParsley & Flex
Parsley & Flexprideconan
 

What's hot (20)

Functional Dependency Injection in C#
Functional Dependency Injection in C#Functional Dependency Injection in C#
Functional Dependency Injection in C#
 
Inversion of Control and Dependency Injection
Inversion of Control and Dependency InjectionInversion of Control and Dependency Injection
Inversion of Control and Dependency Injection
 
Dependency Injection Inversion Of Control And Unity
Dependency Injection Inversion Of Control And UnityDependency Injection Inversion Of Control And Unity
Dependency Injection Inversion Of Control And Unity
 
Getting started with Java 9 modules
Getting started with Java 9 modulesGetting started with Java 9 modules
Getting started with Java 9 modules
 
Context and Dependency Injection 2.0
Context and Dependency Injection 2.0Context and Dependency Injection 2.0
Context and Dependency Injection 2.0
 
TY.BSc.IT Java QB U1
TY.BSc.IT Java QB U1TY.BSc.IT Java QB U1
TY.BSc.IT Java QB U1
 
Java(8) The Good, The Bad and the Ugly
Java(8) The Good, The Bad and the UglyJava(8) The Good, The Bad and the Ugly
Java(8) The Good, The Bad and the Ugly
 
Ecom lec4 fall16_jpa
Ecom lec4 fall16_jpaEcom lec4 fall16_jpa
Ecom lec4 fall16_jpa
 
Indic threads pune12-java ee 7 platformsimplification html5
Indic threads pune12-java ee 7 platformsimplification html5Indic threads pune12-java ee 7 platformsimplification html5
Indic threads pune12-java ee 7 platformsimplification html5
 
To inject or not to inject: CDI is the question
To inject or not to inject: CDI is the questionTo inject or not to inject: CDI is the question
To inject or not to inject: CDI is the question
 
Glassfish JEE Server Administration - Clustering
Glassfish JEE Server Administration - ClusteringGlassfish JEE Server Administration - Clustering
Glassfish JEE Server Administration - Clustering
 
Rhino Mocks
Rhino MocksRhino Mocks
Rhino Mocks
 
Java8 tgtbatu javaone
Java8 tgtbatu javaoneJava8 tgtbatu javaone
Java8 tgtbatu javaone
 
Dependency Injections in Kotlin
Dependency Injections in KotlinDependency Injections in Kotlin
Dependency Injections in Kotlin
 
Hibernate jpa 2 metamodel generator
Hibernate jpa 2 metamodel generatorHibernate jpa 2 metamodel generator
Hibernate jpa 2 metamodel generator
 
EJB Part-1
EJB Part-1EJB Part-1
EJB Part-1
 
Invoke dynamite in Java EE with invoke dynamic
Invoke dynamite in Java EE with invoke dynamicInvoke dynamite in Java EE with invoke dynamic
Invoke dynamite in Java EE with invoke dynamic
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
 
CDI do básico ao avançado
CDI do básico ao avançadoCDI do básico ao avançado
CDI do básico ao avançado
 
Parsley & Flex
Parsley & FlexParsley & Flex
Parsley & Flex
 

Similar to Introduction to CDI

1/3 : introduction to CDI - Antoine Sabot-Durand
1/3 : introduction to CDI - Antoine Sabot-Durand1/3 : introduction to CDI - Antoine Sabot-Durand
1/3 : introduction to CDI - Antoine Sabot-DurandSOAT
 
Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014Antoine Sabot-Durand
 
Using Contexts & Dependency Injection in the Java EE 6 Platform
Using Contexts & Dependency Injection in the Java EE 6 PlatformUsing Contexts & Dependency Injection in the Java EE 6 Platform
Using Contexts & Dependency Injection in the Java EE 6 PlatformArun Gupta
 
Spark IT 2011 - Context & Dependency Injection in the Java EE 6 Ecosystem
Spark IT 2011 - Context & Dependency Injection in the Java EE 6 EcosystemSpark IT 2011 - Context & Dependency Injection in the Java EE 6 Ecosystem
Spark IT 2011 - Context & Dependency Injection in the Java EE 6 EcosystemArun Gupta
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingSteven Smith
 
Extending Java EE with CDI and JBoss Forge
Extending Java EE with CDI and JBoss ForgeExtending Java EE with CDI and JBoss Forge
Extending Java EE with CDI and JBoss ForgeAntoine Sabot-Durand
 
softshake 2014 - Java EE
softshake 2014 - Java EEsoftshake 2014 - Java EE
softshake 2014 - Java EEAlexis Hassler
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFXHendrik Ebbers
 
Java onguice20070426
Java onguice20070426Java onguice20070426
Java onguice20070426Ratul Ray
 
Spring andspringboot training
Spring andspringboot trainingSpring andspringboot training
Spring andspringboot trainingMallikarjuna G D
 
Developing Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJSDeveloping Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJSShekhar Gulati
 
EJB et WS (Montreal JUG - 12 mai 2011)
EJB et WS (Montreal JUG - 12 mai 2011)EJB et WS (Montreal JUG - 12 mai 2011)
EJB et WS (Montreal JUG - 12 mai 2011)Montreal JUG
 
Diving into VS 2015 Day3
Diving into VS 2015 Day3Diving into VS 2015 Day3
Diving into VS 2015 Day3Akhil Mittal
 

Similar to Introduction to CDI (20)

1/3 : introduction to CDI - Antoine Sabot-Durand
1/3 : introduction to CDI - Antoine Sabot-Durand1/3 : introduction to CDI - Antoine Sabot-Durand
1/3 : introduction to CDI - Antoine Sabot-Durand
 
Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014
 
Using Contexts & Dependency Injection in the Java EE 6 Platform
Using Contexts & Dependency Injection in the Java EE 6 PlatformUsing Contexts & Dependency Injection in the Java EE 6 Platform
Using Contexts & Dependency Injection in the Java EE 6 Platform
 
CDI @javaonehyderabad
CDI @javaonehyderabadCDI @javaonehyderabad
CDI @javaonehyderabad
 
Spark IT 2011 - Context & Dependency Injection in the Java EE 6 Ecosystem
Spark IT 2011 - Context & Dependency Injection in the Java EE 6 EcosystemSpark IT 2011 - Context & Dependency Injection in the Java EE 6 Ecosystem
Spark IT 2011 - Context & Dependency Injection in the Java EE 6 Ecosystem
 
CDI in JEE6
CDI in JEE6CDI in JEE6
CDI in JEE6
 
S313937 cdi dochez
S313937 cdi dochezS313937 cdi dochez
S313937 cdi dochez
 
Devoxx 2012 (v2)
Devoxx 2012 (v2)Devoxx 2012 (v2)
Devoxx 2012 (v2)
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Extending Java EE with CDI and JBoss Forge
Extending Java EE with CDI and JBoss ForgeExtending Java EE with CDI and JBoss Forge
Extending Java EE with CDI and JBoss Forge
 
softshake 2014 - Java EE
softshake 2014 - Java EEsoftshake 2014 - Java EE
softshake 2014 - Java EE
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFX
 
Java onguice20070426
Java onguice20070426Java onguice20070426
Java onguice20070426
 
Introduction To Web Beans
Introduction To Web BeansIntroduction To Web Beans
Introduction To Web Beans
 
Spring andspringboot training
Spring andspringboot trainingSpring andspringboot training
Spring andspringboot training
 
Developing Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJSDeveloping Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJS
 
EJB et WS (Montreal JUG - 12 mai 2011)
EJB et WS (Montreal JUG - 12 mai 2011)EJB et WS (Montreal JUG - 12 mai 2011)
EJB et WS (Montreal JUG - 12 mai 2011)
 
CDI 1.1 university
CDI 1.1 universityCDI 1.1 university
CDI 1.1 university
 
Diving into VS 2015 Day3
Diving into VS 2015 Day3Diving into VS 2015 Day3
Diving into VS 2015 Day3
 
Guice gin
Guice ginGuice gin
Guice gin
 

More from Jim Bethancourt

Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in JavaJim Bethancourt
 
Migrating to Maven 2 Demystified
Migrating to Maven 2 DemystifiedMigrating to Maven 2 Demystified
Migrating to Maven 2 DemystifiedJim Bethancourt
 
Hearthstone To The Limit
Hearthstone To The LimitHearthstone To The Limit
Hearthstone To The LimitJim Bethancourt
 
Recognize, assess, reduce, and manage technical debt
Recognize, assess, reduce, and manage technical debtRecognize, assess, reduce, and manage technical debt
Recognize, assess, reduce, and manage technical debtJim Bethancourt
 
Atlassian Bamboo Feature Overview
Atlassian Bamboo Feature OverviewAtlassian Bamboo Feature Overview
Atlassian Bamboo Feature OverviewJim Bethancourt
 

More from Jim Bethancourt (13)

JavaOne 2011 Recap
JavaOne 2011 RecapJavaOne 2011 Recap
JavaOne 2011 Recap
 
Ready, Set, Refactor
Ready, Set, RefactorReady, Set, Refactor
Ready, Set, Refactor
 
Java 8 Feature Preview
Java 8 Feature PreviewJava 8 Feature Preview
Java 8 Feature Preview
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 
Young Java Champions
Young Java ChampionsYoung Java Champions
Young Java Champions
 
Migrating to Maven 2 Demystified
Migrating to Maven 2 DemystifiedMigrating to Maven 2 Demystified
Migrating to Maven 2 Demystified
 
User Group Leader Lunch
User Group Leader LunchUser Group Leader Lunch
User Group Leader Lunch
 
Hearthstone To The Limit
Hearthstone To The LimitHearthstone To The Limit
Hearthstone To The Limit
 
Recognize, assess, reduce, and manage technical debt
Recognize, assess, reduce, and manage technical debtRecognize, assess, reduce, and manage technical debt
Recognize, assess, reduce, and manage technical debt
 
Atlassian Bamboo Feature Overview
Atlassian Bamboo Feature OverviewAtlassian Bamboo Feature Overview
Atlassian Bamboo Feature Overview
 
Java Performance Tweaks
Java Performance TweaksJava Performance Tweaks
Java Performance Tweaks
 
Refactor to the Limit!
Refactor to the Limit!Refactor to the Limit!
Refactor to the Limit!
 
Lambdas and Laughs
Lambdas and LaughsLambdas and Laughs
Lambdas and Laughs
 

Recently uploaded

Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 

Recently uploaded (20)

Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 

Introduction to CDI

  • 2. 1 1 Introduction to CDI 2 Basic Dependency Injection 3 Using Qualifiers to Distinguish Beans of the Same Type 4 Contexts 5 Producers and Disposers 6 Events 7 Interceptors 8 Decorators 9 CDI in Java SE 10 Using CDI and JPA Together Agenda
  • 3. 2 Introduction to CDI • Java EE dependency injection standard • Can also be used in Java SE environments with some help • Strongly typed and type safe • Provides context management • Activated by default since CDI 1.1 • Use beans.xml to activate alternative beans, etc. • Highly Extensible via CDI SPI (not covered here) What is CDI?
  • 4. 3 Introduction to CDI • Managed Beans are basic components • Managed by the container • Have a lifecycle • Can be intercepted (technical concerns) and decorated (business concerns) • Can be injected • Are accessible outside CDI code CDI Beans
  • 5. 4 Agenda 1 Introduction to CDI 2 Basic Dependency Injection 3 Using Qualifiers to Distinguish Beans of the Same Type 4 Contexts 5 Producers and Disposers 6 Events 7 Interceptors 8 Decorators 9 CDI in Java SE 10 Using CDI and JPA Together
  • 6. 5 Basic Dependency Injection public class HowdyService { public String howdy() { return "Howdy!"; } } This is a bean
  • 7. 6 Basic Dependency Injection public class MyBean { private HowdyService service; @Inject public MyBean(HowdyService service) { this.service = service; } } Dependency Injection in Constructors
  • 8. 7 Basic Dependency Injection public class MyBean { private HowdyService service; @Inject public void setService(HowdyService service) { this.service = service; } } Dependency Injection in Setter
  • 9. 8 Basic Dependency Injection public class MyBean { @Inject private HowdyService service; public void sayHowdy() { display(service.howdy()); } } Dependency Injection in Fields
  • 10. 9 Basic Dependency Injection This Works! public class MyBean { @Inject Service<User> userService; @Inject Service<Staff> staffService; } No Type Erasure in CDI
  • 11. 10 1 Introduction to CDI 2 Basic Dependency Injection 3 Using Qualifiers to Distinguish Beans of the Same Type 4 Contexts 5 Producers and Disposers 6 Events 7 Interceptors 8 Decorators 9 CDI in Java SE 10 Using CDI and JPA Together
  • 12. 11 Using Qualifiers to Distinguish Beans of the Same Type public interface HelloService { public String hello(); } public class TexanHelloService implements HelloService { public String hello() { return "Howdy!"; } } public class AustralianHelloService implements HelloService { public String hello() { return "G’day!"; } } Multiple Service Implementations
  • 13. 12 Using Qualifiers to Distinguish Beans of the Same Type @Qualifier @Retention(RUNTIME) @Target({FIELD, TYPE, METHOD, PARAMETER}) public @interface Texan {} @Qualifier @Retention(RUNTIME) @Target({FIELD, TYPE, METHOD, PARAMETER}) public @interface Australian {} We Create Qualifiers
  • 14. 13 Using Qualifiers to Distinguish Beans of the Same Type @Texan public class TexanHelloService implements HelloService { public String hello() { return "Howdy!"; } } @Australian public class AussieHelloService implements HelloService { public String hello() { return "G’day!"; } } And Add Them to Our Implementations
  • 15. 14 Using Qualifiers to Distinguish Beans of the Same Type public class MyTexanBean { @Inject @Texan HelloService service; public void displayHello() { display(service.hello()); } } public class MyAussieBean { @Inject @Australian HelloService service; public void displayHello() { display(service.hello()); } } And Specify at our Injection Site
  • 16. 15 Using Qualifiers to Distinguish Beans of the Same Type @Qualifier @Retention(RUNTIME) @Target({FIELD, TYPE, METHOD, PARAMETER}) public @interface Language { public enum Languages { TEXAN, AUSTRALIAN } Languages value(); @Nonbinding String description() default ""; } Qualifiers can have Members
  • 17. 16 Using Qualifiers to Distinguish Beans of the Same Type @Language(TEXAN) public class TexanHelloService implements HelloService { public String hello() { return "Howdy!"; } } @Language(AUSTRALIAN) public class AussieHelloService implements HelloService { public String hello() { return "G’day!"; } } Class definition for Qualifiers with Members
  • 18. 17 Using Qualifiers to Distinguish Beans of the Same Type public class MyTexanBean { @Inject @Language(TEXAN) HelloService service; public void displayHello() { display(service.hello()); } } public class MyAussieBean { @Inject @Language(AUSTRALIAN) HelloService service; public void displayHello() { display(service.hello()); } } Injection using Qualifiers with Members
  • 19. 18 Using Qualifiers to Distinguish Beans of the Same Type public class MyTexanBean { @Inject @Texan HelloService service; public void displayHello() { display(service.hello()); } } @Texan @Console @Secured public class TexanHelloService implements HelloService { public String hello() { return "Howdy!"; } } Multiple Qualifiers
  • 20. 19 Using Qualifiers to Distinguish Beans of the Same Type public class MyTexanBean { @Inject @Texan @Console HelloService service; public void displayHello() { display(service.hello()); } } @Texan @Console @Secured public class TexanHelloService implements HelloService { public String hello() { return "Howdy!"; } } Multiple Qualifiers
  • 21. 20 Using Qualifiers to Distinguish Beans of the Same Type public class MyTexanBean { @Inject @Texan @Console @Secured HelloService service; public void displayHello() { display(service.hello()); } } @Texan @Console @Secured public class TexanHelloService implements HelloService { public String hello() { return "Howdy!"; } } Multiple Qualifiers
  • 22. 21 Using Qualifiers to Distinguish Beans of the Same Type public class MyTexanBean { @Inject @Texan @Console @Secured HelloService service; public void displayHello() { display(service.hello()); } } @Texan @Console public class TexanHelloService implements HelloService { public String hello() { return "Howdy!"; } } Multiple Qualifiers – Won’t work
  • 23. 22 Using Qualifiers to Distinguish Beans of the Same Type @Default @Any @Named Reserved Qualifiers
  • 24. 23 Using Qualifiers to Distinguish Beans of the Same Type public class MyBean { @Inject @Any Instance<HelloService> service; public void displayHello() { HelloService helloService = service.select(new AnnotationLiteral<Texan>(){}).get(); display(helloService.hello()); } } Programmatic Bean Lookup (a.k.a. Lazy Injection)
  • 25. 24 Using Qualifiers to Distinguish Beans of the Same Type @Texan @Vetoed public class TexanHelloService implements HelloService { public String hello() { return "Howdy!"; } } package-info.java class: @Vetoed package cdi_experiments; import javax.enterprise.inject.Vetoed; Ignoring Beans and Packages with @Vetoed
  • 26. 25 Using Qualifiers to Distinguish Beans of the Same Type @Texan @Alternative public class MyHowdyYallService implements HelloService { public String hello() { return "Howdy y'all!"; } } <beans …> <alternatives> <class>cdi_experiments.MyHowdyYallService</class> </alternatives> </beans> Specifying and using Alternative Beans
  • 27. 26 1 Introduction to CDI 2 Basic Dependency Injection 3 Using Qualifiers to Distinguish Beans of the Same Type 4 Contexts 5 Producers and Disposers 6 Events 7 Interceptors 8 Decorators 9 CDI in Java SE 10 Using CDI and JPA Together
  • 28. 27 Contexts Help the container to choose when a bean should be intstantiated and destroyed Enforce singleton pattern for a given context Built-in CDI contexts: • @Dependent (default) • @ApplicationScoped, @SessionScoped, @RequestSCoped • @ConversationScoped • @Singleton You can create your own scope Contexts Manage Bean Lifecycle
  • 29. 28 Contexts @SessionScoped public class CartBean { public void addItem(Item item) { ... } } A shopping cart is almost always tied to a user’s session, so using the @SessionScoped context makes the most sense. Choosing the Right Context
  • 30. 29 Contexts @ApplicationScoped public class CartBean { public void addItem(Item item) { ... } } BAD IDEA! Having a single cart used across all users doesn’t make any sense.
  • 31. 30 1 Introduction to CDI 2 Basic Dependency Injection 3 Using Qualifiers to Distinguish Beans of the Same Type 4 Contexts 5 Producers and Disposers 6 Events 7 Interceptors 8 Decorators 9 CDI in Java SE 10 Using CDI and JPA Together
  • 32. 31 Producers and Disposers @Produces public OldRanchHand oldRanchHandProducer() { return new OldRanchHand(); } … @Inject OldRanchHand ranchHand; The return type and the injection type are identical. CDI uses this type information to inject produced values. To inject different values into different injection points of the same type, use qualifiers Method producers can be used to generate complex objects Create a Bean from any Class
  • 33. 32 Producers and Disposers @Produces public Logger produceLog(InjectionPoint injectionPoint) { return Logger.getLogger(injectionPoint.getMember() .getDeclaringClass().getName()); } Getting Injection Point Information
  • 34. 33 Producers and Disposers @Produces public <K, V> Map<K, V> produceMap(InjectionPoint ip) { if (valueIsNumber(ip.getType())) { return new TreeMap<K, V>(); } return new HashMap<K, V>(); } No Type Erasure Enables Fancy Stuff
  • 35. 34 1 Introduction to CDI 2 Basic Dependency Injection 3 Using Qualifiers to Distinguish Beans of the Same Type 4 Contexts 5 Producers and Disposers 6 Events 7 Interceptors 8 Decorators 9 CDI in Java SE 10 Using CDI and JPA Together
  • 36. 35 Events public class MyTexanBean { @Inject Event<HelloService> helloEvent; @Inject @Texan HelloService service; public void displayHello() { display(service.hello()); } public void iSaidHowdy(HelloService helloService) { helloEvent.fire(helloService); } } public class HelloListenerBean { public void listenForHello(@Observes HelloService hello) { System.out.println(hello.hello() + " to you too!"); } }
  • 37. 36 Events public class HelloListenerBean { public void listenForHello(@Observes @Texan HelloService hello) { System.out.println(hello.hello() + " to you too!"); } } Qualifiers on Observers
  • 38. 37 Events The producer must wait for all observers to execute before continuing If any observer throws an exception, all remaining observer methods are not called and the exception is thrown by the firing observing method Observers have no priority in CDI 1.1, but is on the roadmap for 2.0 Some Caveats
  • 39. 38 1 Introduction to CDI 2 Basic Dependency Injection 3 Using Qualifiers to Distinguish Beans of the Same Type 4 Contexts 5 Producers and Disposers 6 Events 7 Interceptors 8 Decorators 9 CDI in Java SE 10 Using CDI and JPA Together
  • 40. 39 Interceptors Classes that provide technical cross-cutting functionality are interceptors Technical cross-cutting concerns include: • Transactions • Security • Logging Interceptors are not enabled by default, but can be enabled and ordered in beans.xml Interceptors can have multiple qualifiers Interceptors
  • 41. 40 Interceptors Interceptor bindings are qualifiers that instruct the CDI container to intercept a method or all methods on a class @InterceptorBinding @Target({METHOD, TYPE}) @Retention(RUNTIME) public @interface Loggable {} Qualified Interceptor Binding
  • 42. 41 Interceptors @Interceptor @Loggable @Priority(Interceptor.Priority.APPLICATION) public class LogInterceptor { @AroundInvoke public Object log(InvocationContext ic) throws Exception { System.out.println("Entering " + ic.getMethod().getName()); try { return ic.proceed(); } finally { System.out.println("Exiting " + ic.getMethod().getName()); } } } Interceptor Class
  • 43. 42 Interceptors @Loggable public class MyTexanBean { @Inject @Texan HelloService service; public void displayHello() { display(service.hello()); } } Intercepting a Class or Method
  • 44. 43 Interceptors @Interceptor @Loggable public class FriendlyCowboy { @PostConstruct public void smile(InvocationContext invocationContext) { //smile } @PreDestroy public void tipHat(InvocationContext invocationContext) { //tip hat } } Lifecycle Interceptors
  • 45. 44 1 Introduction to CDI 2 Basic Dependency Injection 3 Using Qualifiers to Distinguish Beans of the Same Type 4 Contexts 5 Producers and Disposers 6 Events 7 Interceptors 8 Decorators 9 CDI in Java SE 10 Using CDI and JPA Together
  • 46. 45 Decorators Classes that provide additional business functionality are decorators Interceptors are called before decorators Decorator classes must implement the same interface(s) as the classes that they are decorating Can be abstract if it only decorates some of the methods in the interface Enabled in the <decorators> section in the beans.xml file and can be enabled and ordered in the beans.xml and via the @Priority annotation Decorators
  • 47. 46 Decorators @Decorator public abstract class HelloDec implements HelloService { // The decorated service may be restricted with qualifiers @Inject @Delegate HelloService service; public String hello() { return service.hello() + "-decorated"; } } Short Example
  • 48. 47 1 Introduction to CDI 2 Basic Dependency Injection 3 Using Qualifiers to Distinguish Beans of the Same Type 4 Contexts 5 Producers and Disposers 6 Events 7 Interceptors 8 Decorators 9 CDI in Java SE 10 Using CDI and JPA Together
  • 49. 48 CDI in Java SE Currently possible through the DeltaSpike library Slated for standardization in CDI 2.0 public class MainApp { public static void main(String[] args) { CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer(); cdiContainer.boot(); // Starting the application-context enables use of @ApplicationScoped beans ContextControl contextControl = cdiContainer.getContextControl(); contextControl.startContext(ApplicationScoped.class); // You can use CDI here //... cdiContainer.shutdown(); } } CDI – Not Just for JEE!
  • 50. 49 1 Introduction to CDI 2 Basic Dependency Injection 3 Using Qualifiers to Distinguish Beans of the Same Type 4 Contexts 5 Producers and Disposers 6 Events 7 Interceptors 8 Decorators 9 CDI in Java SE 10 Using CDI and JPA Together
  • 51. 50 Using CDI and JPA Together CDI Beans and JPA Beans have different lifecycles As a result, CDI Beans can’t be injected directly into JPA Beans EntityListeners can be used to bridge CDI and JPA when needed CDI and JPA – Friends at Last
  • 52. 51 Using CDI and JPA Together @Entity @EntityListeners(PostListener.class) public class Post implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column(name = "CREATED_DATE") private Date created; @Column(name = "LAST_MODIFIED_DATE") private Date lastModified; public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public Date getLastModified() { return lastModified; } public void setLastModified(Date lastModified) { this.lastModified = lastModified;} } The JPA Class
  • 53. 52 Using CDI and JPA Together public class PostListener { @Inject Logger log; @PrePersist public void prePresist(Object o) { log.info("call prePresist"); if (o instanceof Post) { Post post = (Post) o; final Date created = new Date(); post.setCreated(created); post.setLastModified(created); } } @PreUpdate public void preUpdate(Object o) { log.info("call preUpdate"); if (o instanceof Post) { Post post = (Post) o; post.setLastModified(new Date()); } } }

Editor's Notes

  1. Two injection points with different parameterized types. CDI can differentiate between these two types, without the need to introduce extra code. But what if we have two implementations of the same interface?
  2. Here we have two implementations of the HelloService
  3. CDI can now determine which implementation you want to use
  4. Ideally, you’ll want to have Boolean and Enum members to keep things as type-safe and controlled as possible. The @Nonbinding annotation indicates that the description value will not be considered for injection target matching
  5. The container will find the HelloService qualified with the Texan qualifier
  6. The container will find the HelloService qualified with both with the Texan and Console qualifiers
  7. The container will find the HelloService qualified with both with the Texan, Console, and Secured qualifiers
  8. However, the container will not find the HelloService qualified with the Texan, Console, and Secured qualifiers since there is no bean with all qualifier types – we only have Texan and Console qualifiers on the TexanHelloService
  9. @Default is the qualifier that is applied to all beans that don’t have qualifiers @Any – used in programmatic lookup @Named – exception in strong typing – used to give a name to a bean to access bean via JSF view or JSP page
  10. The Any qualifer says to inject an instance of HelloService regardless of their qualifier. Here we’re selecting the Texan service through the use of the AnnotationLiteral abstract class, which supports inline instantiation of annotation type instances.
  11. In this case, the TexanHelloService class will be ignored by the CDI container since it is marked as @Vetoed To veto an entire package, mark the package as @Vetoed in the package-info.java class
  12. Instead of saying Howdy! all injected Texan beans will say Howdy y’all since it is activated in the alternatives section of the beans.xml Useful both in testing to inject test beans to isolate behavior Useful in production environments to enable different behavior depending on requirements
  13. Turns a POJO into a CDI bean and alllows 3rd party frameworks to be used by CDI by exposing their objects as CDI beans Producers allow additional control over object creation You can also have @Alternative producers
  14. The InjectionPoint class allows for programmatic retrieval of some information about the injection site class.
  15. Since CDI doesn’t erase type information, we can use generic type information in our code
  16. Events allow for the decoupling of producers and consumers Require no compile-time dependencies
  17. Events can be qualified to only listen for events from specific beans
  18. Interceptors can have multiple qualifiers or can use qualifiers with members to allow the CDI container to differentiate interceptors Interceptors can be @Vetoed
  19. Interceptor classes must be annotated / qualified with the @Interceptor annotation and at least one other qualifier The intercepting method must be annotated with @AroundInvoke Interceptor methods must take an InvocationContext type as a parameter and return Object and call the proceed method once it’s done. Wrap the proceed() method call in a try/finally block to execute code if the intercepted method needs to have additional code executed after it completes / exits Interceptors are executed in the order they are defined in beans.xml, but only in that archive Interceptors can be ordered for an entire application using the @Priority annotation -- smaller priority called first.  Defined in the qualifier / annotation class
  20. Interceptor annotations can be declared on individual methods, or they can be placed on all public methods by annotating the class with the interceptor
  21. Annotate the method on the CDI bean you want executed at the appropriate point in the life cycle Lifecycle @Interceptor beans can be defined and can be applied to multiple CDI managed beans.   The methods annotated with @PostConstruct and @PreDestroy must take an InvocationContext as a parameter and return void
  22. Starting the container does not automatically start all CDI Contexts. Contexts must be started independently using DeltaSpike’s provided ContextControl class.
  23. Bare bones blog post JPA example From http://hantsy.blogspot.com/2013/12/jpa-21-cdi-support.html
  24. Logger is injected and invoked on JPA lifecycle event occurrences