Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Contexts and Dependency Injection (CDI)                                                  in JEE 6                         ...
About meA Java developer (now interested in Scala as well ...)Active member in JUG ChennaiCreator & Co-Founder of scalaxia...
AgendaWhat is & Why DI ?What is CDIFeatures of CDIAdvantages of CDIInjecting Beans (with some EJB tit-bits !!!)Types of In...
What is Dependency Injection        PHONE ==> SIM ==> TOWER
Why Dependency Injectionuser just relies (on)ly the Phonestreamlining dependent objects injection by“Dont call your servic...
What is CDI JSR 299 Provides a unifying Dependency Injection and contextual lifecycle model for Java EE A completely ne...
Advantages of CDIDecouple server and client   – Using well-defined types and qualifiers   – Allows server implementations ...
Injecting BeansCDI is all about DICDI makes use of annotations3 Types of injection points    Field    Method ParameterMe...
Basic CDI Beanspublic interface Greeting {    public String greet(String greet);}@Defaultpublic class PlainGreetingCDI imp...
Qualifiers – Built in & custom quali-                     fierspublic interface CurrencyConverter {    public Double conve...
Qualifier in CDIpublic interface CurrencyConverter {    public Double convert(Double howMuchToConvert);}@Qualifier@Retenti...
Qualifier in CDI (Contd)// client code@Inject @CurrencyTypeprivate CurrencyConverter def_converter;@Inject @CurrencyType(t...
Scoped Beans – Request Scope@RequestScopedpublic class RequestScopedCurrencyConverterCDI {    private double currValue;   ...
Scoped Beans – Request Scope@Statelesspublic class ScopedCurrencyConverterEJB {    @Inject private RequestScopedCurrencyCo...
Scoped Beans – Request Scope// client code@EJBprivate ScopedCurrencyConverterEJB converterEJB;converterEJB.getConvertedVal...
Scoped Beans – Application Scope@ApplicationScopedpublic class ApplicationScopedCDI {    private double currValue;   publi...
Scoped Beans – Application Scope@ApplicationScopedpublic class ApplicationScopedCDI {    private double currValue;   publi...
CDI optionsReference ImplementationsJBoss Weldhttp://seamframework.org/WeldApache OpenWebBeanshttp://openwebbeans.apache.o...
How do I learn / adoptAll code presented here are available underhttps://github.com/prassee/JTechDayCDIExamplesYou are wel...
Q&A
Upcoming SlideShare
Loading in …5
×

CDI in JEE6

2,026 views

Published on

this slide has been presented in JUGChennai JTechDay held on Sep 15 2012

Published in: Technology
  • Be the first to comment

  • Be the first to like this

CDI in JEE6

  1. 1. Contexts and Dependency Injection (CDI) in JEE 6 Presenter Prasanna Kumar.SJUGChennai JTech Day Sep12
  2. 2. About meA Java developer (now interested in Scala as well ...)Active member in JUG ChennaiCreator & Co-Founder of scalaxia.com (a site which lists out the popular tweets about scala)Contributed for ScalaFX projectContactMail me prassee.sathian@gmail.comFollow me @prasonscalaI blog @ prasonscala@tumblr.com
  3. 3. AgendaWhat is & Why DI ?What is CDIFeatures of CDIAdvantages of CDIInjecting Beans (with some EJB tit-bits !!!)Types of InjectionQualifiers – Built in & custom qualifiersScope – Types of ScopesCDI options....Questions
  4. 4. What is Dependency Injection PHONE ==> SIM ==> TOWER
  5. 5. Why Dependency Injectionuser just relies (on)ly the Phonestreamlining dependent objects injection by“Dont call your service,let service call you”Achieve loose coupling of objectsWell proven practise
  6. 6. What is CDI JSR 299 Provides a unifying Dependency Injection and contextual lifecycle model for Java EE A completely new, richer dependency management model Type-safe dependency injection Designed for use with stateful objects (scoped objects)
  7. 7. Advantages of CDIDecouple server and client – Using well-defined types and qualifiers – Allows server implementations to varyDecouple lifecycle of collaborating components – Automatic contextual lifecycle management by the CDI runtimeDecouple orthogonal concerns (AOP) – Interceptors & DecoratorsNO XML !!!!Can be used in both JAVA SE and JAVA EE applications
  8. 8. Injecting BeansCDI is all about DICDI makes use of annotations3 Types of injection points Field Method ParameterMethods can be Constructor Initializer Setter Producer Observer
  9. 9. Basic CDI Beanspublic interface Greeting { public String greet(String greet);}@Defaultpublic class PlainGreetingCDI implements Greeting { public String greet(String greet) { return "greetings to " + greet; }}// client code@Injectprivate Greeting greeting;public String sayGreeting(String whomToGreet) { return greeting.greet(whomToGreet);}
  10. 10. Qualifiers – Built in & custom quali- fierspublic interface CurrencyConverter { public Double convert(Double howMuchToConvert);}@Qualifier@Retention(RUNTIME)@Target({ TYPE, METHOD, FIELD, PARAMETER })public @interface CurrencyType { VariousCurrencies type() default VariousCurrencies.DOLLAR;}@CurrencyType(type = VariousCurrencies.DOLLAR)public class DollarConverter implements CurrencyConverter { @Override public Double convert(Double howMuchToConvert) { return howMuchToConvert * 55; }}public enum VariousCurrencies { DOLLAR,EURO,POUNDS }
  11. 11. Qualifier in CDIpublic interface CurrencyConverter { public Double convert(Double howMuchToConvert);}@Qualifier@Retention(RUNTIME)@Target({ TYPE, METHOD, FIELD, PARAMETER })public @interface CurrencyType { VariousCurrencies type() default VariousCurrencies.DOLLAR;}@CurrencyType(type = VariousCurrencies.DOLLAR)public class DollarConverter implements CurrencyConverter { @Override public Double convert(Double howMuchToConvert) { return howMuchToConvert * 55; }}public enum VariousCurrencies { DOLLAR,EURO,POUNDS }
  12. 12. Qualifier in CDI (Contd)// client code@Inject @CurrencyTypeprivate CurrencyConverter def_converter;@Inject @CurrencyType(type=VariousCurrencies.EURO)private CurrencyConverter euroConverter;
  13. 13. Scoped Beans – Request Scope@RequestScopedpublic class RequestScopedCurrencyConverterCDI { private double currValue; @Inject @CurrencyType(type=VariousCurrencies.DOLLAR) private CurrencyConverter currencyConverter; // setters and getter for currencyConverter public double convert() { return currencyConverter.convert(currValue); }}
  14. 14. Scoped Beans – Request Scope@Statelesspublic class ScopedCurrencyConverterEJB { @Inject private RequestScopedCurrencyConverterCDI currencyConverterCDI @EJB private FormattedCurrencyConverter formattedCurrencyConverter; public String getConvertedValue(double valueToConvert) { currencyConverterCDI.setCurrValue(valueToConvert); return formattedCurrencyConverter.getFormattedCurrency(); }}@Stateless @LocalBeanpublic class FormattedCurrencyConverter { @Inject private RequestScopedCurrencyConverterCDI currencyConverter; public String getFormattedCurrency() { return "The converted "+ currencyConverter.getCurrValue() +" to " +currencyConverter.convert(); }}
  15. 15. Scoped Beans – Request Scope// client code@EJBprivate ScopedCurrencyConverterEJB converterEJB;converterEJB.getConvertedValue(2) // "The converted 2.0 to 110.0"
  16. 16. Scoped Beans – Application Scope@ApplicationScopedpublic class ApplicationScopedCDI { private double currValue; public void setCurrValue(double currValue) { this.currValue = currValue; } public double getCurrValue() { return currValue; }}// client codepublic void setValueToConvert(double value) { applicationScopedCurrencyConverterCDI.setCurrValue(2); } public double getValueToConvert() { return applicationScopedCurrencyConverterCDI.getCurrValue(); }
  17. 17. Scoped Beans – Application Scope@ApplicationScopedpublic class ApplicationScopedCDI { private double currValue; public void setCurrValue(double currValue) { this.currValue = currValue; } public double getCurrValue() { return currValue; }}// client codepublic void setValueToConvert(double value) { applicationScopedCurrencyConverterCDI.setCurrValue(2); } public double getValueToConvert() { return applicationScopedCurrencyConverterCDI.getCurrValue(); }
  18. 18. CDI optionsReference ImplementationsJBoss Weldhttp://seamframework.org/WeldApache OpenWebBeanshttp://openwebbeans.apache.org/owb/indexhtmlCauchohttp://www.caucho.com/Supported App ContainersJboss AS 7,GlassFish 3.1x ,Caucho, TomEE !!! &all JEE6 containers
  19. 19. How do I learn / adoptAll code presented here are available underhttps://github.com/prassee/JTechDayCDIExamplesYou are welcome to contribute your own examples -(Shameless sales pitch !!!!)Project using CDIAGORAVA project - http://agorava.org/Feel free to contributeJavaPassion sitehttp://javapassion.com/jugtalksTomEE examples Trunkhttp://tomee.apache.org/examples-trunk/index.html
  20. 20. Q&A

×