context and dependency injection
DESCRIPTION
A Cocktail of Guice and Seam, the missing ingredients for Java EE 6TRANSCRIPT
![Page 1: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/1.jpg)
Context & Dependency Injection
A Cocktail of Guice and Seam, the missing ingredients for Java EE 6
Werner Keil
21/04/2011
![Page 2: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/2.jpg)
Without Dependency Injection
2
Explicit Constructor
class Stopwatch { final TimeSource timeSource; Stopwatch () { timeSource = new AtomicClock(...); } void start() { ... } long stop() { ... }}
www.catmedia.us
![Page 3: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/3.jpg)
Without Dependency Injection
3
Using a Factory
class Stopwatch {
final TimeSource timeSource; Stopwatch () {
timeSource = DefaultTimeSource.getInstance();
}
void start() { ... }
long stop() { ... }
}
www.catmedia.us
Somewhat like in java.util.Calendar
![Page 4: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/4.jpg)
Without Dependency Injection
4
Mock Data
void testStopwatch() { TimeSource original = DTS.getInstance(); DefaultTimeSource.setInstance(new MockTimeSource());
try {Stopwatch sw = new Stopwatch();
... } finally { DTS.setInstance(original); } }
www.catmedia.us
![Page 5: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/5.jpg)
With Dependency Injection
5
@Inject
class Stopwatch { final TimeSource timeSource; @Inject Stopwatch(TimeSource timeSource) {
this.timeSource = TimeSource; } void start() { ... } long stop() { ... } }
www.catmedia.us
![Page 6: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/6.jpg)
JSR-330
@Inject Identifies injectable constructors, methods, and fields .
This works for both static and non-static elements. @Qualifier
Identifies qualifier annotations to declare binding types of your API
@Named A name (string) based qualifier Others may be declared application - or domain-specific
www.catmedia.us 6
Definition
![Page 7: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/7.jpg)
JSR-330
@Provider Provides instances of an (injectable) type. Allows
Retrieving multiple instances. Lazy or optional retrieval of an instance. Breaking circular dependencies. Abstracting scope
@Singleton Identifies a type that the injector only instantiates once. See the well-known Design Pattern
www.catmedia.us 7
Implementation
![Page 8: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/8.jpg)
DEMO
www.catmedia.us 8
![Page 9: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/9.jpg)
JSR-330
1. Minimal but usable API: A small API surface seems more important than making it as convenient as possible for clients. There are only two methods in the proposed API that could be considered convenience, added because they seemed to pull their weight
InjectionConfigurer.inject and InjectionSpec.inject(Object)
2. Builder-style API: so-called "fluent APIs“• Open to other styles.
www.catmedia.us 9
Configuration
![Page 10: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/10.jpg)
JSR-330
3. Abstract classes instead of interfaces: 2 main types (InjectionConfigurer and InjectionSpec) are abstract classes
instead of interfaces. This allows new methods to be added later in a binary-
compatible way.
4. Separate configuration API• The API does not specify how an instance of
InjectionConfigurer is obtained. • Otherwise we have to standardize the injector API itself,
something outside of the stated scope of JSR-330.www.catmedia.us 10
Configuration (2)
![Page 11: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/11.jpg)
JSR-330
5. Names are different from Guice's configuration API: This is mostly
to keep this separate from existing configuration APIs, but also so
that new concepts (like "Binding") don't have to be defined.
www.catmedia.us 11
Configuration (3)
![Page 12: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/12.jpg)
More about JSR-330, seehttp://code.google.com/p/atinject
orhttp://jcp.org/en/jsr/summary?id=330
www.catmedia.us 12
![Page 13: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/13.jpg)
www.catmedia.us
Formerly known as WebBeans…
13
![Page 14: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/14.jpg)
JSR-299
The lifecycle and interactions of stateful components bound to well-defined lifecycle contexts, where the set of contexts is extensible
A sophisticated, type safe dependency injection mechanism, including a facility for choosing between various components that implement the same Java interface at deployment time
An event notification model
www.catmedia.us 14
Services
![Page 15: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/15.jpg)
JSR-299
Integration with the Unified Expression Language (EL), allowing any component to be used directly within a JSF or JSP page
The ability to decorate injected components A web conversation context in addition to the three
standard web contexts defined by the Java Servlets specification
An SPI allowing portable extensions to integrate cleanly with the Java EE environment
www.catmedia.us 15
Services (2)
![Page 16: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/16.jpg)
• Sophisticated,• Type Safe,• Dependency
Injection,…
www.catmedia.us 16
![Page 17: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/17.jpg)
IN MEMORIAM
17
Pietro Ferrero Jr.
www.catmedia.us
11 September 1963 – 18 April 2011
![Page 18: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/18.jpg)
JSR-299
A (nonempty) set of bean types A (nonempty) set of bindings A scope A deployment type Optionally, a bean EL name A set of interceptor bindings A bean implementation
www.catmedia.us 18
Bean Attributes
![Page 19: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/19.jpg)
JSR-299
• A custom implementation of Context may be associated with a scope type by calling BeanManager.addContext().
public void addContext(Context context);
www.catmedia.us 19
Context
![Page 20: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/20.jpg)
JSR-299
• BeanManager.getContext() retrieves an active context object associated with the a given scope:
public Context getContext(Class<? extends Annotation> scopeType);
→ Context used in a broader meaning than some other parts of Java (Enterprise)
www.catmedia.us 20
Context (2)
![Page 21: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/21.jpg)
JSR-299
@SessionScoped
public class PaymentStrategyProducer {
private PaymentStrategyType paymentStrategyType;
public void setPaymentStrategyType(
PaymentStrategyType type) {
paymentStrategyType = type;
}
www.catmedia.us 21
Restricted Instantiation
![Page 22: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/22.jpg)
JSR-299
@Produces PaymentStrategy getPaymentStrategy(@CreditCard PaymentStrategy creditCard,
@Cheque PaymentStrategy cheque,
@Online PaymentStrategy online) {
switch (paymentStrategyType) {
case CREDIT_CARD: return creditCard;
case CHEQUE: return cheque;
[…]www.catmedia.us 22
Restricted Instantiation (2)
![Page 23: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/23.jpg)
JSR-299
[…]
case ONLINE: return online;
default: throw new IllegalStateException();
} }
}
www.catmedia.us 23
Restricted Instantiation (3)
![Page 24: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/24.jpg)
JSR-299
• Then the following observer method will always be notified of the event:
• public void afterLogin(@Observes LoggedInEvent event) { ... }
• Whereas this observer method may or may not be notified, depending upon the value of user.getRole():
• public void afterAdminLogin(@Observes @Role("admin") LoggedInEvent event) { ... }
www.catmedia.us 24
Event Observer
![Page 25: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/25.jpg)
JSR-299
• As elsewhere, binding types may have annotation members:
@BindingType@Target(PARAMETER)@Retention(RUNTIME)public @interface Role {String value();
}
www.catmedia.us 25
Event Binding
![Page 26: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/26.jpg)
JSR-299
Actually @Role could extend @Named from JSR-330 JSRs here potentially not consequently streamlined…?
www.catmedia.us 26
Event Binding (2)
![Page 27: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/27.jpg)
JSR-299
public interface Bean<T>extends Contextual<T> {public Set<Type> getTypes();public Set<Annotation> getBindings();public Class<? extends Annotation>
getScopeType();
www.catmedia.us 27
Portable Extensions
![Page 28: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/28.jpg)
JSR-299
public Class<? extends Annotation>
getDeploymentType();public String getName();public Class<?> getBeanClass();public boolean isNullable();
public Set<InjectionPoint> getInjectionPoints();
}www.catmedia.us 28
Portable Extensions (2)
![Page 29: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/29.jpg)
DEMO
www.catmedia.us 31
![Page 30: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/30.jpg)
More about JSR-299, see
http://www.seamframework.org/JCP
http://jcp.org/en/jsr/summary?id=299or
• http://www.developermarch.com/developersummit/sessions.html#session66
• (cancelled ;-/)www.catmedia.us 32
![Page 31: Context and Dependency Injection](https://reader035.vdocuments.us/reader035/viewer/2022081414/54bd4cff4a795953058b45a9/html5/thumbnails/31.jpg)
Further Reading• Java.net
http://www.java.net/• Java™ EE Patterns and Best Practices
http://kenai.com/projects/javaee-patterns /
www.catmedia.us 33