Java EE6 CDI

17,308 views

Published on

Presentation on Java EE 6 CDI with a twist of bashing Spring. Held at Callista Developers Conference 2010.

Published in: Technology, Education
5 Comments
18 Likes
Statistics
Notes
  • http://dbmanagement.info/Tutorials/Java.htm
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Very useful info
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • I'll give you all your points regarding facts on Spring. The general point I want to make, is that it isn't as obvious as it has been, that Spring simplifies development over Java EE, when speaking for the average in-house application developer. I fully agree that Spring has far more complete architectural foundation, in terms of bootstrapping etc.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Valuable information on CDI but it would have made a better impression without the obviously wrong info in the first 3rd of the slidedeck:

    Slide 2: Besides the fact that the timelines are plain wrong (all the libraries that according to your graphics have been introduced in 2010 we're there from the beginning and there hasn't been any new modules in 2010 as Spring 3 came out in 2009), it's obvious that you took a list of modules available from the Subversion repository and blame it for being what? Comprehensive? So the time fact is wrong, you include modules purely needed for build, you include a lot of modules that approach APIs or problems JavaEE never cared about just to create the impression of Spring being bloated?

    Slide 3/4: Just note that there is a BeanDefinition abstraction inside Spring that completely decouples the factory's internals from the way it is configured. That's there since the early days. Spring *is* designed to use other config formats than XML (e.g. annotations, programatic or even properties files if you need). I think it is best practice to verify such bold statements if you like to place 'em that prominently ;).

    Regarding the need for best practices. There are developers out there that need choice, that don't want to turn off their brains because a technology implies there is one right way to achieve something. So IMHO choice is something valuable, as it raises the chance the technology as applicable to my problem space.

    Slide 5: So simply consider the Java EE toolsuite to be Netbeans provided by Sun. Where's the difference?

    So I thinks it's a pity to wash-out the great CDI overview with such a daring intro that everyone who has a little insight into Spring will recognize to be wrong anyway.

    Regards,
    Ollie
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • SlideShare does not seem to support KeyNote very well. Fonts are scaled so that content is lost.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
17,308
On SlideShare
0
From Embeds
0
Number of Embeds
263
Actions
Shares
0
Downloads
0
Comments
5
Likes
18
Embeds 0
No embeds

No notes for slide
  • Säg att ultimata beviset för att en “lättviktig” miljö gubbat till sig, är att det behövs en specialpaketerad eclipse-distribution.
  • Spring är definitivt där.
    Det är en stor plattform i sig själv. Vi behöver något lättviktigare!
  • Ta av ryggsäcken!
  • Nämn tydligt att det handlar om annoteringsbaserad komponentmodel!
    Det finns inget standardiserat sätt konfigurera “bönor” med XML i CDI.
  • Det är ju trots allt enhetsestning / TDD som är drivkraften för DI!

    Gå långsamt igenom projektet.
    Visa beans.xml.

    Dema usecase 1 - brytpunkt i Service, visa att DAO injecerats.
  • Qualifiers:
    - Förklara
    - Öppna Qualifer-annoteringen

    Producers
    - Klargör att detta blir extra mycket använt när man inte har XML-konfigurering. En klass kan ju bara ha en annotering, men många bean-konfigar (i Spring)
    - Visa JmsTemplates och förklara först producer methods och sedan producer fields.
  • Ta på den lilla ryggsäcken!
    Kommentera Spring integrationstestning!
    Gå igenom uppsättningen. med Test-EJB.
    - Visa testcase.
    - Visa Resources-klassen (producer som arv i Java EE 5)
    - Visa Dao och sätt brytpunkt där, visa att EntityManager har injecerats.
  • Use-case 3: Systematically handle cross-cutting concerns ("AOP")

    Rational:
    - Toggling technical features like transaction handling, logging, etc without editing a lot of classes

    Charactersistics
    - One bean per class (of technical feature)

    Options for annotation based DI
    - Integration of a full-fledged point-cut language (e.g. aspectj)
    - Injecting proxy handlers (interceptor) based on annotations
    - Injecting type-compatible “pre-preprocessor” (decorator) based on annotations

    Dema:
    - Visa koden
    - Kör koden och visa konsolen.
  • Brokering of events between beans

    - Beskriv caset
    - Visa koden
    - kommentera att det är typsäkert, lätviktigt och flexibelt
  • Use-case 5: Contextual services

    Gå igenom bilden. Ingen demo.
  • SPI för:
    - metaprogrammering
    - integrera CDI-implementationer i olika containers (t.ex. Weld i Tomcat)
    - Att göra kundspecifika tillägg till programmeringsmodellen

    Demo!
    - Visa TemplateProducern
    - Kör koden
    - Ändra i filen
    - Visa att testet fallerar
  • Java EE6 CDI

    1. 1. The  New  Java  EE CDI  -­‐  the  re-­‐invented  component  model for  Java  EE  6.  BeAer  than  Spring? Johan  Eltes  |  johan.eltes@callistaenterprise.se  |  Cadec  2010-­‐01-­‐20 onsdag 27 januari 2010 1
    2. 2. Once  upon  a  time  Spring  was  comprehensible… 1 0 20 0 8 20 0 4 20 2 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 2
    3. 3. Are  you  a  Java  or  XML  developer? 3 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 3
    4. 4. Spring  wasn’t  built  bottom  up  for  noxml Spring  wasn’t  built  bo?om  up  for  noXML You  need  to  understand  what  can  be  done  using  JavaConfig  /  annotaFons  and  when  to  step   down  to  XML. There  are  so  many  options  in  Spring  that  you  need  a  100  page  company-­best-­practice  guide  so  that   developers  can  read  each  others  code. 4 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 4
    5. 5. Complex  tools  require  tool  suites 5 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 5
    6. 6. Meanwhile,  Java  EE  is  shrinking... o Rile  Pr eb  6  W a  EE Jav E va  E l l  Ja Fu 6 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 6
    7. 7. ...and  moving  to  a  very  slick  DI-­based  component   model:  CDI CDI  requires  support   from  an  EJB  3.1  Light-­‐ container  for  EJB-­‐ CDI  container  (injecKon,  aop,  contexts) specific  capabiliKes   (transatcions,   security) JSF/Facelets Managed  Bean Special  case:  EJB  Session  and  Singleton CDI  container   CDI  container   will  offer  the   will  resolve   bean  for   dependency injecKon   Servlets MDBs WS CDI  consolidates  JSR  250  (common  annota7ons),  JSR-­‐330  ( javax.inject)  and  “EJB  3.1   light”  into  a  unified  “POJO”  component  model. 7 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 7
    8. 8. So,  let’s  have  a  look  at  CDI! All demo code for the use-cases are available for public checkout at this svn repo: • Use-­‐cases  for  CDI https://svn.callistaenterprise.se/public/Cadec/Cadec2010/cdi/trunk – UC  1:  Basic  DI  for  Unit  tesKng – UC  2:  Factories  and  qualifiers – UC  3:  IntegraKon  tesKng  in  JUnit – UC  4:  AOP  with  interceptors – UC  5:  Loose  coupling  with  events – UC  6:  Handling  of  scopes – UC  7:  Extensions AnnotaKons,  annotaKons,  annotaKons...  @Inject,  @Produces,  @Qualifer,  @AlternaKve,  @Stateless,  @ConversaKon 8 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 8
    9. 9. UC  1:  Basic  DI  for  Unit  testing Interface:  OrderDao Class OracleOrderDaoImpl extends Class  OrderServiceImpl  { OrderDao @Inject OrderDao  orderDao; ... ? } Class MySqlOrderDaoImpl extends OrderDao Class MockOrderDaoImpl extends OrderDao @Inject  is  by  default  auto-­‐injecKon  by  type 9 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 9
    10. 10. 9 UC  2:  Factories  and  qualiRiers Instance of Class JmsTemplate: @DestClass(ERROR)   Interface:   receiveTimeout: 3000 defaultDestinationName: “error.queue” JmsTemplate ... Instance of Class JmsTemplate: @  DestClass(LOG)   Class  OrderServiceImpl  { receiveTimeout: 100 @Inject defaultDestinationName: “log.queue” JmsTemplate  @JTQualifer(ERROR) ... errorDestTemplate; ? ... Instance of } Class JmsTemplate: @Named(“service1”) receiveTimeout: 4000 defaultDestinationName: “service1” ... Class JmsTemplateFactory    { @Producer  @DestClass(ERROR)  JmsTemplate  getErrorDestTemplate()  {} @Producer  @DestClass(LOG)  JmsTemplate  getLogDestTemplate()  {}   @Producer  @Named(“invoice”)  JmsTemplate  getInvoiceDestTemplate()  {} } With  @Produces  we  can  create  mulKple  beans  of  the  same  type With  @Qualifier-­‐annotaKons,  we  select  which  bean  to  inject 10 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 10
    11. 11. UC  3:  Integration  testing  with  JUnit How  much  weight  has  EJB  lost??  Demo: JUnit  TestCase-­‐>TestEjb-­‐>Ejb-­‐>Dao-­‐>JPA ...with  embeddable  EJB  container 11 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 11
    12. 12. 9 UC  4:  Cross-­cutting  concerns Class  OrderServiceImpl  { Interface:  OrderDao @Inject @Trace OrderDao  orderDao; Class OracleOrderDaoImpl extends OrderDao ... { } Order save(Order order) { } } @Trace @Interceptor public class TraceLogInterceptor { @AroundInvoke public Object trace(InvocationContext ctx) throws Exception { ... } } Sample:  Trace  entry  and  exit  of  method  invocaKons  in  all  classes  annotated  with  @Trace 12 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 12
    13. 13. UC  5:  Events Class CustomerFormBean { @Inject Class CustomerServiceImpl { CustomerService cs; @Inject @Deleted Event<Customer> public void delete(Customer deletedEvent; c) { cs.delete(c) public void delete(Customer c) } { } ... “CDI Event broker” deletedEvent.fire(c); } Class  OrderServiceImpl  { } @Inject  OrderDao  od; public  void  validateDelete(@Observes  @Deleted  Customer  c)  { if  (od.hasOrderForCustomer(c))  throw  new  VetoExcepKon(); } } Order  service  listens  to  customer  deleKon  events  to  prevent  deleKon  of  customers  with  orders. 13 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 13
    14. 14. UC  6:  Declarative  scope  management • ejb  Session CDI handles the scoping / context aspects of EJBs as • ejb  Singleton well (without an EJB container) • ApplicaKonScoped • RequestScoped • SessionScoped • ConversaKonScoped • AddiKonal  scopes    may  be  added  via  extenKon  api  (“SPI”) Use-­‐case  5:    Contextual  services 14 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 14
    15. 15. 9 UC  7:  Extentions  -­  Sample:  conRig  Rile Instance of Class JmsTemplate: @DestClass(ERROR)   Interface:   receiveTimeout: 3000 defaultDestinationName: “error.queue” JmsTemplate ... Instance of Class JmsTemplate: @  DestClass(LOG)   Class  OrderServiceImpl  { receiveTimeout: 100 @Inject defaultDestinationName: “log.queue” JmsTemplate  @JTQualifer(ERROR) ... errorDestTemplate; ? ... Instance of } Class JmsTemplate: @Named(“invoice”) receiveTimeout: 4000 defaultDestinationName: “invoice.queue” ... Class JmsTemplateFactory    { @Producer  @DestClass(ERROR)  JmsTemplate  getErrorDestTemplate()  {} @Producer  @DestClass(LOG)  JmsTemplate  getLogDestTemplate()  {}   @Producer  @Named(“invoice”)  JmsTemplate  getInvoiceDestTemplate()  {} } Sample:  Extension  for  file-­‐based  configuraKon  of  @Producers 15 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 15
    16. 16. Is  Java  EE  6  and  CDI  for  me? • Why? – Java  EE  development  is  FUN  (producKve)  again! – It  is  a  neat,  simple  model – Zero  config  /  overhead  /  layers  if  you  want  to  start  simple – Do  we  sKll  need  “help”  from  Spring? • When? – JBoss,  Oracle  and  Sun  seems  to  be  there  “soon” – Likely  to  get  OS-­‐extensions  that  bring  it  to  Tomcat  very  soon • Who? – Very  good  news  for  no-­‐floss-­‐shops  :) – OpKon  for  building  up  Spring  competence  -­‐  Java  EE  6  Web  Profile  is  likely  an  easier  start 16 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 16
    17. 17. Thank’s  for  listening! 17 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 17
    18. 18. Credits • Image  of  feathers: – hAp://askabiologist.asu.edu 18 CDI  -­‐  the  re-­‐invented  component  model  for  Java  EE  6 ©  2010  Callista  Enterprise  |  www.callistaenterprise.se onsdag 27 januari 2010 18

    ×