CDI in JEE6
Upcoming SlideShare
Loading in...5
×
 

CDI in JEE6

on

  • 2,204 views

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

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

Statistics

Views

Total Views
2,204
Views on SlideShare
1,873
Embed Views
331

Actions

Likes
0
Downloads
37
Comments
0

13 Embeds 331

http://prasonscala.tumblr.com 226
http://prassee.com 37
http://www.google.com 19
http://luahoany.tumblr.com 10
http://www.bing.com 9
http://www.soso.com 9
http://search.yahoo.com 8
http://yandex.ru 8
http://prasonjava.tumblr.com 1
http://reginaldgo.tumblr.com 1
http://www.docseek.net 1
http://blog.prassee.me 1
http://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

CDI in JEE6 CDI in JEE6 Presentation Transcript

  • Contexts and Dependency Injection (CDI) in JEE 6 Presenter Prasanna Kumar.SJUGChennai JTech Day Sep12
  • 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
  • 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
  • What is Dependency Injection PHONE ==> SIM ==> TOWER
  • 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
  • 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)
  • 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
  • Injecting BeansCDI is all about DICDI makes use of annotations3 Types of injection points Field Method ParameterMethods can be Constructor Initializer Setter Producer Observer
  • 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);}
  • 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 }
  • 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 }
  • Qualifier in CDI (Contd)// client code@Inject @CurrencyTypeprivate CurrencyConverter def_converter;@Inject @CurrencyType(type=VariousCurrencies.EURO)private CurrencyConverter euroConverter;
  • 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); }}
  • 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(); }}
  • Scoped Beans – Request Scope// client code@EJBprivate ScopedCurrencyConverterEJB converterEJB;converterEJB.getConvertedValue(2) // "The converted 2.0 to 110.0"
  • 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(); }
  • 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(); }
  • 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
  • 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
  • Q&A