cdi 1.1 university

58
@ antoine_sd # CDIUni CDI University @antoine_sd Red Hat, http://cdi-spec.org CDI specification leader

Upload: antoine-sabot-durand

Post on 10-May-2015

846 views

Category:

Technology


1 download

DESCRIPTION

English version of my Devoxx France 2014 univeristy on CDI 1.1.

TRANSCRIPT

Page 1: CDI 1.1 university

@antoine_sd#CDIUni

CDI University

@antoine_sd Red Hat, http://cdi-spec.org CDI specification leader

Page 2: CDI 1.1 university

@antoine_sd#CDIUni

Antoine Sabot-Durand

•Senior Software Developer @Red Hat !

•Java & OSS :

• CDI co-spec lead

• CDI eco-system development

• Tech Lead on Agorava

!

•@antoine_sd

•@cdispec

Page 3: CDI 1.1 university

@YourTwitterHandle#DVXFR14{session hashtag} @antoine_sd#CDIUni

CDI b

asics

Page 4: CDI 1.1 university

@antoine_sd#CDIUni

•CDI is a lot of things

• Java EE IoC standard also working on Java SE

• Context management

• Observer pattern included

• The « cement » between most Java EE spec

• An evolution engine for Java EE thanks to portable extensions

• All of this based on java strong typing

Context & Dependency Injection

Page 5: CDI 1.1 university

@antoine_sd#CDIUni

CDI history & actual state

•Main milestone :

• December 2009 : CDI 1.0

• June 2013 : CDI 1.1

• April 2014 : CDI 1.2

• Summer 2014 : CDI 2.0 development starting

•Implementations :

• JBoss Weld (RI) : WildFly, JBoss EAP, Glassfish, Weblogic

• Apache OpenWebBeans : TomEE, Websphere

•Check http://cdi-spec.org

Page 6: CDI 1.1 university

@antoine_sd#CDIUni

CDI activated by default in Java EE 7

•In Java EE 6, you must add a beans.xml file to your archive

•No need to in Java EE 7 but you can still to have an explicit activation (behave differently)

beans.xml

Page 7: CDI 1.1 university

@antoine_sd#CDIUni

Bean

•In Java EE 6 everything is a Managed Bean

• Managed beans are basic components

• They are managed by the container

• They all have a lifecycle

• They can be intercepted (AOP)

• They can be injected

• Accessible from outside CDI code.

Page 8: CDI 1.1 university

@antoine_sd#CDIUni

Dependency Injection

@Inject

Page 9: CDI 1.1 university

@antoine_sd#CDIUni

public class HelloService {!    public String hello() {!        return "Hello World!";!    }!}

Dependency Injection

Page 10: CDI 1.1 university

@antoine_sd#CDIUni

public class MyBean {!!    private HelloService service;!!    @Inject!    public MyBean(HelloService service) {!        this.service = service;!    }!    public void displayHello() {!        display( service.hello();!    }!}

Dependency Injection in constructor

Page 11: CDI 1.1 university

@antoine_sd#CDIUni

public class MyBean {!!    private HelloService service;!!    @Inject!    public void setService(HelloService service) {!        this.service = service;!    }!    public void displayHello() {!        display( service.hello();!    }!}

Dependency Injection in setter

Page 12: CDI 1.1 university

@antoine_sd#CDIUni

public class MyBean {!!    @Inject HelloService service;!!    public void displayHello() {!        display( service.hello();!    }!}

Dependency Injection in field

Page 13: CDI 1.1 university

@antoine_sd#CDIUni

public interface HelloService {!    public String hello();!}!public class FrenchHelloService implements HelloService {!    public String hello() {!        return "Bonjour tout le monde!";!    }!}!public class EnglishHelloService implements HelloService {!    public String hello() {!        return "Hello World!";!    }!}

Qualifiers

Page 14: CDI 1.1 university

@antoine_sd#CDIUni

@Qualifier!@Retention(RUNTIME)!@Target({FIELD, TYPE, METHOD, PARAMETER}) !public @interface French {}!!@Qualifier!@Retention(RUNTIME)!@Target({FIELD, TYPE, METHOD, PARAMETER}) !public @interface English {}

Qualifiers

Page 15: CDI 1.1 university

@antoine_sd#CDIUni

@French!public class FrenchHelloService implements HelloService {!    public String hello() {!        return "Bonjour tout le monde!";!    }!}!!@English!public class EnglishHelloService implements HelloService {!    public String hello() {!        return "Hello World!";!    }!}

Qualifiers

Page 16: CDI 1.1 university

@antoine_sd#CDIUni

public class MyBean {!    @Inject @French HelloService service;!    public void displayHello() {!        display( service.hello();!    }!}!public class MyBean {!    @Inject @English HelloService service;!    public void displayHello() {!        display( service.hello();!    }!}

Qualifiers

Page 17: CDI 1.1 university

@antoine_sd#CDIUni

@Qualifier!@Retention(RUNTIME)!@Target({FIELD, TYPE, METHOD, PARAMETER}) !public @interface Language {!!    Languages value();! ! @Nonbinding String description() default "";!!    public enum Languages { !        FRENCH, ENGLISH!    }!}

Qualifiers with members

Page 18: CDI 1.1 university

@antoine_sd#CDIUni

@Language(FRENCH)!public class FrenchHelloService implements HelloService {!    public String hello() {!        return "Bonjour tout le monde!";!    }!}!@Language(ENGLISH)!public class EnglishHelloService implements HelloService {!    public String hello() {!        return "Hello World!";!    }!}

Qualifiers

Page 19: CDI 1.1 university

@antoine_sd#CDIUni

public class MyBean {!    @Inject @Language(ENGLISH) HelloService service;!    public void displayHello() {!        display( service.hello();!    }!}!!public class MyBean {!    @Inject @Language(FRENCH) HelloService service;!    public void displayHello() {!        display( service.hello();!    }!}

Qualifiers

Page 20: CDI 1.1 university

@antoine_sd#CDIUni

public class MyBean {!    @Inject @French ! HelloService service;!}!!@French @Console @Secured!public class FrenchHelloService implements HelloService {!!}

Qualifiers

Page 21: CDI 1.1 university

@antoine_sd#CDIUni

public class MyBean {!    @Inject @French @Console! HelloService service;!}!!@French @Console @Secured!public class FrenchHelloService implements HelloService {!!}

Qualifiers

Page 22: CDI 1.1 university

@antoine_sd#CDIUni

public class MyBean {!    @Inject @French @Console @Secured ! HelloService service;!}!!@French @Console @Secured!public class FrenchHelloService implements HelloService {!!}

Qualifiers

Page 23: CDI 1.1 university

@antoine_sd#CDIUni

public class MyBean {!    @Inject @French @Console @Secured ! HelloService service;!}!!@French @Secured!public class FrenchHelloService implements HelloService {!!}

Qualifiers

Page 24: CDI 1.1 university

@antoine_sd#CDIUni

Reserved Qualifiers

@Default!@Any!@Named

Page 25: CDI 1.1 university

@antoine_sd#CDIUni

public class MyBean {!!    @Inject Instance<HelloService> service;!!    public void displayHello() {!        display( service.get().hello() );!    }!}

Programmatic lookup

Page 26: CDI 1.1 university

@antoine_sd#CDIUni

public class MyBean {!!    @Inject Instance<HelloService> service;!!    public void displayHello() {!        if (!service.isUnsatisfied()) {!            display( service.get().hello() );!        }!    }!}

Programmatic lookup

Page 27: CDI 1.1 university

@antoine_sd#CDIUni

public class MyBean {!!    @Inject @Any Instance<HelloService> services;!!    public void displayHello() {!        for (HelloService service : services) {!            display( service.hello() );!        }!    }!}

Programmatic lookup

Page 28: CDI 1.1 university

@antoine_sd#CDIUni

public class MyBean {!!    @Inject @Any Instance<HelloService> services;!!    public void displayHello() {!        display( !            service.select(! new AnnotationLiteral()<French> {})!                .get() );!    }!}

Programmatic lookup

Page 29: CDI 1.1 university

@antoine_sd#CDIUni

Contexts

•Manage bean lifecycle • context helps container to choose when a bean should be generated and

destroyed

• it enforces the fact that a given bean is a singleton for a given context

•Built-in CDI contexts : • @Dependent (default)

• @RequestScoped

• @SessionScoped

• @ConversationScoped

• @ApplicationScoped

• @Singleton

•You can create your own scope

Page 30: CDI 1.1 university

@antoine_sd#CDIUni

@SessionScoped!public class CartBean {!!    public void addItem(Item item) {! ...!    } !}

Contexts

Page 31: CDI 1.1 university

@antoine_sd#CDIUni

@ApplicationScoped!public class CartBean {!!    public void addItem(Item item) {! ...!    } !}

Contexts

FAIL !!

!

Page 32: CDI 1.1 university

@antoine_sd#CDIUni

@ConversationScoped!public class CartBean {!!    public void addItem(Item item) {! ...!    } !}

Contexts

Page 33: CDI 1.1 university

@antoine_sd#CDIUni

@ThreadScoped!public class CartBean {!!    public void addItem(Item item) {! ...!    } !}

Contexts

Page 34: CDI 1.1 university

@antoine_sd#CDIUni

@HourScoped!public class CartBean {!!    public void addItem(Item item) {! ...!    } !}

Contexts

Page 35: CDI 1.1 university

@antoine_sd#CDIUni

@RandomScoped!public class CartBean {!!    public void addItem(Item item) {! ...!    } !}

Contexts

Page 36: CDI 1.1 university

@antoine_sd#CDIUni

@Produces!public MyNonCDIClass myProducer() {!return new MyNonCdiClass();!}!...!@Inject!MyNonCDIClass bean;!

Producers

Page 37: CDI 1.1 university

@antoine_sd#CDIUni

@Produces!@RequestScoped!public FacesContext produceFacesContext() {! return FacesContext.getCurrentInstance();!}

Producers

Page 38: CDI 1.1 university

@antoine_sd#CDIUni

@Produces!public Logger produceLog(InjectionPoint injectionPoint) {! return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());!}

Producers

Page 39: CDI 1.1 university

@antoine_sd#CDIUni

@Decorator!@Priority(Interceptor.Priority.APPLICATION)!public class HelloDecorator implements HelloService {!!    @Inject @Delegate HelloService service;!!    public String hello() {!        return service.hello() + "-decorated";!    }!}

Decorators

Page 40: CDI 1.1 university

@antoine_sd#CDIUni

@InterceptorBinding!@Target({METHOD, TYPE}) !@Retention(RUNTIME)!public @interface Loggable {}

Interceptors

Page 41: CDI 1.1 university

@antoine_sd#CDIUni

@Interceptor @Loggable !public class LogInterceptor {!  @AroundInvoke!  public Object log(InvocationContext ic) throws Exception {!   System.out.println("Entering " + ic.getMethod().getName());!        try {!            return ic.proceed(); !        } finally {!            System.out.println("Exiting " + ic.getMethod().getName());!        }!    } !}

Interceptors

Page 42: CDI 1.1 university

@antoine_sd#CDIUni

@Loggable!public class MyBean {!!    @Inject HelloService service;!!    public void displayHello() {!        display( service.hello();!    }!}

Interceptors

Page 43: CDI 1.1 university

@antoine_sd#CDIUni

@Inject Event<Post> evt;!!…!!evt.fire(new Post("John Doe ", "Hello", ));!!…!!public void receiveEvt(@Observes Post evt) {!    System.out.println("Received : " + evt.message());!}

Events

Page 44: CDI 1.1 university

@YourTwitterHandle#DVXFR14{session hashtag} @antoine_sd#CDIUni

CDI

exte

nsion

Page 45: CDI 1.1 university

@antoine_sd#CDIUni

Extension for what use ?

•To change CDI container meta-data:

• AnnotatedType

• InjectionPoint / InjectionTarget

• BeanAttributes/Beans

• Producer

• Observer

!

•By observing the events triggered by CDI at boot time

Page 46: CDI 1.1 university

@antoine_sd#CDIUni

How to build a CDI extension

•Create a class implementing javax.enterprise.inject.spi.Extension !

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

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

containing extension classname

Page 47: CDI 1.1 university

@antoine_sd#CDIUni

public interface ProcessAnnotatedType<X> {! public AnnotatedType<X> getAnnotatedType(); public void setAnnotatedType(AnnotatedType<X> type); public void veto();} !public interface AnnotatedType<X> extends Annotated { public Class<X> getJavaClass(); public Set<AnnotatedConstructor<X>> getConstructors(); public Set<AnnotatedMethod<? super X>> getMethods(); public Set<AnnotatedField<? super X>> getFields();

For Instance

•We can observe ProcessAnnotatedType event to change a type information or force the container to ignore it.

Page 48: CDI 1.1 university

@antoine_sd#CDIUni

public class VetoEntity implements Extension { /** * Version CDI 1.0 */ public void vetoEntityLegacy(@Observes ProcessAnnotatedType<?> pat) { AnnotatedType at = pat.getAnnotatedType(); if (at.isAnnotationPresent(Entity.class)) pat.veto(); } /** * Version CDI 1.1+ */ public void vetoEntity(@Observes @WithAnnotations({Entity.class})ProcessAnnotatedType<?> pat) { pat.veto(); } }

Example 1 : Ignoring JPA entities

•A commonly admitted good practice is to avoid using JPA entities as CDI beans.

•This extension excludes entities from the set of types discovered by CDI.

Page 49: CDI 1.1 university

@antoine_sd#CDIUni

Things to know

•Once application is bootstrapped, the Bean Manager is in read only mode (no dynamic bean registration) !

•Extensions are launched during bootstrap and are based on CDI events !

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

Page 50: CDI 1.1 university

@antoine_sd#CDIUni

Example 2 : Add a Bean to CDI container

•There are many ways to add a bean to those automatically discovered at boot time !

•The easier method is to add an AnnotatedType to those discovered by CDI.

Page 51: CDI 1.1 university

@antoine_sd#CDIUni

public class HashMapAsBeanExtension implements Extension{ public void addHashMapAsAnnotatedType(@Observes BeforeBeanDiscovery bbd, BeanManager beanManager) { bbd.addAnnotatedType(beanManager.createAnnotatedType(HashMap.class)); } }

Adding a HashMap Bean 1/2

•Observing BeforeBeanDiscovery event we can add a new AnnotatedType based on HashMap classe.

Page 52: CDI 1.1 university

@antoine_sd#CDIUni

@Decorator@Priority(Interceptor.Priority.APPLICATION) public abstract class MapDecorator implements Map{ @Inject @Delegate Map delegate; @Override public Object put(Object key, Object value) { System.out.println("------- Putting Something in the Map -----------"); if ("key".equals(key)) { System.out.println("==== Not adding key key ======"); return null; } return delegate.put(key,value); }}

Adding a HashMap Bean 2/2

•Once created we can add CDI service on this HashMap bean. See that Decorator.

Page 53: CDI 1.1 university

@antoine_sd#CDIUni

CDI 1.1 Lifecycle

Before Bean Discovery

Process BeanProcess

Annotated Type

Scan Archive

Application Running

After Deployment Validation

Before Shutdown

Undeploy Application

Process Producer

After Bean Discovery

Process Injection Target

Process Observer Method

Process Injection

Point

Process Bean Attributes

After Type Discovery

événement unique

événements multiples

étape interne

Deployment starts

Bean eligibility

check

Page 54: CDI 1.1 university

@antoine_sd#CDIUni

Scheduler Extension1/4

•This extension example comes from Apache Deltaspike project. It’s a simplified version. !

•We want to be able to schedule jobs from by using annotation @Schedule.

Page 55: CDI 1.1 university

@antoine_sd#CDIUni

public class SchedulerExtension implements Extension { private List<Class> foundManagedJobClasses = new ArrayList<Class>(); private Scheduler scheduler; public <X> void findScheduledJobs(@Observes @WithAnnotations({ Scheduled.class }) ProcessAnnotatedType<X> pat) { Class<X> beanClass = pat.getAnnotatedType().getJavaClass(); this.foundManagedJobClasses.add(beanClass); } public <X> void scheduleJobs(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) { initScheduler(afterBeanDiscovery) List<String> foundJobNames = new ArrayList<String>(); for (Class jobClass : this.foundManagedJobClasses) { foundJobNames.add(jobClass.getSimpleName()); this.scheduler.registerNewJob(jobClass); } } public <X> void stopScheduler(@Observes BeforeShutdown beforeShutdown) { if (this.scheduler != null) { this.scheduler.stop(); this.scheduler = null; } } private void initScheduler(AfterBeanDiscovery afterBeanDiscovery) { this.scheduler = new QuartzScheduler(); this.scheduler.start(); } }

Scheduler extension 2/4 (extension code)

Page 56: CDI 1.1 university

@antoine_sd#CDIUni

Steps used in the extension

Before Bean Discovery

Process BeanProcess

Annotated Type

Scan Archive

Application Running

After Deployment Validation

Before Shutdown

Undeploy Application

Process Producer

After Bean Discovery

Process Injection Target

Process Observer Method

Process Injection

Point

Process Bean Attributes

After Type Discovery

occurs once

occurs on each elt

Internal step w/o hook

Deployment starts

Bean eligibility

check

Page 57: CDI 1.1 university

@antoine_sd#CDIUni

public interface Scheduler<T> { void start(); void stop(); void pauseJob(Class<? extends T> jobClass); void resumeJob(Class<? extends T> jobClass); void interruptJob(Class<? extends T> jobClass); boolean isExecutingJob(Class<? extends T> jobClass); void registerNewJob(Class<? extends T> jobClass); void startJobManually(Class<? extends T> jobClass); <S> S unwrap(Class<? extends S> schedulerClass);}

Scheduler Extension 3/4 (scheduler interface)

Page 58: CDI 1.1 university

@antoine_sd#CDIUni

@ApplicationScopedpublic class SchedulerProducer{ @Inject private SchedulerExtension schedulerExtension; @Produces @ApplicationScoped protected Scheduler produceScheduler() { return this.schedulerExtension.getScheduler(); }}

Scheduler extension 4/4 (scheduler producer)