Java EE 6 introduced the new Context & Dependency Injection (CDI) specification and for the first time it is now possible and practical to build a full app just using the Java EE specification - all the pieces for an application is available out of the box on a Java EE 6 certified container.
This talk will briefly introduce you to the concepts of CDI and then via code examples show how CDI and Java EE 6 have been used to implement key aspects of the application.
See Context & Dependency Injection from Java EE 6 in Action
1.
2. Context & Dependency Injection
from Java EE 6
in Action
Max Rydahl Andersen
Red Hat
http://about.me/maxandersen
14th April
Miracle Open World 2011
3. About Me
• Max Rydahl Andersen
• Lead of JBoss Tools & Developer Studio
• Committer on Hibernate Core, Seam & Weld
• Co-host of JBoss Community Asylum Podcast
• @maxandersen on Twitter
• http://about.me/maxandersen
9. What is new in Java EE 6 ?
• Forget everything bad you have seen or heard
about Java EE, because...
• ...it is highly simplified
• ...it’s actually usable now
• ...it Just Works!
10. Java EE 6: Goals
•Extensibility
•Allow more components to be standalone (EJB 3.1)
•Profiles
•Subsets of “full” EE platform
•Web Profile
•Pruning
•CMP, JAX-RPC, JAXR, JSR-88 are “pruned” in EE6
•Technology Improvements
8 Pete Muir
11. Java EE 6: Newcomers
•Managed Beans (part of JSR-316)
•Contexts and Dependency Injection - JSR-299
•Bean Validation - JSR-303
•JAX-RS (RESTful Web Services) - JSR-311
9 Pete Muir
15. What is CDI ?
• “...unify the JSF managed bean component
model with the EJB component model,
resulting in a significantly simplified
programming model for web-based
applications.” - JSR-299/Gavin King
• “CDI simplifies and sanitizes the API for DI
and AOP like JPA did for ORM” - CDI
Tutorial/Rick Hightower
16. CDI Acronyms
• The Spec - Context & Dependency Injection
for JEE (CDI)
• The Reference Implementation - Weld
• Other implementations - CanDI (Resin),
Open WebBeans (Apache)
17. The Simplest CDI Bean
public class Welcome {
public String buildPhrase(String city) {
return "Hello from " + city + "!";
}
}
18. The Simplest Session Bean
@Stateless public class Welcome {
public String buildPhrase(String city) {
return "Hello from " + city + "!";
}
}
22. Dependency Injection
public class Greeter {
@Inject
Welcome w;
public void welcome() {
System.out.println(w.buildPhrase("Billund"));
}
}
23. Another Implementation
public class TranslatingWelcome extends
Welcome {
@Inject GoogleTranslator translator;
public String buildPhrase(String city) {
return translator.translate(
"Welcome to " + city + "!");
}
}
24. Quiz: What gets Injected ?
public class Greeter {
@Inject
Welcome w; // Welcome or TranslatingWelcome ?
public void welcome() {
System.out.println(w.buildPhrase("Billund"));
}
}
25. Qualifiers to the rescue
@Qualifier
@Target({ TYPE, METHOD,
PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
public @interface Translating {
}
26. Qualifiers to the rescue
@Translating
public class TranslatingWelcome extends Welcome {
@Inject GoogleTranslator translator;
public String buildPhrase(String city) {
return translator.translate("Welcome to " + city + "!");
}
}
27. Qualifiers to the rescue
@Translating
public class TranslatingWelcome extends Welcome {
@Inject GoogleTranslator translator;
public String buildPhrase(String city) {
return translator.translate("Welcome to " + city + "!");
}
}
28. Qualifiers to the rescue
@Translating
public class TranslatingWelcome extends Welcome {
@Inject GoogleTranslator translator;
public String buildPhrase(String city) {
return translator.translate("Welcome to " + city + "!");
}
public class Greeter {
}
@Inject @Translating
Welcome w; // No ambigiuty!
public void welcome() {
System.out.println(w.buildPhrase("Billund"));
}
}
30. Scopes
• JEE 6 provides a set of Scopes
• @Dependent, @ApplicationScoped,
@RequestScoped, @SessionScoped,
@ConversationScoped, <put your own here>
• Each have a context which are managed by
the container (you don’t have to do explicit
cleanup)
31. More CDI
• Event/Observers
• Stereo Types
• Alternatives
• API for Framework Implementors
32. Deep-dive Example
• PasteCode - part of the Weld Examples
• Uses just Servlet, CDI, JSF & EJB’s
• JEE 6 Web Profile
33. Challenges
• Persistence
• Validation of data from client to database
• Configuration of Servlet’s, EJB’s etc.
• Access to Beans from UI-Layer (EJB->JSF)
• Dependency Injection for decoupling concerns
• Setup things at Application Startup
• Cross-cutting custom business concerns (example Flood Control)
• Sending and receiving events
• Batch job’s
• ...and more
34. CDI Bean In Action
public class PasteWindow
{
...
private CodeFragmentManager
codeFragmentManager;t
public CodeFragment getCodeFragment()
{
return codeFragment;
}
…
35. CDI Bean In Action
@Named
public class PasteWindow
{
...
private CodeFragmentManager
codeFragmentManager;t
public CodeFragment getCodeFragment()
{
return codeFragment;
}
…
36. CDI Bean In Action
@Named
@RequestScoped
public class PasteWindow
{
...
private CodeFragmentManager
codeFragmentManager;t
public CodeFragment getCodeFragment()
{
return codeFragment;
}
…
37. CDI Bean In Action
@Named
@RequestScoped
public class PasteWindow
{
...
@Inject private CodeFragmentManager
codeFragmentManager;t
public CodeFragment getCodeFragment()
{
return codeFragment;
}
…
40. Sending and receiving events
@Inject
private Event<CodeFragment> event;
...
event.fire(code);
public void
addEntry(@Observes CodeFragment codeFragment)
{
this.log.add(codeFragment);
}
41. Cross-Cutting Business
@Decorator w/Decoratorimplements
public abstract class FloodingDecorator
CodeFragmentManager, Serializable
{
@Inject @Delegate
private CodeFragmentManage codeFragmentManager;
public String addCodeFragment(CodeFragment code,
boolean privateFragment)
{
...
return
codeFragmentManager.addCodeFragment(code,
privateFragment);
42. Summary
• JSR-299 provides a set of services for Java EE
• Bridges JSF and EJB
• Offers loose coupling with strong typing
• Extensive SPI for third-party integration with
Java EE
• Weld: JSR-299 Reference Implementation
• Seam 3: Portable extensions for Java EE