SlideShare ist ein Scribd-Unternehmen logo
1 von 53
Contexts And Dependency Injection In The Java EE 6 Ecosystem ,[object Object]
Software Engineering Manager
[object Object]
How we got here ? ,[object Object],[object Object],[object Object],[object Object]
More stateful and less XML-centric than Spring
More web and enterprise-capable than Guice ,[object Object],[object Object]
CDI Key Concepts ,[object Object]
Strong typing, Loose coupling ,[object Object],[object Object]
Works with Java EE modular and component architecture ,[object Object],[object Object]
Bridge EJB (transactional tier) and JSF (presentation tier) in the platform
Loose Coupling ,[object Object]
Producer – runtime polymorphism
Interceptors – decouple technical and business concerns
Decorators – decouple business concerns
Event notifications – decouple event producer and consumers
Contextual lifecycle management decouples bean lifecycles
Strong Typing ,[object Object]
Lift the semantic level of code ,[object Object]
@Asynchronous  instead of  asyncPaymentProcessor ,[object Object]
What is a CDI managed bean ? ,[object Object],[object Object]
Concrete class or decorated with @Decorator
Constructor with no parameters or a constructor annotated with @Inject ,[object Object]
How to configure ? There is none! ,[object Object]
“ beans.xml” ,[object Object]
META-INF of JAR
META-INF of directory in the classpath ,[object Object]
Injection Points ,[object Object]
0 or more qualifiers
Type @Inject @LoggedIn User user @Inject   @LoggedIn   User  user Request Injection What ? (Type) Which one ? (Qualifier)
Basic – Sample Code public interface Greeting {   public String greet(String name); } @Stateless public class GreetingService {   @Inject Greeting greeting;   public String greet(String name) {   return greeting.greet(name);   } } public class SimpleGreeting  implements Greeting  {   public String greet(String name) {   return “Hello “ + name;   } }
Field and Method Injection public class CheckoutHandler { @Inject @LoggedIn User user; @Inject  PaymentProcessor processor; @Inject void setShoppingCart(@Default Cart cart) { … } }
Constructor Injection public class CheckoutHandler { @Inject CheckoutHandler(@LoggedIn User user, PaymentProcessor processor, Cart cart) { ... } } ,[object Object]
Makes the bean immutable
Qualifier ,[object Object]
No direct dependency to any particular implementation (of many)
Built-in qualifiers ,[object Object]
Qualifier – Sample Code @Fancy public class FancyGreeting implements Greeting {   public String sayHello(String name) {   return “Nice to meet you, hello “ + name;   } } @Stateless public class GreetingService {   @Inject  @Fancy  Greeting greeting;   public String sayHello(String name) {   return greeting.sayHello(name);   } } @Qualifier @Retention(RUNTIME) @Target({METHOD, FIELD, PARAMETER, TYPE}) public @interface Fancy { }
Multiple Qualifiers and Qualifiers with Arguments public class CheckoutHandler { @Inject CheckoutHandler(@LoggedIn User user, @Reliable @PayBy(CREDIT_CARD) PaymentProcessor processor , @Default Cart cart) { ... } }
Typesafe Resolution ,[object Object]
@Qualifier ,  @Alternative ,[object Object]
Explicitly enable an  @Alternative  bean using beans.xml
Make sure it is in the classpath ,[object Object],[object Object]
Disable one of the beans using  @Alternative
Move one implementation out of classpath
Client Proxies ,[object Object],@ApplicationScoped public class UserService {   @Inject User user;   public void doSomething() {   user.setMessage("...");   // some other stuff   user.getMessage();   } } @RequestScoped public class User {   private String message;   // getter & setter }  “ User” proxy is injected CDI will supply a different “User” for each request
Types of Scopes ,[object Object]
Everywhere:  @ApplicationScoped ,  @RequestScoped
Web app:  @SessionScoped  (serializable)
JSF app:  @ConversationScoped  (serializable) ,[object Object],[object Object]
Typically defined by framework authors
@ConversationScope ,[object Object]
Unlike – demarcated explicitly by the application, holds state with a particular browser tab in a JSF application ,[object Object],public class ShoppingService {   @Inject Conversation conv;   public void startShopping() {   conv.begin();   }   . . .   public void checkOut() {   conv.end();   } }
@New Qualifier ,[object Object],@ConversationScoped public class Calculator { . . .   } public class PaymentCalc { @Inject Calculator calculator; @Inject  @New  Calculator newCalculator; }

Weitere ähnliche Inhalte

Was ist angesagt?

Hibernate complete Training
Hibernate complete TrainingHibernate complete Training
Hibernate complete Training
sourabh aggarwal
 
Ejb - september 2006
Ejb  - september 2006Ejb  - september 2006
Ejb - september 2006
achraf_ing
 
Spring 3: What's New
Spring 3: What's NewSpring 3: What's New
Spring 3: What's New
Ted Pennings
 

Was ist angesagt? (20)

Hibernate complete Training
Hibernate complete TrainingHibernate complete Training
Hibernate complete Training
 
Hibernate II
Hibernate IIHibernate II
Hibernate II
 
JPA 2.0
JPA 2.0JPA 2.0
JPA 2.0
 
JPA Best Practices
JPA Best PracticesJPA Best Practices
JPA Best Practices
 
JPA For Beginner's
JPA For Beginner'sJPA For Beginner's
JPA For Beginner's
 
TY.BSc.IT Java QB U4
TY.BSc.IT Java QB U4TY.BSc.IT Java QB U4
TY.BSc.IT Java QB U4
 
JSP Technology II
JSP Technology IIJSP Technology II
JSP Technology II
 
Introducing Application Context - from the PL/SQL Potpourri
Introducing Application Context - from the PL/SQL PotpourriIntroducing Application Context - from the PL/SQL Potpourri
Introducing Application Context - from the PL/SQL Potpourri
 
Spring Framework-II
Spring Framework-IISpring Framework-II
Spring Framework-II
 
Hibernate III
Hibernate IIIHibernate III
Hibernate III
 
Spring Framework - III
Spring Framework - IIISpring Framework - III
Spring Framework - III
 
Hibernate I
Hibernate IHibernate I
Hibernate I
 
Jpa
JpaJpa
Jpa
 
Ejb - september 2006
Ejb  - september 2006Ejb  - september 2006
Ejb - september 2006
 
JavaEE Security
JavaEE SecurityJavaEE Security
JavaEE Security
 
TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3
 
TY.BSc.IT Java QB U1
TY.BSc.IT Java QB U1TY.BSc.IT Java QB U1
TY.BSc.IT Java QB U1
 
Spring 3: What's New
Spring 3: What's NewSpring 3: What's New
Spring 3: What's New
 
TY.BSc.IT Java QB U5&6
TY.BSc.IT Java QB U5&6TY.BSc.IT Java QB U5&6
TY.BSc.IT Java QB U5&6
 
MongoDB Stitch Tutorial
MongoDB Stitch TutorialMongoDB Stitch Tutorial
MongoDB Stitch Tutorial
 

Ähnlich wie CDI @javaonehyderabad

Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6
Ray Ploski
 

Ähnlich wie CDI @javaonehyderabad (20)

Spark IT 2011 - Context & Dependency Injection in the Java EE 6 Ecosystem
Spark IT 2011 - Context & Dependency Injection in the Java EE 6 EcosystemSpark IT 2011 - Context & Dependency Injection in the Java EE 6 Ecosystem
Spark IT 2011 - Context & Dependency Injection in the Java EE 6 Ecosystem
 
Using Contexts & Dependency Injection in the Java EE 6 Platform
Using Contexts & Dependency Injection in the Java EE 6 PlatformUsing Contexts & Dependency Injection in the Java EE 6 Platform
Using Contexts & Dependency Injection in the Java EE 6 Platform
 
JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
JavaCro'14 - Building interactive web applications with Vaadin – Peter LehtoJavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
JavaCro'14 - Building interactive web applications with Vaadin – Peter Lehto
 
RESTEasy
RESTEasyRESTEasy
RESTEasy
 
Domain-Driven Design with SeedStack
Domain-Driven Design with SeedStackDomain-Driven Design with SeedStack
Domain-Driven Design with SeedStack
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasy
 
OpenWebBeans/Web Beans
OpenWebBeans/Web BeansOpenWebBeans/Web Beans
OpenWebBeans/Web Beans
 
Context and Dependency Injection
Context and Dependency InjectionContext and Dependency Injection
Context and Dependency Injection
 
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
 
Dependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony ContainerDependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony Container
 
CDI in JEE6
CDI in JEE6CDI in JEE6
CDI in JEE6
 
Hexagonal architecture in PHP
Hexagonal architecture in PHPHexagonal architecture in PHP
Hexagonal architecture in PHP
 
Learning To Run - XPages for Lotus Notes Client Developers
Learning To Run - XPages for Lotus Notes Client DevelopersLearning To Run - XPages for Lotus Notes Client Developers
Learning To Run - XPages for Lotus Notes Client Developers
 
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
 
Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6
 
softshake 2014 - Java EE
softshake 2014 - Java EEsoftshake 2014 - Java EE
softshake 2014 - Java EE
 
Get Hip with JHipster - GIDS 2019
Get Hip with JHipster - GIDS 2019Get Hip with JHipster - GIDS 2019
Get Hip with JHipster - GIDS 2019
 
Codemotion appengine
Codemotion appengineCodemotion appengine
Codemotion appengine
 
Security enforcement of Java Microservices with Apiman & Keycloak
Security enforcement of Java Microservices with Apiman & KeycloakSecurity enforcement of Java Microservices with Apiman & Keycloak
Security enforcement of Java Microservices with Apiman & Keycloak
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenches
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

CDI @javaonehyderabad

Hinweis der Redaktion

  1. With Guic CDI is Java EE oriented, integrates well with EL Portable extensions that run very well with other Java EE 6 compliant containers. Spring, Guice, and Seam evolved to support corner cases, CDI has been designe to support these cases.
  2. Strong typing, loose coupling - A bean specifies only the type and semantics of other beans it depends upon, and that too using typing information available in the the Java object model, and no String-based identifiers. It need not be aware of the actual lifecycle, concrete implementation, threading model or other clients of any bean it interacts with. Even better, the concrete implementation, lifecycle and threading model of a bean may vary according to the deployment scenario, without affecting any client. This loose-coupling makes your code easier to maintain. Events, interceptors and decorators enhance the loose-coupling inherent in this model: • event notifications decouple event producers from event consumers, • interceptors decouple technical concerns from business logic, and • decorators allow business concerns to be compartmentalized.
  3. These techniques serve to enable loose coupling of client and server. The client is no longer tightly bound to an implementation of an interface, nor is it required to manage the lifecycle of the implementation. This approach lets stateful objects interact as if they were services. Loose coupling makes a system more dynamic. The system can respond to change in a well-defined manner. In the past, frameworks that attempted to provide the facilities listed above invariably did it by sacrificing type safety (most notably by using XML descriptors). CDI is the first technology, and certainly the first specification in the Java EE platform, that achieves this level of loose coupling in a typesafe way.
  4. These techniques serve to enable loose coupling of client and server. The client is no longer tightly bound to an implementation of an interface, nor is it required to manage the lifecycle of the implementation. This approach lets stateful objects interact as if they were services. Loose coupling makes a system more dynamic. The system can respond to change in a well-defined manner. In the past, frameworks that attempted to provide the facilities listed above invariably did it by sacrificing type safety (most notably by using XML descriptors). CDI is the first technology, and certainly the first specification in the Java EE platform, that achieves this level of loose coupling in a typesafe way.
  5. SimpleGreeting is created when GreetingService is created and destroyed alongwith it.
  6. For example, producer methods let us: expose a JPA entity as a bean, expose any JDK class as a bean, define multiple beans, with different scopes or initialization, for the same implementation class, or vary the implementation of a bean type at runtime.
  7. Observers and Producers are completely decoupled from each other and thus providing an additional level of loose coupling. Observers can specify a combination of “selectors” to narrow the set of event notifications they will receive.
  8. Observers and Producers are completely decoupled from each other and thus providing an additional level of loose coupling. Observers can specify a combination of “selectors” to narrow the set of event notifications they will receive.