SlideShare a Scribd company logo
Copyright	©	2017,	Oracle	and/or	its	affiliates.	All	rights	reserved.		|
CDI	2.0	(JSR	365)
Contexts	and	Dependency	Injection	for	Java	2.0
Akihiro	Nishikawa
Cloud	Technology	Business	Unit
Oracle	Corporation	Japan
17,	May,	2017
Java	Day	Tokyo	2017
#JavaDayTokyo
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Safe	Harbor	Statement
The	following	is	intended	to	outline	our	general	product	direction.	It	is	intended	for	
information	purposes	only,	and	may	not	be	incorporated	into	any	contract.	It	is	not	a	
commitment	to	deliver	any	material,	code,	or	functionality,	and	should	not	be	relied	upon	
in	making	purchasing	decisions.	The	development,	release,	and	timing	of	any	features	or	
functionality	described	for	Oracle’s	products	remains	at	the	sole	discretion	of	Oracle.
2
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Program	Agenda
3
What’s	CDI	?
What’s	new	in	CDI	2.0?
Summary
1
2
3
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
What’s CDI?
4
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
What’s	CDI?
• Lifecycle	for	stateful objects	bound	to	lifecycle	contexts
• Type-safe	dependency	injection mechanism
• Event	notification
• Integration	with	the	Unified	Expression	Language	(EL)
• ...
5
Major	theme	of	CDI	is	“loose	coupling”
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
CDI	2.0	(JSR-365)
• Major	evolution	of	the	CDI	1.2	(JSR	346)	specification
• Spec	Lead
– Antoine Sabot-Durand	(Red	Hat	Inc.)	(@antoine_sd)
• Reference	Implementation
– Weld	:	http://download.jboss.org/weld/3.0.0.Final/
6
http://www.cdi-spec.org (@cdispec)
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		| 7
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		| 8
CDI	Timeline
December	2009
CDI	1.0	(Java	EE	6)
June	2013
CDI	1.1	(Java	EE	7)
April	2014
CDI	1.2	(1.1	Maintenance	Release)
September	2014
CDI	2.0	starts…
May	2017
CDI	2.0
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
CDI	1.0	(JSR-299)
• A	well-defined	lifecycle	for	stateful objects	bound	to	lifecycle	contexts
– Where	the	set	of	contexts	is	extensible
• A	type-safe	dependency	injection	mechanism	without	verbose	
configuration
– Dependencies	can	be	selected	at	development	or	deployment	time
• Type-safe	decorators	and	interceptors
• An	event	notification	model
• An	SPI	allowing	portable	extensions	to	integrate	cleanly	with	the	container
9
Contexts	and	Dependency	Injection	for	the	Java	EE	platform	1.0
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
CDI	1.1/1.2	(JSR-346)
• Add	automatic	enablement	of	CDI	in	Java	EE	(beans.xml	is	not	required)
• Add	introspection	with	event,	bean,	decorator	and	interceptor	meta	data
• Ease	access	to	bean	manager	from	outside	CDI	with	CDI	class
• Add	global	enablement	of	interceptors	using	the	@Priority annotation
• Add	unmanaged	allowing	easy	access	to	non-contexutal instances
• Spec	clarification	of	CDI	Lifecycle	and	Events
10
Contexts	and	Dependency	Injection	for	the	Java	EE	platform	1.1/1.2
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
CDI	1.1/1.2	(JSR-346)
• Reworking	Bean	defining	annotation	to	avoid	conflict	with	other	JSR	330	
frameworks
• Clarification	on	conversation	resolution
• OSGi official	support	in	the	API
11
Contexts	and	Dependency	Injection	for	the	Java	EE	platform	1.1/1.2
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		| 12
CDI	is	associated	with	the	following	specs
DI	(Dependency	Injection)
EJB	(Enterprise	
JavaBeans)
Bean	Validation
Interceptors
Managed	Beans
Common	Annotation
CDIJSF	(JavaServer Faces)
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
What’s	new	in	CDI	2.0?
13
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Major	Changes	
• Spec	was	split	into	3	parts
• Java	SE	Support	– Using	CDI	outside	Java	EE
• Alignment	on	Java	8	features	(streams,	lambdas,	repeating	qualifiers)
• Event	enhancement
• Configurators for	major	SPI	elements
• Possibility	to configure	or	veto	observer	methods
• Add built-in	annotation	literals
• Make	possible	to apply	interceptor	on	producers
14
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Spec	was	split	into	3	parts
15
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		| 16
Structure
CDI	Core
Java	SE Java	EE
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Why	the	spec	was	split?
• To	align	on	many	other	Java	EE	spec	which	support	Java	SE	bootstrapping
– JAX-RS,	JPA...
• To	boost	CDI	adoption	for	Spec	and	Frameworks
– Java	SE	has	been	already	supported	by	Weld,	Apache	OpenWebBeans,	Apache	
DeltaSpike and	so	on,	but	API	is	different	from	each	implementation.
– Programming	model	should	be	defined	to	avoid	user	confusion.	
• To	provide	a	mean	of	building	new	stacks	out	of	Java	EE	(MicroProfile)
17
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Name	was	changed...
• JSR	346
Contexts	and	Dependency	Injection	for	Java	EE
• JSR	365
Contexts	and	Dependency	Injection	for	Java
18
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Java	SE	support
Using	CDI	outside	Java	EE
19
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
CDI	1.x
• Not	specified
• Depending	upon	each	
implementation
– Weld
– Apache	DeltaSpike
– ...
CDI	2.0
• Standard	API	is	specified
• Must	explicitly	boot	CDI	container
20
API	to boot	CDI	in	Java	SE
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
API	to boot	CDI	in	Java	SE
import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
public class Sample {
public static void main(String... args) {
try (SeContainer container = SeContainerInitializer
.newInstance()
.initialize()) {
...
}
}
}
21
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		| 22
SeContainerInitializer
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
SeContainer interface
• Handle	for	CDI	Interface
– The	handle	is	used	to	access	the	BeanManager
• AutoCloseable
• Extends	javax.enterprise.inject.Instance
– Able	to	use	SeContainer to	look	up	Instance programatically
– If	no	qualifier	is	passed	to	SeContainer#select(),	the	@Default qualifier	is	
assumed.
– If	no	required	type	is	given,	the	required	type	is	the	same	as	the	parent’s	one.
• Able	to	use	CDI#current()
23
javax.enterprise.inject.se.SeContainer
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Controlling	the	classpath
try (SeContainer container = SeContainerInitializer.newInstance()
.addPackages(Pojo.class)
.disableDiscovery()
.initialize()) {
...
}
24
When	adding	Beans	explicitly,	bean	discovery	should	be	disabled
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Configurable
try (SeContainer container = SeContainerInitializer.newInstance()
.addExtensions()
.enableDecorators()
.selectAlternativeStereotypes()
.selectAlternatives()
.addProperty()
.initialize()) {
...
}
25
Able	to	add	Alternatives,	Stereotypes,	Decorators,	and	Extensions
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Request	Context	Management
• In	Java	SE,	most	of	built-in	contexts	is	unavailable…
–Request	context
–Session	context
–Conversation	context
–...
26
Why	is	this	feature	required?
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Request	Context	Management
• Start	and	stop	request	context	programmatically	through	built-in	bean
• Provide	built-in	Interceptor	to	handle	it	automatically
• This	feature	is	also	available	for	Java	EE.
27
How	to	manage	request	contexts
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Request	Context	Management
@ApplicationScoped
public class MyBean {
@Inject
RequestContextController requestContextController;
public void doRequest(String body) {
boolean started = requestContextController.activate();
...
if( started ) {
requestContextController.deactivate();
}
}
}
28
1)	Built-in	Beans to	activate/deactivate	Request	Context
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Request	Context	Management
@ApplicationScoped
public class MyBean {
@ActivateRequestContext
public void doRequest(String body) {
// Request context is activated during this invocation
...
}
}
Priority
Interceptor.Priority.PLATFORM_BEFORE + 100 (100)
29
2)	Built-in	Interceptor	for	automatically	processing
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Java	8	Alignment
30
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Java	8	Alignment
• Use	Java	8	feature	and	functionalities	heavily
– Functional	Interface
– Stream
• For	example,	Instance directly	supports	Stream	API
– Previously, unable	to	use	Stream	API	along	with	Instance
• Custom	libraries	using	CDI	1.x	might	be	built	again	to	align	with	CDI	2.0
31
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Example
@Inject
@Any
private Instance<Object> objects;
public void onBean(@Observers AfterBeanDiscovery abd) {
abd.addBean().produceWith(instance -> new Object())
.destroyWith((o, cc) -> {});
objects.stream().filter(Objects::nonNull)
.forEach(o->o.notify());
...
32
Builder-based	Bean	Configuration,	Lambda	expression,	and	Stream	API
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Repeating	Qualifier
• Able	to	qualify	classes	and	methods	using	the	same	annotation
– Supported	by	language	level
• In	case	of	get	all	annotations…
33
javax.enterprise.inject.spi.Annotated.getAnnotations()
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Event	Enhancement
Event	ordering
Asynchronous	events
34
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Event	Ordering
• Event	ordering	using	@Priority
– The	lowest	value	is	first
– Observers	with	no	explicit	priority	have	a	middle	range	priority	for	allowing	observer	
to	be	called	last
– Observer	with	the	same	priority	are	called	in	an	undefined	order
• Only	applicable	to	synchronous	events
– @Priority does	not	work	for	ordering	asynchronous	events
35
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Event	Ordering
import javax.enterprise.inject.spi.ObserverMethod;
import javax.interceptor.Interceptor;
...
// ObserverMethod.DEFAULT_PRIORITY
// = Interceptor.Priority.APPLICATION + 500
System.out.println(ObserverMethod.DEFAULT_PRIORITY); // 2500
System.out.println(Interceptor.Priority.APPLICATION); // 2000
36
Default	Priority
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Event	Ordering
public void observer1(
@Observes @Priority(1) Payload p) {
...
}
public void observer2(
@Observes @Priority(2) Payload p) {
...
}
37
Example	1
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Event	Ordering
public class MyExtension implements Extension {
public void firstProcessAnnotatedType(
@Observes @Priority(1) ProcessAnnotatedType<?> pat) {
...
}
public void secondProcessAnnotatedType(
@Observes @Priority(2) ProcessAnnotatedType<?> pat) {
...
}
}
38
Example	2	– along	with	portable	extensions
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Events	in	CDI	1.x
• Sync/Async is	not	specified,	but	implementation	uses	Synchronous	model
– Observers	are	called	in	the	firing	thread	(i.e.	synchronous event)	without	particular	
order	specified
• Immutable	status	of	the	payload	is	not	specified
– Payload	is	mutated in	some	implements	and	framework
• Observers	may	inject	beans
• Observers	are	aware	of	Transactional	and	security	contexts	
39
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Synchronous	Events
@Inject
Event<Payload> event;
public void syncCaller() {
// Fire an event synchronously
event.fire(new Payload());
}
public void syncCallee(@Observes Payload p) {
// Do something
}
40
Firing	and	Observing	events
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Asynchronous	Events
• Unable	to	mutate	payload
• Unable	to	access	CDI	contexts	state	at	the	firing	point
• Not	transactional
Event#fireAsync()
@ObservesAsync
41
CDI	2.0
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Asynchronous	Events
@Inject
Event<Payload> event;
public void asyncCaller() {
// Fire an event asynchronously
event.fireAsync(new Payload());
}
public void syncCallee(@ObservesAsync Payload p) {
// Do something
}
42
Firing	and	observing	events
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Asynchronous	Events
@Inject
Event<PanelUpdater> event;
public void someMethod() {
event.fireAsync( new PanelUpdater(green),
executor);
...
}
43
Example	1	– Executor
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Asynchronous	Events
@Inject
Event<PanelUpdater> event;
public void someMethod() {
event.fireAsync( new PanelUpdater(green),
SwingUtilities::invokeLater );
...
}
44
Example	2	– Asynchronous	event	in	GUI	thread
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Asynchronous	Events
• Exception	raised	in	one	async observer	does	not	stop	execution	as	in	sync	
observer.
– One	of	the	reasons	why	firing	async event	does	not	trigger	sync	observers.
• Event.fireAsync returns	CompletionStage
– Exception	in	fireAsync returned	CompletionStage is	CompletionException,	
which	holds	all	the	exceptions	in	the	suppressed	exception	set.
• Allowing	integration	of	async events	in	standard	Java	8	async pipeline.
45
Exception	handling
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Asynchronous	Events
@Inject
Event<PanelUpdater> event;
public void someMethod() {
...
CompleteionStage<PanelUpdater> stage =
event.fireAsync( new PanelUpdater(green),
SwingUtilities::invokeLater );
...
}
46
Example	3	– Exception	handing
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Asynchronous	Events
• stage.whenComplete()
– To	handle	result	or	exception
• stage.handle()
– Same	as	above	but	allows	transformation	of	stage.
• stage.exceptionally()
– Used	in	the	only	case	for	treating	exception
47
Exception	handling	with	standard	Java	8	Asynchronous	API
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		| 48
Synchronous Events and Asynchronous Events
event.fire() @Observes
event.fireAsync() @ObservesAsync
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Are	you	planning	migration	to	asynchronous	events?
• Simple	replacement	might	raise	problems!
• Recommendation
– In	case	of	using	sync	events	as	of	now,	you	should	keep	as	it	is.
– Some	limitation	should	be	recognized.
– Going	sync/async should	be	a	decision	taken	from	firing	side.
– Being	sync	should	be	possible	from	observing	side.
49
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Threading	and	Events in	Java	SE
• Async events
– Able	to	use	Custom	Executor
• Sync	events
– Always	within	the	same	thread
– When	observers	handle	
synchronous	event	in	Java	SE,	
request	scope	activation	might	
be	required.
50
@Inject
Event <Pojo> event;
@ActivateRequestContext
public void syncSender() {
event.fire(event);
...
}
public void syncReceiver(
@Observes Pojo event ) {
...
}
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Previous
• BeanManager#fireEvent()
CDI	2.0
• BeanManager#getEvent()
– Able	to	get	Event object
– Event	is	typed	to	Object and	implicitly	
annotated	with	@Default qualifier
• BeanManager#fireEvent() is	
deprecated	since	CDI	2.0.
51
Programmatic	Events
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Add	built-in	annotation	literals
52
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		| 53
Indeed	AnnotationLiteral is	helpful	if	there	is	no	member	in	an	annotation.
Standard	Annotation	Instances
Annotation instance =
new AnnotationLiteral<ApplicationScoped>(){};
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		| 54
But	we	have	to	create	a	class	when	an	annotation	has	members...
Standard	Annotation	Instances
public class NamedLiteral
extends AnnotationLiteral<Named> implements Named{
private String value;
public NamedLiteral(String value) {
this.value = value;
}
@Override public String value() {
return null;
}
}
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
• javax.enterprise.inject.
– Any
– Default
– New
– Specialized
– Vetoed
• javax.enterprise.util.
– Nonbinding
• javax.enterprise.inject.
– Alternative
– Typed
• javax.enterprise.context.
– Initialized
– Destroyed
– RequestScoped
– SessioScoped
– ApplicationScoped
– Dependent
– ConversationScoped
55
Built-in	annotation	literals
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Built-in	annotation	literals
Default defaultLiteral = new Default.Literal();
// No members
ApplicationScoped literal = ApplicationScoped.Literal.INSTANCE;
// If annotation have members
Initialized initializedForApplicationScoped
= new Initialized.Literal(ApplicationScoped.class);
Initialized initializedForRequestScoped
= Initialized.Literal.of(RequestScoped.class);
56
Example
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Configurators	for	major	SPI	elements
57
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Configurators	for	metadata
• AnnotatedType
• InjectionPoint
• Bean
• ObserverMethod
58
Even	if	adding	info	to	an	existing	metadata,	quite	verbose	work	is	required.
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		| 59
In	CDI	1.2
Configurators for metadata
I	want	to	modify	some	legacy	framework	
which	contains	@CacheContext annotations	
in	order	to	adapt	CDI.	What	should	I	do?
If	I	were	you,	I	would	create	some	extension	to	
replace	@CacheContext annotation	with	
@Inject in	AnnotatedTypes...
But	Do	I	have	to	create	huge	codes	to	do	so?
Exactly...
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Configurators	interfaces
• AnnotatedType
– AnnotatedTypeConfigurator
• InjectionPoint
– InjectionPointConfigurator
• BeanAttributes
– BeanAttributesConfigurator
• Bean
– BeanConfigurator
• ObserverMethod
– ObserverMethodConfigurator
• Producer
– ProducerConfigurator
60
Newly	introduced	in	CDI	2.0
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		| 61
In	CDI	2.0
Configurators for meta-data
What	do	you	do	when	you	want	to	
adapt	CDI	to	some	legacy	framework?
If	I	were	you,	I	would	create	metadata	with	
“configurators”,	which	are	accessible	
through	container	lifecycle	events	and	
automatically	built	by	the	container	at	the	
end	of	observer	invocation.
Oh,	it	sounds	nice…
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
CDI 2.0
public class AutoInjectExtension implements Extension {
public <T> void CreateIP(
@Observes
@WithAnnotations(CacheContext.class)
ProcessAnnotatedType<T> pat) {
pat.configureAnnotatedType().filterFields(
f -> f.isAnnotationPresent(CacheContext.class)
).forEach(f -> f.add(InjectLiteral.INSTANCE));
}
}
62
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Configure	or	veto	observer	methods
63
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Application	Lifecycle	Events
(fired	only	one	time)
• Startup
– BeforeBeanDiscovery
– AfterTypeDiscovery
– AfterBeanDiscovery
– AfterDeploymentValidation
• Shutdown
– BeforeShutdown
Bean	Discovery	Events
(might	be	fired	multiple	times)
– ProcessAnnotatedType
– ProcessInjectionPoint
– ProcessInjectionTarget
– ProcessBeanAttributes
– ProcessBean
– ProcessProducer
– ProcessObserverMethod
64
Container	lifecycle	events
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Directing	
requests
Type	
Discovery
Search	service	
providers
Bean
Discovery
65
Container	Startup
Application	Lifecycle	Events
BeforeBeanDiscovery
Detect	deployment	
problems
AfterTypeDiscovery
AfterBeanDiscovery
AfterDeploymentValidation
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
destroy	all	
contexts
66
Container	Shutdown
Application	Lifecycle	Events
BeforeShutdown
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Add	new	methods	in	ProcessObserverMethod
public interface ProcessObserverMethod<T, X> {
...
// Replaces the ObserverMethod
public void setObserverMethod(ObserverMethod<T> observerMethod);
// Returns a ObserverMethodConfigurator initialized with the
ObserverMethod processed by this event
public ObserverMethodConfigurator<T> configureObserverMethod();
// forces the container to ignore the ObserverMethod
public void veto();
}
67
javax.enterprise.inject.spi.ProcessSyntheticObserverMethod<T, X>
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Apply	interceptor	on	producers
68
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		| 69
Able to apply @Transactional to a	producer method
Interceptor	is	not	bound	to	a	produced	Bean	in	CDI	1.x
@Produces
@Transactional
public MyService produceService() {
...
}
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
IntereptionFactory Interface
public interface InterceptionFactory<T> {
InterceptionProxyFactory<T> ignoreFinalMethods();
AnnotatedTypeConfigurator<T> configure();
<T> T createInterceptedInstance(T InstanceToWrap);
}
70
javax.enterprise.inject.spi.InterceptionFactory<T>
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Add	@transactional on	one	produced	bean	method
@Produces @RequestScoped
public MyClass createJpaEmAssumed(InterceptionFactory<MyClass> f) {
AnnotatedTypeConfigurator<MyClass> atc = f.configure();
atc.filterMethods(m -> m.getJavaMember()
.getName()
.equals("doSomething"))
.findFirst()
.ifPresent(m -> m.add(
new AnnotationLiteral<Transactional>(){ }));
return f.createInterceptedInstance(new MyClass());
}
71
CDI	2.0
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Others
72
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Programmatic	lookup	– Instance Enhancement
Instance obj;
...
if( !obj.isAmbiguous() && !obj.isUnsatisfied() ) {
...
}
73
Previous
isUnsatisfied()
Returns true if	there	is	no	bean	found,	or false otherwise.
isAmbiguous()
Returns true if	there	is	more	than	one	bean	found,	or false otherwise.
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Programmatic	lookup	– Instance Enhancement
Instance obj;
...
if(obj.isResolvable()) {
...
}
74
CDI	2.0
isResolvable()
Returns true if	there	is	exactly	one	bean	found,	or false otherwise.
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Summary
75
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Key	takeaway
• Able	to	use	CDI	2.0	on	Java	EE	as	well	as	Java	SE.
• Enhancements	and	new	features	help	increase	developer	productivity.
• Weld	3.0,	reference	Implementation of	CDI,	will	be	integrated	with	
GlassFish 5	soon!
76
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Appendix
77
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Deprecated	methods
When? Method Alternatives
CDI	2.0 BeanManager#fireEvent() BeanManager#getEvent()
CDI	1.1 BeanManager#createInjectionTarget() BeanManager#getInjectionTargetFactory()
CDI	1.1 javax.enterprise.inject
.spi.Bean#isNullable()
No alternatives
CDI	1.1 AnnotatedMember#isStatic() Check with
AnnotatedMember#getJavaMember().getModi
fiers()
CDI	1.1 BeforeBeanDiscovery#addAnnotatedType(
AnnotatedType<?> type)
eforeBeanDiscovery#addAnnotatedType(
AnnotatedType<?> type, String id)
78
Copyright	©	2017, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Safe	Harbor	Statement
The	preceding	is	intended	to	outline	our	general	product	direction.	It	is	intended	for	
information	purposes	only,	and	may	not	be	incorporated	into	any	contract.	It	is	not	a	
commitment	to	deliver	any	material,	code,	or	functionality,	and	should	not	be	relied	upon	
in	making	purchasing	decisions.	The	development,	release,	and	timing	of	any	features	or	
functionality	described	for	Oracle’s	products	remains	at	the	sole	discretion	of	Oracle.
79
CDI 2.0 (JSR 365) - Java Day Tokyo 2017 (English)
CDI 2.0 (JSR 365) - Java Day Tokyo 2017 (English)

More Related Content

PDF
Polyglot on the JVM with Graal (Japanese)
Logico
 
PDF
Polyglot on the JVM with Graal (English)
Logico
 
PDF
Build Smart Service on GCP - Google DevFest 2018 Taiwan
David Chen
 
PDF
Test Engineering on Mobage
Masaki Nakagawa
 
PDF
Next ’19 的 Istio 場次 重點摘要
William Yeh
 
PDF
Dev(Sec)Ops - Architecture for Security and Compliance
Yi-Feng Tzeng
 
PDF
Software Supply Chain Management with Grafeas and Kritis
Aysylu Greenberg
 
PDF
Eclipse microprofile config and OSGi config admin - E Jiang
mfrancis
 
Polyglot on the JVM with Graal (Japanese)
Logico
 
Polyglot on the JVM with Graal (English)
Logico
 
Build Smart Service on GCP - Google DevFest 2018 Taiwan
David Chen
 
Test Engineering on Mobage
Masaki Nakagawa
 
Next ’19 的 Istio 場次 重點摘要
William Yeh
 
Dev(Sec)Ops - Architecture for Security and Compliance
Yi-Feng Tzeng
 
Software Supply Chain Management with Grafeas and Kritis
Aysylu Greenberg
 
Eclipse microprofile config and OSGi config admin - E Jiang
mfrancis
 

What's hot (20)

PDF
Node.js vs. java
MoonTechnolabsPvtLtd
 
DOCX
Venkateswarlu_Malapati - Lead Developer
Venki Malapati
 
PDF
Java web applications based on new Struts 2.5 and AngularJS (ApacheCon NA 2016)
Johannes Geppert
 
PDF
The Future of Java and You
Heather VanCura
 
PDF
de:code 2019 DT06 vs-show どっちのVSショー
Issei Hiraoka
 
PDF
楽天が挑むDevOps
Rakuten Group, Inc.
 
PDF
PRINCE2 Foundation Slides - Sample
Frank Turley
 
PDF
Why Have The Specifications Been Based On Java™ Technology? - P Kriens
mfrancis
 
PDF
Joseph W. Yoder, Being Agile about Quality
Hironori Washizaki
 
PDF
Journey from Monolith to a Modularized Application - Approach and Key Learnin...
mfrancis
 
PDF
Oracle Cloud Platform - Migrer vers la Database 12c
Bastien Leblanc
 
PDF
Developers vs DBAs - How to win the war
gvenzl
 
PDF
Profiling distributed Java applications
Constantine Slisenka
 
PDF
洗白你的軟體架構
teddysoft
 
PDF
PRINCE2 Practitioner Classroom slides - Sample
Frank Turley
 
PDF
PRINCE2 Foundation Classroom slides - Sample - Classic
Frank Turley
 
PDF
Devops Intro - Devops for Unicorns & DevOps for Horses
BoonNam Goh
 
PDF
Microsoft Silverlight 2
David Chou
 
PDF
BT215-BarracudaWebSecurityServiceCertifiedProductSpecialist
Nat (Natalie) Dowling
 
PPTX
DevOps overview and tech interview tips
Daniel Bezerra
 
Node.js vs. java
MoonTechnolabsPvtLtd
 
Venkateswarlu_Malapati - Lead Developer
Venki Malapati
 
Java web applications based on new Struts 2.5 and AngularJS (ApacheCon NA 2016)
Johannes Geppert
 
The Future of Java and You
Heather VanCura
 
de:code 2019 DT06 vs-show どっちのVSショー
Issei Hiraoka
 
楽天が挑むDevOps
Rakuten Group, Inc.
 
PRINCE2 Foundation Slides - Sample
Frank Turley
 
Why Have The Specifications Been Based On Java™ Technology? - P Kriens
mfrancis
 
Joseph W. Yoder, Being Agile about Quality
Hironori Washizaki
 
Journey from Monolith to a Modularized Application - Approach and Key Learnin...
mfrancis
 
Oracle Cloud Platform - Migrer vers la Database 12c
Bastien Leblanc
 
Developers vs DBAs - How to win the war
gvenzl
 
Profiling distributed Java applications
Constantine Slisenka
 
洗白你的軟體架構
teddysoft
 
PRINCE2 Practitioner Classroom slides - Sample
Frank Turley
 
PRINCE2 Foundation Classroom slides - Sample - Classic
Frank Turley
 
Devops Intro - Devops for Unicorns & DevOps for Horses
BoonNam Goh
 
Microsoft Silverlight 2
David Chou
 
BT215-BarracudaWebSecurityServiceCertifiedProductSpecialist
Nat (Natalie) Dowling
 
DevOps overview and tech interview tips
Daniel Bezerra
 
Ad

Viewers also liked (13)

PDF
劇的改善 Ci4時間から5分へ〜私がやった10のこと〜
aha_oretama
 
PDF
ユニットテストのアサーション 流れるようなインターフェースのAssertJを添えて 入門者仕立て
Ryosuke Uchitate
 
PDF
Java9を迎えた今こそ!Java本格(再)入門
Takuya Okada
 
PDF
JJUG初心者のためのJava/JJUG講座
Yusuke Suzuki
 
PDF
Java SE 9の紹介: モジュール・システムを中心に
Taku Miyakawa
 
PPTX
将来 自分で サービスを持ちたいエンジニアの葛藤
Yoshio Kajikuri
 
PPTX
高速なソートアルゴリズムを書こう!!
masakazu matsubara
 
PDF
JVM上で動くPython処理系実装のススメ
Yoshiaki Shibutani
 
PDF
Another compilation method in java - AOT (Ahead of Time) compilation
Logico
 
PPTX
Dockerで始める Java EE アプリケーション開発 for JJUG CCC 2017
Kohei Saito
 
PPTX
サーバサイド Kotlin
Hiroki Ohtani
 
PDF
Selenide or Geb 〜あなたはその時どちらを使う〜
Youtarou TAKAHASHI
 
PPTX
JEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_ccc
YujiSoftware
 
劇的改善 Ci4時間から5分へ〜私がやった10のこと〜
aha_oretama
 
ユニットテストのアサーション 流れるようなインターフェースのAssertJを添えて 入門者仕立て
Ryosuke Uchitate
 
Java9を迎えた今こそ!Java本格(再)入門
Takuya Okada
 
JJUG初心者のためのJava/JJUG講座
Yusuke Suzuki
 
Java SE 9の紹介: モジュール・システムを中心に
Taku Miyakawa
 
将来 自分で サービスを持ちたいエンジニアの葛藤
Yoshio Kajikuri
 
高速なソートアルゴリズムを書こう!!
masakazu matsubara
 
JVM上で動くPython処理系実装のススメ
Yoshiaki Shibutani
 
Another compilation method in java - AOT (Ahead of Time) compilation
Logico
 
Dockerで始める Java EE アプリケーション開発 for JJUG CCC 2017
Kohei Saito
 
サーバサイド Kotlin
Hiroki Ohtani
 
Selenide or Geb 〜あなたはその時どちらを使う〜
Youtarou TAKAHASHI
 
JEP280: Java 9 で文字列結合の処理が変わるぞ!準備はいいか!? #jjug_ccc
YujiSoftware
 
Ad

Similar to CDI 2.0 (JSR 365) - Java Day Tokyo 2017 (English) (20)

PDF
MySQL Shell - The DevOps Tool for MySQL
Miguel Araújo
 
PDF
REST in an Async World
David Delabassee
 
PDF
Java EE 8 - February 2017 update
David Delabassee
 
PDF
Oow MySQL Whats new in security overview sept 2017 v1
Mark Swarbrick
 
PDF
Why MySQL High Availability Matters
Mark Swarbrick
 
PDF
MOUG17 Keynote: What's New from Oracle Database Development
Monica Li
 
PDF
TDC2018SP | Trilha Java Enterprise - O Java EE morreu? EE4J e so um plugin? E...
tdc-globalcode
 
PDF
Time-series Analytics using Matrix Profile and SAX
SUPREET OBEROI
 
PDF
TDC2018SP | Trilha Arq Java - Crie arquiteturas escalaveis, multi-language e ...
tdc-globalcode
 
PDF
NZOUG-GroundBreakers-2018 - Troubleshooting and Diagnosing 18c RAC
Sandesh Rao
 
PDF
JCP 20 Year Anniversary
Heather VanCura
 
PDF
Bringing Java into the Open
Heather VanCura
 
PPTX
OpenJDK 参加入門 [JJUG CCC 2017 Fall E2]
David Buck
 
PDF
Deep Dive into Automating Oracle GoldenGate Using the New Microservices
Kal BO
 
PDF
The Future of Java and You
Heather VanCura
 
PDF
Chicago JUG / GOTO Meetup
Ed Burns
 
PDF
Application Development with Oracle Database
gvenzl
 
PDF
Harnessing the Power of Optimizer Hints
Maria Colgan
 
PDF
Democratizing Serverless—The Open Source Fn Project - Serverless Summit
CodeOps Technologies LLP
 
PDF
Migrating your infrastructure to OpenStack - Avi Miller, Oracle
OpenStack
 
MySQL Shell - The DevOps Tool for MySQL
Miguel Araújo
 
REST in an Async World
David Delabassee
 
Java EE 8 - February 2017 update
David Delabassee
 
Oow MySQL Whats new in security overview sept 2017 v1
Mark Swarbrick
 
Why MySQL High Availability Matters
Mark Swarbrick
 
MOUG17 Keynote: What's New from Oracle Database Development
Monica Li
 
TDC2018SP | Trilha Java Enterprise - O Java EE morreu? EE4J e so um plugin? E...
tdc-globalcode
 
Time-series Analytics using Matrix Profile and SAX
SUPREET OBEROI
 
TDC2018SP | Trilha Arq Java - Crie arquiteturas escalaveis, multi-language e ...
tdc-globalcode
 
NZOUG-GroundBreakers-2018 - Troubleshooting and Diagnosing 18c RAC
Sandesh Rao
 
JCP 20 Year Anniversary
Heather VanCura
 
Bringing Java into the Open
Heather VanCura
 
OpenJDK 参加入門 [JJUG CCC 2017 Fall E2]
David Buck
 
Deep Dive into Automating Oracle GoldenGate Using the New Microservices
Kal BO
 
The Future of Java and You
Heather VanCura
 
Chicago JUG / GOTO Meetup
Ed Burns
 
Application Development with Oracle Database
gvenzl
 
Harnessing the Power of Optimizer Hints
Maria Colgan
 
Democratizing Serverless—The Open Source Fn Project - Serverless Summit
CodeOps Technologies LLP
 
Migrating your infrastructure to OpenStack - Avi Miller, Oracle
OpenStack
 

More from Logico (13)

PDF
Welcome, Java 15! (Japanese)
Logico
 
PDF
Look into Project Valhalla from CLR viewpoint
Logico
 
PDF
Jvmls 2019 feedback valhalla update
Logico
 
PDF
Project Helidon Overview (Japanese)
Logico
 
PDF
Oracle Code One 2018 Feedback (Server Side / Japanese)
Logico
 
PDF
ADBA (Asynchronous Database Access)
Logico
 
PDF
Java EE 8 Overview (Japanese)
Logico
 
PDF
Nashorn in the future (Japanese)
Logico
 
PDF
Nashorn in the future (English)
Logico
 
PDF
これからのNashorn
Logico
 
PDF
Nashorn in the future (English)
Logico
 
PDF
Nashorn: JavaScript Running on Java VM (English)
Logico
 
PDF
Nashorn : JavaScript Running on Java VM (Japanese)
Logico
 
Welcome, Java 15! (Japanese)
Logico
 
Look into Project Valhalla from CLR viewpoint
Logico
 
Jvmls 2019 feedback valhalla update
Logico
 
Project Helidon Overview (Japanese)
Logico
 
Oracle Code One 2018 Feedback (Server Side / Japanese)
Logico
 
ADBA (Asynchronous Database Access)
Logico
 
Java EE 8 Overview (Japanese)
Logico
 
Nashorn in the future (Japanese)
Logico
 
Nashorn in the future (English)
Logico
 
これからのNashorn
Logico
 
Nashorn in the future (English)
Logico
 
Nashorn: JavaScript Running on Java VM (English)
Logico
 
Nashorn : JavaScript Running on Java VM (Japanese)
Logico
 

Recently uploaded (20)

PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Software Development Methodologies in 2025
KodekX
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 

CDI 2.0 (JSR 365) - Java Day Tokyo 2017 (English)