Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's New in CDI 2.0 at JUG HH

Mark Paluch
November 08, 2018

What's New in CDI 2.0 at JUG HH

Slides to the talk I gave at JUG Hamburg.

Code: https://github.com/mp911de/cdi-2.0

Mark Paluch

November 08, 2018
Tweet

More Decks by Mark Paluch

Other Decks in Programming

Transcript

  1. CDI Timeline Dec 2009 CDI 1.0 (Java EE 6) Jun

    2013 CDI 1.1 (Java EE 7) Apr 2014 CDI 1.2 (M R) Sep 2014 CDI 2.0 (Kickoff) May 2017 CDI 2.0 (Release) Jul 2018 CDI 2.0 SP1 (Bugfix)
  2. Road towards JavaEE 8 • Java 8 baseline • Spec

    split: Core/SE/EE CDI Specification CDI Core CDI for Java SE CDI for Java EE
  3. Asynchronous Events • Notify event observers asynchronously • One or

    more different threads • Decoupled from synchronous events
  4. Asynchronous Events • Exceptions and Synchronization with CompletionStage • Active

    scopes: Request, Application • Custom scopes depend on the implementation
  5. Ordered Events • Add @Priority to event observers • Aids

    observer ordering • Total global order determined when the event is fired
  6. public void earlier(@Observes @Priority(2499) MyEvent event) {
 // yay! I'm

    first
 System.out.println("Notified before all other observers");
 }
 public void observer(@Observes MyEvent event) {
 System.out.println("Default priority");
 } public void later(@Observes @Priority(2501) MyEvent event) {
 System.out.println("Notified after all other observers");
 }
  7. public void earlier(@Observes @Priority(2499) MyEvent event) {
 // yay! I'm

    first
 System.out.println("Notified before all other observers");
 }
 public void observer(@Observes MyEvent event) {
 System.out.println("Default priority");
 } public void later(@Observes @Priority(2501) MyEvent event) {
 System.out.println("Notified after all other observers");
 }
  8. public void earlier(@Observes @Priority(2499) MyEvent event) {
 // yay! I'm

    first
 System.out.println("Notified before all other observers");
 }
 public void observer(@Observes MyEvent event) {
 System.out.println("Default priority");
 } public void later(@Observes @Priority(2501) MyEvent event) {
 System.out.println("Notified after all other observers");
 }
  9. public void earlier(@Observes @Priority(2499) MyEvent event) {
 // yay! I'm

    first
 System.out.println("Notified before all other observers");
 }
 public void observer(@Observes MyEvent event) {
 System.out.println("Default priority");
 } public void later(@Observes @Priority(2501) MyEvent event) {
 System.out.println("Notified after all other observers");
 }
  10. SeContainerInitializer initializer = SeContainerInitializer .newInstance()
 .disableDiscovery()
 .addBeanClasses(MyApp.class)
 .addPackages(MyOtherServices.class)
 .addExtensions(MyExtension.class);
 


    try (SeContainer container = initializer.initialize()) {
 
 MyApp service = container.select(MyService.class).get();
 service.runMyApplication();
 }
  11. SeContainerInitializer initializer = SeContainerInitializer .newInstance()
 .disableDiscovery()
 .addBeanClasses(MyApp.class)
 .addPackages(MyOtherServices.class)
 .addExtensions(MyExtension.class);
 


    try (SeContainer container = initializer.initialize()) {
 
 MyApp service = container.select(MyService.class).get();
 service.runMyApplication();
 }
  12. SeContainerInitializer initializer = SeContainerInitializer .newInstance()
 .disableDiscovery()
 .addBeanClasses(MyApp.class)
 .addPackages(MyOtherServices.class)
 .addExtensions(MyExtension.class);
 


    try (SeContainer container = initializer.initialize()) {
 
 MyApp service = container.select(MyService.class).get();
 service.runMyApplication();
 }
  13. SPI

  14. Meta-Data Builder API • Standardized API • CDI 1.0+: Very

    verbose to create • AnnotatedTypes, Beans, BeanAttributes, InjectionPoints, and ObserverMethods • Builder and Configurator-style
  15. public class MyExtension {
 public void afterBeanDiscovery( @Observes AfterBeanDiscovery event)

    {
 event.addBean() .beanClass(MyService.class) .scope(RequestScoped.class) .name("myservice"); } } Add a Bean
  16. public class MyExtension {
 public void afterBeanDiscovery( @Observes AfterBeanDiscovery event)

    {
 event.addBean() .beanClass(MyService.class) .scope(RequestScoped.class) .name("myservice"); } } Add a Bean
  17. public class MyExtension {
 public <T> void processAnnotatedType(@Observes @WithAnnotations(UseCase.class) ProcessAnnotatedType<T>

    pat) {
 
 pat.configureAnnotatedType() .methods() 
 .forEach(m -> m.add(CacheableLiteral.INSTANCE));
 } } Add annotation to Methods
  18. public class MyExtension {
 public <T> void processAnnotatedType(@Observes @WithAnnotations(UseCase.class) ProcessAnnotatedType<T>

    pat) {
 
 pat.configureAnnotatedType() .methods() 
 .forEach(m -> m.add(CacheableLiteral.INSTANCE));
 } } Add annotation to Methods
  19. AOP on produced Bean • CDI 1.0+: Interceptor applied to

    producer method • InterceptionFactory a possible answer @Produces 
 @Transactional
 public MyService produceService() {
 ... 
 }
  20. AOP on produced Bean @Produces
 @RequestScoped
 public MyService createTransactional(InterceptionFactory<MyService> ipf)

    {
 
 ipf.configure()
 .filterMethods(m -> m.getJavaMember().getName().equals("save"))
 .findFirst()
 .ifPresent(m -> m.add(new AnnotationLiteral<Transactional>() {}));
 
 return ipf.createInterceptedInstance(new MyService());
 }
  21. AOP on produced Bean @Produces
 @RequestScoped
 public MyService createTransactional(InterceptionFactory<MyService> ipf)

    {
 
 ipf.configure()
 .filterMethods(m -> m.getJavaMember().getName().equals("save"))
 .findFirst()
 .ifPresent(m -> m.add(new AnnotationLiteral<Transactional>() {}));
 
 return ipf.createInterceptedInstance(new MyService());
 }
  22. Proxy injection @Inject @Named("myBean")
 private ConcurrentHashMap<String, String> myService; /** Implementation

    for put and putIfAbsent */
 final V putVal(K key, V value, boolean onlyIfAbsent)
  23. Normal-scoped beans • Must be proxyable • accessible, no-args constructor

    • non-final classes • accessible, non-final instance methods
  24. Enabling proxying @Produces
 @RequestScoped
 public MyService createTransactional(InterceptionFactory<MyService> factory) {
 


    factory.ignoreFinalMethods(); 
 return factory.createInterceptedInstance(new MyService());
 }
  25. Enabling proxying @Produces
 @RequestScoped
 public MyService createTransactional(InterceptionFactory<MyService> factory) {
 


    factory.ignoreFinalMethods(); 
 return factory.createInterceptedInstance(new MyService());
 }
  26. Get in touch Web: cdi-spec.org Mailing list: [email protected] IRC: irc://freenode.net/#cdi-dev

    Twitter: @cdispec // @mp911de Github: github.com/cdi-spec CDI 2.0 JCP page: jcp.org/en/jsr/summary?id=365