2/3 : cdi advanced - antoine sabot-durand

26
CDI ADVANCED Meta Data & Extensions @antoine_sd

Upload: soat

Post on 25-Jun-2015

225 views

Category:

Technology


7 download

DESCRIPTION

Allez plus Loin avec CDI En moins de 5 ans d’existence, Contexts and Dependency Injection (CDI) est devenue l’une des principale spécification de Java EE. Néanmoins, CDI est bien souvent perçu comme une simple solution d’injection de dépendance enrichie alors que cette spécification est bien plus riche que ça. Lors de cette présentation, après un rapide rappel des fonctionnalités de base de CDI, nous montrerons comment son utilisation avancée permet Java EE en intégrant des technologies legacy ou plus récent de manière naturelle. Nous finirons avec le travail en cours sur CDI 2.0 qui a commencé début septembre.

TRANSCRIPT

Page 1: 2/3 : CDI advanced - Antoine Sabot-Durand

CDI ADVANCEDMeta Data & Extensions

@antoine_sd

Page 2: 2/3 : CDI advanced - Antoine Sabot-Durand

ANTOINE SABOT-DURAND

• Senior Software Engineer @Red Hat

• Java & OSS :

• CDI co-spec lead

• CDI community development

• Tech Lead on Agorava

• @antoine_sd

Page 3: 2/3 : CDI advanced - Antoine Sabot-Durand

MEET CDI SPI

Page 4: 2/3 : CDI advanced - Antoine Sabot-Durand

SPI CAN BE SPLIT IN 4 PARTS

Page 5: 2/3 : CDI advanced - Antoine Sabot-Durand

1.SPI DEDICATED TO TYPE META-MODEL

Page 6: 2/3 : CDI advanced - Antoine Sabot-Durand

WHY HAVING A TYPE META-MODEL?

Page 7: 2/3 : CDI advanced - Antoine Sabot-Durand

WHY HAVING A TYPE META-MODEL?Because @Annotations are configuration

Page 8: 2/3 : CDI advanced - Antoine Sabot-Durand

WHY HAVING A TYPE META-MODEL?Because @Annotations are configuration

but they are also read-only

Page 9: 2/3 : CDI advanced - Antoine Sabot-Durand

WHY HAVING A TYPE META-MODEL?Because @Annotations are configuration

but they are also read-only

as we want to write configuration We need a mutable meta-model

Page 10: 2/3 : CDI advanced - Antoine Sabot-Durand

WHY HAVING A TYPE META-MODEL?Because @Annotations are configuration

but they are also read-only

as we want to write configuration We need a mutable meta-model

for annotated types

Page 11: 2/3 : CDI advanced - Antoine Sabot-Durand

2.SPI DEDICATED TO CDI META-MODEL

Page 12: 2/3 : CDI advanced - Antoine Sabot-Durand

SPI CAN BE USED IN YOUR CODE 1

@Producespublic <K, V> Map<K, V> produceMap(InjectionPoint ip) { if (valueIsNumber(ip.getType())) { return new TreeMap<K, V>(); } return new HashMap<K, V>();}

Page 13: 2/3 : CDI advanced - Antoine Sabot-Durand

SPI CAN BE USED IN YOUR CODE 2public abstract class AbstractBean { @Inject Bean<AbstractBean> meta; @Inject Instance<Foo> fooInstances; public Foo getFooWithSameQualifiers() { Annotation[] beanQualifiers = (Annotation[]) meta.getQualifiers().toArray(); Instance<Foo> mySelect = fooInstances.select(beanQualifiers); if (!(mySelect.isUnsatisfied() && mySelect.isAmbiguous())) return mySelect.get(); else throw new IllegalStateException("Foo with same qualifiers not found"); } }

Page 14: 2/3 : CDI advanced - Antoine Sabot-Durand

SPI CAN BE USED IN YOUR CODE 3

@ApplicationScoped public class ExampleBean { private void strictListen(@Observes @Qualified Payload evt, EventMetadata meta) { if(meta.getQualifiers().contains(new QualifiedLiteral()) && meta.getType().equals(Payload.class)) System.out.println("Do something"); else System.out.println("ignore"); } }

Page 15: 2/3 : CDI advanced - Antoine Sabot-Durand

3. CDI ENTRY POINTS

Page 16: 2/3 : CDI advanced - Antoine Sabot-Durand

SPI DEDICATED TO EXTENSIONS

Page 17: 2/3 : CDI advanced - Antoine Sabot-Durand

MOST OF THESE SPI ARE EVENTS CONTAINING META MODEL SPI CLASSES

Page 18: 2/3 : CDI advanced - Antoine Sabot-Durand

MOST OF THESE SPI ARE EVENTS CONTAINING META MODEL SPI CLASSES

ProcessAnnotatedType

Page 19: 2/3 : CDI advanced - Antoine Sabot-Durand

MOST OF THESE SPI ARE EVENTS CONTAINING META MODEL SPI CLASSES

These Events are automatically fired by CDI when application starts and can observed in method included in a CDI extension

Page 20: 2/3 : CDI advanced - Antoine Sabot-Durand

CDI EXTENSIONS

Page 21: 2/3 : CDI advanced - Antoine Sabot-Durand

CDI EXTENSIONS FOR WHAT ?• A CDI extension can :

• Create / modify :

• Beans

• Injection Target

• Injection Points

• Cancel beans creation

• More generally extensions can modify all CDI context at launch time

Page 22: 2/3 : CDI advanced - Antoine Sabot-Durand

TO UNDERSTAND EXTENSIONS• Once application is bootstrapped, the

Bean Manager is in read only mode (no dynamic bean registration)

• Extensions are launch during bootsrap and are based on CDI events

• You only have to @Observes built-in CDI event to create your extensions

• Learn the different events fired at boot time on next slide

Page 23: 2/3 : CDI advanced - Antoine Sabot-Durand

CDI 1.1 LIFECYCLE

Before Bean Discovery

Process BeanProcess Annotated

Type

ScanArchive

ApplicationRunning

After Deployment Validation

Before Shutdown Undeploy ApplicationProcess Producer

After Bean Discovery

ProcessInjection Target

Process Observer Method

ProcessInjection Point

Process Bean Attributes

After Type Discovery

Occurs once

occurs for each elts

internal step

Deployment starts

Bean eligibility check

Page 24: 2/3 : CDI advanced - Antoine Sabot-Durand

HOW TO BUILD A CDI EXTENSION• Create a class implementing javax.enterprise.inject.spi.Extension

• Add some method that observes CDI lifecyle events to modify Bean Manager meta data

• Add file :META-INF/services/javax.enterprise.inject.spi.Extension

in class path. And put the qualified name of the extension in it

Page 25: 2/3 : CDI advanced - Antoine Sabot-Durand

SIMPLE EXAMPLE - VETO JPA ENTITIES

public class VetoEntityExtension implements Extension { void vetoEntites(@Observes @WithAnnotations(Entity.class)ProcessAnnotatedType<?> pat) { pat.veto(); }}

Page 26: 2/3 : CDI advanced - Antoine Sabot-Durand

QUESTIONS