The future of enterprise dependency injection: Contexts & Dependency Injection for Java EE (JSR-299)

2,536 views
2,391 views

Published on

Talk about the Contexts & Dependency Injection specification introduced in Java EE 6. It's a basic introduction to the most important concepts of the specification.
The slide were made for a 50 minute talk at the NL-JUG conference in November 2009.

Published in: Technology
1 Comment
6 Likes
Statistics
Notes
No Downloads
Views
Total views
2,536
On SlideShare
0
From Embeds
0
Number of Embeds
33
Actions
Shares
0
Downloads
204
Comments
1
Likes
6
Embeds 0
No embeds

No notes for slide
  • The future of enterprise dependency injection: Contexts & Dependency Injection for Java EE (JSR-299)

    1. 1. The future of Enterprise Dependency Injection Paul Bakker
    2. 2. Outline • The history of JSR-299 • JSR-299 dependency injection • Contextual components • Conversations • Producers
    3. 3. About me Paul Bakker • Trainer Info Support Hibernat EJB 3 e • NLJUG speaker JSF Flex • Java Magazine author Seam Groovy JavaFX Grails Spring
    4. 4. History WebBeans
    5. 5. History Contexts & Dependency Injection WebBeans for the Java EE platform
    6. 6. History Contexts & Dependency Injection WebBeans for the Java EE platform JSR-330 JSR-299 Dependency Uses Contexts & Dependency Injection for Injection for the Java EE Java platform Java SE Java EE
    7. 7. JSR-330 • Standardized dependency injection for Java SE –maximize reusability, testability and maintainability • Bob Lee and Rod Johnson • Specifies annotations and use of annotations • Does NOT specify a container • JSR-299 uses JSR-330 annotations
    8. 8. JSR-330 annotations • @Inject • @Qualifier • @Named –String based qualifier • @Scope • @Singleton –singleton scope • Provider<T> –provides instances of T
    9. 9. JSR-299 • Type safe Dependency Injection for Java EE components • Lifecycle aware –EE components often have a scope/context –i.e. request scoped JSF managed bean • Integration with the Unified Expression Language • Additional services –i.e. event notification, decoration and interceptors
    10. 10. Implementations • Reference Implementation: –WebBeans • First alternative implementation –Apache OpenWebBeans –ASL-Licenced –aims to provide a new set of WebBeans components of other Apache projects
    11. 11. Implementations • Reference Implementation: –WebBeans Weld • First alternative implementation –Apache OpenWebBeans –ASL-Licenced –aims to provide a new set of WebBeans components of other Apache projects
    12. 12. Exposes bean to EL
    13. 13. Bean valid for this request
    14. 14. Inject instance of Greeter An instance will be created by the container, and bound to request scope
    15. 15. The Greeter class • The Greeter is a simple POJO
    16. 16. The Greeter class • But could have been a EJB Session Bean...
    17. 17. Types of injection Field injection Constructor injection Setter injection
    18. 18. Types of injection Field injection Constructor injection Setter injection
    19. 19. Where can I use CDI? • Inject into: –every POJO –every EJB Session Bean –every Servlet • Injectable types: –every POJO –every EJB Session Bean –JEE Resources (e.g. a DataSource)
    20. 20. Stereotypes • Declare common meta data in a new annotation –@Model is a standard stereotype
    21. 21. Qualifiers • An interface could have multiple implementations –the container must choose an implementation • Add a qualifier (another annotation) to each implementation –the container chooses the correct implementation
    22. 22. Qualifiers
    23. 23. Qualifiers Self written qualifier annotation Self written qualifier annotation
    24. 24. Qualifiers
    25. 25. Qualifiers No compile-time coupling
    26. 26. Scopes • In a web application, an object always has a scope (e.g. Request or Session scope) • CDI is context/scope aware –injection is scope sensitive –instances may be shared by injection points –container cleans up • Scopes: Request, Session, Conversation, Application • Default scope: @Dependent –never shared, bound to lifecycle of injection point
    27. 27. Default Scope @Dependent @Dependent MyClass MyClass @Inject @Inject Request Request scoped scoped bean ‘A’ bean ‘B’ request
    28. 28. Request Scope @RequestScoped @RequestScoped MyClass MyClass @Inject @Inject @Inject Request Request Request scoped scoped scoped bean ‘A’ bean ‘B’ bean ‘A’ request 1 request 2
    29. 29. Session Scope @SessionScoped MyClass @Inject @Inject @Inject Request Request Request scoped scoped scoped bean ‘A’ bean ‘B’ bean ‘A’ request 1 request 2
    30. 30. Conversation scope • Multi-request scope within a session • Isolated from other conversations • Defined begin and end point browser tab 2 find room book room step 2 confirm browser tab 1 find room book room step 2 confirm Login Logout
    31. 31. Conversations example Conversational bean Upgrade to long-running Schedule end of conversation at end of request
    32. 32. Producer methods • A source of injectable objects –objects are not required to be beans (e.g. a List) –concrete type varies at runtime –objects require extra initialization • Most similar to a traditional Factory Method –but more loosely coupled
    33. 33. Producer methods
    34. 34. Producer methods
    35. 35. Producer methods Exposed to EL
    36. 36. Producer methods Put in Application Scope
    37. 37. Producer methods Custom qualifier
    38. 38. Using CDI • CDI is part of JEE 6 –Every JEE 6 app server must be CDI compatible –GlassFish V3 and JBoss 5.2 bundled with Weld • Weld can be installed on Tomcat or Jetty
    39. 39. The verdict Pros • Finally a decent DI framework • Makes extreme loose coupling possible Cons • Can code be too decoupled? • Where did my instance come from?
    40. 40. More about this • January 11: Season Class • Bert Ertman & Paul Bakker
    41. 41. More about this • January 11: Season Class –JSF 2.0 –Servlet 3.0 –EJB 3.1 (lite) –JPA 2.0 –Contexts & DI –JAX-RS –JavaFX

    ×