Day 1 Track 1 Session 3
Day 1 Track 1 Session 3
Robb Greathouse
([email protected])
December 2012
Building on Common Ground!
Pete Muir
Why Should You Be Interested?!
• ISV –"
•Extends the reach of your product. "
• Batch can become Realtime"
• Customers can inject your functionality into their custom apps"
•Makes your product easier to use"
•SI or Inhouse"
•Makes code reuse much easier"
•Easier integration much easier"
Contexts and Dependency Injection for the Java EE platform!
Pete Muir
"What makes CDI
different?"
standardized
type-safe
extensible
Pete Muir
The crash course,
please
Pete Muir
What are the traits of a! BEAN?
Pete Muir
Bean implementation
public class StatusUpdater { class
}
Pete Muir
Bean types
public class GoogleUrlShortener
implements UrlShortener {
}
Pete Muir
Qualifiers
@Google
public class GoogleUrlShortener
implements UrlShortener {
}
Pete Muir
Alternative
@Alternative
public class DataLoader {
Classification
}
Pete Muir
Scope
@RequestScoped
public class StatusUpdater {
}
Pete Muir
Interceptor binding
@Auditable
public class StatusDao {
}
Pete Muir
Bean name
@Named
public class Registration {
}
Pete Muir
"When is a bean !
recognized?"!
Pete Muir
Bean archive (jar) - META-INF/beans.xml!
Pete Muir
beans.xml
can be
empty!!
Pete Muir
I nje ctio
n Ba s i c
s
public class StatusUpdater {!
@Inject!
private UrlShortener shortener;!
!
public void post(String username, String status) {!
String sStatus = shortener.shortenUrls(status);!
System.out.println(username + " said " + sStatus);!
}!
}!
Pete Muir
"How are injections
resolved?"!
Use the !
source!!
Pete Muir
Type!
Pete Muir
Type!
Parameters!
Pete Muir
Annotations!
Pete Muir
Method!
signatures!
Pete Muir
bean producers"
bean alternatives"
CDI also provides
interceptor bindings"
bean decorators"
Unified EL integration"
Pete Muir
Pete Muir
SPI for hacking Java EE:!
register additional
beans
Register extension
Pete Muir
CDI deployment lifecycle with
possible activities"
Pete Muir
Deploy Before Bean Scan
Application
Discovery Archives
+ add scope
+ add qualifier
+ add stereotype
Pete Muir
Deploy Process Process
Before Bean Scan (Annotated) Injection
Application Discovery Archives Types Targets
Pete Muir
Process
Process
Scan (Annotated) Injection
Archives
Types Targets
Pete Muir
Process Process Process
(Annotated) Injection Process Process Observer
Types Targets Beans Producers Methods
Pete Muir
Process Process Injection Targets Process
(Annotated)
Beans
Types
Pete Muir
Process Process Process
(Annotated) Injection Process Process Observe
Types Targets Beans Producers Methods
Pete Muir
Process Process Process
Injection
Targets Beans Producers
Pete Muir
Process Process
Injection Process Process Observer After B
Targets Beans Producers Methods Discov
Pete Muir
Process
Process Observer
After Bean
Producers Methods Discovery
+ add bean
+ add context
Pete Muir
After
Deployment
ean Application Before
ery Validation Running Shutdown
+ assessment
+ cleanup
Pete Muir
After
Deployment Application Before Undeploy
Validation Running Shutdown Application
Pete Muir
When is an extension !
recognized?!
Pete Muir
META-INF/services/javax.enterprise.inject.spi.Extension!
com.acme.vetobean.VetoExtension
Pete Muir
Extensions!
Entity listeners
Built-in components:
beans, producers
interceptors,
decorators,
observers
Pete Muir
AnnotatedType
Bean
Pete Muir
Substituting bean metadata
@Named
@ManagedBean
public class Service { ... }
Pete Muir
I think I need to create
an annotation...
Pete Muir
new AnnotationLiteral<X>() {} creates annotation X instance"
Pete Muir
new AnnotationLiteral<Named>() {} creates @Named instance"
Pete Muir
new NamedLiteral() creates @Named instance"
Pete Muir
Seam Solder!
Pete Muir
AnnotatedType builder!
Bean builders!
Annotation inspectors!
Reflection utilities!
Pete Muir
BeanManager locator!
Annotation literals!
(for standard annotations)
Resource loading!
Method injector!
Pete Muir
Implementation!
"
Ah, but can
we test it?
Pete Muir
Pete Muir
@RunWith(Arquillian.class)!
public class GreeterTestCase {!
@Deployment!
public static JavaArchive createDeployment() {!
return ShrinkWrap.create(JavaArchive.class)!
.addClass(Greeter.class)!
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");!
}!
!
@Inject Greeter greeter;!
!
@Test!
public void shouldBeAbleToInvokeBean() throws Exception {!
Assert.assertEquals("Hello, Earthlings", greeter.greet("Earthlings"));!
}!
}!
Pete Muir
Photo of man with
globe
Pete Muir
Extension Showcase!
•Alias annotations"
•Veto beans"
•Register beans from third-party library"
•Introduce behaviors to built-in annotations"
•Override injection point types"
•Bridge Servlet events to CDI"
•Expose standard component as beans"
•Emulate EJB services for managed beans"
•Set conversation boundaries declaratively"
•Initialize beans on application startup"
•Scanning for third-party framework"
Pete Muir
Who's writing extensions?!
SoftwareMill - https://github.com/softwaremill/
softwaremill-common
Pete Muir
You are!!
Pete Muir
CDI combines loose coupling
with strong typing.
Pete Muir
Q & A!
email: [email protected]!
twitter: @plmuir!
blog: http://in.relation.to/Bloggers/Pete!