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.
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. 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. JSR-330 annotations
• @Inject
• @Qualifier
• @Named
–String based qualifier
• @Scope
• @Singleton
–singleton scope
• Provider<T>
–provides instances of T
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. 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. 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
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
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
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
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
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. 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. More about this
• January 11: Season Class
• Bert Ertman & Paul Bakker
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