0% found this document useful (0 votes)
7 views64 pages

Day 1 Track 1 Session 3

The document discusses the benefits and features of Contexts and Dependency Injection (CDI) for Java EE, highlighting its standardized, type-safe, and extensible nature. It covers how CDI manages bean lifecycles, supports dependency injection, and allows for the creation of portable extensions to enhance Java EE applications. Additionally, it provides insights into the CDI deployment lifecycle and the use of extensions to customize and improve functionality within Java EE environments.

Uploaded by

israel.paulo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views64 pages

Day 1 Track 1 Session 3

The document discusses the benefits and features of Contexts and Dependency Injection (CDI) for Java EE, highlighting its standardized, type-safe, and extensible nature. It covers how CDI manages bean lifecycles, supports dependency injection, and allows for the creation of portable extensions to enhance Java EE applications. Additionally, it provides insights into the CDI deployment lifecycle and the use of extensions to customize and improve functionality within Java EE environments.

Uploaded by

israel.paulo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Hacking Java EE

Robb Greathouse
([email protected])

Principal Software Engineer


Red Hat, Inc.

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

What can CDI do for me?!


Pete Muir
discover and resolve beans"

CDI can manage the lifecycle of beans"

inject dependent beans"

bind a bean to its scope"

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!

Bean archive (war) - WEB-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

event notification model"

interceptor bindings"

bean decorators"

Unified EL integration"

Pete Muir
Pete Muir
SPI for hacking Java EE:!
register additional
beans

satisfy injection points

introduce custom scopes

augment or override bean metadata


Pete Muir
How does it work?!

Based on Service Provider Interface


(SPI) pattern!

Implement empty Extension interface

Observe container events to alter deployment

Register extension
Pete Muir
CDI deployment lifecycle with
possible activities"

Deploy Process Process


Before Bean Scan (Annotated) Injection
Application Discovery Archives Types Targets

Pete Muir
Deploy Before Bean Scan
Application
Discovery Archives

+ add scope

+ add annotated type

+ add qualifier

+ add interceptor binding

+ 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

+ veto types and prevent further processing

+ replace annotated type

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

+ replace injection target

Pete Muir
Process Process Process
(Annotated) Injection Process Process Observe
Types Targets Beans Producers Methods

Pete Muir
Process Process Process
Injection
Targets Beans Producers

+ prepare additional beans

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 observer method

+ 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!

"All we have to decide is what


to do with the beans that are
given to us."!
Pete Muir
Extending !
Java EE! Portable 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!

Generally usefully stuff for CDI applications.!


A swiss army knife for extension writers.!

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?!

Seam 3 project - http://seamframework.org/Seam3

MyFaces CODI - http://www.irian.at/myfaces_codi

SoftwareMill - https://github.com/softwaremill/
softwaremill-common

Pete Muir
You are!!

List your extension @


http://tinyurl.com/cdi-extensions

Pete Muir
CDI combines loose coupling
with strong typing.

CDI makes Java EE flexible,


portable and extensible.

CDI extensions make Java EE


competitive and awesome!

Pete Muir
Q & A!

email: [email protected]!

twitter: @plmuir!

blog: http://in.relation.to/Bloggers/Pete!

You might also like