SlideShare una empresa de Scribd logo
1 de 42
Descargar para leer sin conexión
<Insert Picture Here>




Contexts and Dependency Injection for Java EE
Jérome Dochez
GlassFish Architect
The following is intended to outline our general product
direction. It is intended for information purposes, and
may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or
functionality, and should not be relied upon in making
purchasing decisions.

The development, release, and timing of any features
or functionality described for Oracle's products remains
at the sole discretion of Oracle.
What is JSR 299


JSR-299 defines a unifying dependency injection and
contextual lifecycle model for Java EE 6
• a completely new, richer dependency management model
• designed for use with stateful objects – integrates the “web”
  and “transactional” tiers – makes it much easier to build
  applications using JSF and EJB together
• includes a complete SPI allowing third-party frameworks to
  integrate cleanly in the EE 6 environment
What can be injected



• Certain kinds of things pre-defined by the specification:
  – (Almost) any Java class
  – EJB session beans
  – Objects returned by producer methods
  – Java EE resources (Datasources, JMS topics/queues, etc)
  – Persistence contexts (JPA EntityManager)
  – Web service references
  – Remote EJBs references
• An SPI allows third-party frameworks to introduce new kinds
 of things
Simple Example



• A really simple Java class:

public class Greeting {
    public String greet(String name) {
        return “hello “ + name;
    }
}
Bean Types



• A Bean type can be almost any Java type
• The example below has 4 bean types
 public class Bookshop
               extends Business
               implements Shop<Book> {
 ...
 }

• Yes 4 ! Bookshop, Business, Shop<Book> and Object...
• Restrict bean types with @Typed annotation
Bean Constructor



• Bean constructor are identified with the @Inject
    annotation :

public class ShoppingCart {

       final private User user;

       @Inject
       public ShoppingCart(User customer) {
           user = customer;
       }
}
Field Injection



• A simple client:


public class Printer {
    @Inject
    Greeting greeting;

      public void greet() {
          System.out.println(
               greeting.greet(“world”) );
      }
}
Constructor Injection



public class Printer {
    private final Greeting greeting;
    @Inject
    public Printer(Greeting greeting) {
        this.greeting = greeting;
    }
    public void greet() {
        System.out.println(
            greeting.greet(“world”) );
    }
}
Method Injection


public class Printer {
    private Greeting greeting;

    @Inject
    void init(Greeting greeting) {
       this.greeting = greeting;
    }
    public void greet() {
        System.out.println(
            greeting.greet(“world”) );
    }
}
Producer methods



• A producer method acts as a source of objects to be
  injected
• Use the @Produces annotation


Public class Shop {

     @Produces
     PaymentProcessor getPaymentProcessor();
     @Produces
     List<Product> getProducts();
}
Qualifiers



A qualifier is an annotation that lets a client choose
between multiple implementations of a certain type
(class or interface)
• Qualifiers replace lookup via string-based names
• @Default is the default qualifier
Defining new Qualifier



To define a new qualifier :
• Write a new annotation
• Annotate it with @Qualifier




@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface Informal {}
Specifying qualifiers



• On injected field :
  @Inject @Informal Greeting greeting;
• In method or constructor parameter
  public void setGreeting(@Informal Greeting greeting)
Scope and Context



Extensible context model
• A scope type is an annotation
• A context implementation can be associated with the
  scope type
Dependent scope, @Dependent
• this is the default
• it means that an object exists to serve exactly one
  client, and has the same lifecycle as that client
Built-in Scopes



Any web request, web service request, RMI call, EJB
timeout:
• @ApplicationScoped
• @RequestScoped
Any servlet, JSF requests:
• @SessionScoped
• @ConversationScoped
Custom scopes – provided by third-party frameworks
via an SPI
Scoped Objects


• A session scoped object


@SessionScoped
public class Login {
    private User user;

      public void login() {
          user = …
      }
      public User getUser() { return user; }
}
Injecting a scoped object




public class Printer {

     @Default Greeting greeting;
     @SessionScoped Login login;
     public void greet() {
        System.out.println(
           greeting.greet(
              login.getUser().getName() ) );
     }
}
Producing a scoped object



• Annotate the bean with the scope annotation
 @SessionScoped
 public class Login {
     private User user;
     @Produces
     User getCurrrentUser() {
         ...
     }
 }
• Injectable as
 @Inject @SessionScoped User user
Producing a scoped object

• Annotate the producer with the scope annotation

  @RequestScoped
  public class Login {
        private User user;
        @Produces
        @SessionScoped
        User getCurrrentUser() {
             ...
        }
  }
• Injectable as
  @Inject @SessionScoped User user
Producer fields



Producer fields are just a shortcut:

public class Login {
    @Produces
    @SessionScoped
    User user;

      public void login() {
          user = ...;
      }
}
Custom scopes



• Create an annotation for the custom scope
   – @Target({TYPE, FIELD, METHOD})
   – @Retention(Runtime)
   – @Scope or @NormalScope


 @Inherited
 @NormalScope
 @Target({TYPE, METHOD, FIELD})
 @Retention(RUNTIME)
 public @interface BusinessProcessScoped
 {...}
Names



To use our class in Unified EL expressions, give it a name:

@Named(“printer”)
public class Printer {
    @Current Greeting greeting;
    public void greet() {
        System.out.println(
            greeting.greet(“world”) );
    }
}
Unified EL



• Now we can use the object in a JSF or JSP page:


<h:commandButton value=”Say Hello”
action=”#{printer.greet}”/>

<h:outputText value=”#{products.total}”/>
Stateful class



If we want our object to hold state, we need to declare the
scope of that state:
@RequestScoped @Named public class Printer {
      @Inject Greeting g;
      private String name;
      public void setName(String name)
           { this.name=name; }
      public String getName() { return name; }
      public void greet() {
         System.out.println(g.greet(name) );
}}
Unified EL



• And now we can use it to process a JSF form:


<h:form>
  <h:inputText value=”#{printer.name}”/>
  <h:commandButton value=”Say Hello”
     action=”#{printer.greet}”/>
</h:form>

• Or a JSP :

name : ${printer.name}
Events



• Beans may produce and consume events
• Event is :
    – A Java object – the event object
    – A (possibly empty) set of qualifiers types


public interface Event<T> {
    public void fire(T event);
    public Event<T> select(Annotation...

                      qualifiers);
       ... more select...
}
Event producer



• Beans fire event via an Event instance


 @Inject
 @Admin
 Event<LoggedInEvent> loggedInEvent;

• Use the fire() method to send the events
public void login() {
    ...
    loggedInEvent.fire(
         new LoggedInEvent(user);
}
Observer Method



• Use the @Observes annotation


  public void afterLogin(
            @Observes LoggedInEvent event) {
      ....
  }
• Or use some qualifiers


 public void afterLogin(
     @Observes @Admin LoggedInEvent event) {
    ....
 }
Interceptor Binding Types



• Interceptors are used to separate cross-cutting concerns
  from business logic
• Can be enabled or disabled at runtime
• Interceptor binding type is an annotation annotated with
  InterceptorBinding

@Inherited
@InterceptorBinding
@Target({TYPE, METHOD})
@Retention(RUNTIME)
public @interface Transactional {...}
Bindings for an interceptor



• Interceptor bindings are specified by annotating the
 interceptor class with
  – The binding type (here it's @Transactional)
  – The Interceptor annotation


@Transactional
@Interceptor
public class TransactionInterceptor {
    @AroundInvoke
    public Objet manageTx(InvocationContext
              ctx) throws Exception {...}
}
Binding an interceptor



• Annotate with the interceptor binding type a bean class :

@Transactional
public class ShoppingCart {
   // all business methods are
transactional
}

• Or just a method :
Public class ShoppingCart {
     @Transactional
     public void placeOrder() {...}
}
Decorators



• Similar to interceptors but only apply to a particular Java
 type.
  – Can be easily enabled or disabled at runtime
  – Strong semantic binding between the decorator and decorated


public interface Persistence<T> {
    void save(T object);
    T load();
}
Decorator definition


• Annotate with Decorator annotation
@Decorator public class AuthPersistence<T>
              implements Persistence<T> {

     @Inject @Delegate
     Persistence<T> delegate;

     public void save(T object) {
         authorize();
         delegate.save(object);
     }
}
Stereotypes



• It is not only interceptor bindings we want to reuse!
• We have common architectural “patterns” in our
  application, with recurring component roles
   – Capture the roles using stereotypes
• A stereotype packages:
   – A default scope
   – A set of interceptor bindings
   – A default scope
   – Defaulted bean EL names
Simple Action Stereotype



@Stereotype
@Target(TYPE)
@Retention(RUNTIME)
@Secure
@RequestScoped
@Transactional
public @interface Action {
...
}

optional annotations in orange
Using a stereotype



• Annotating a Bean


@Action
public class Greeting {
    public String greet(String name) {
        return “hello “ + name;
    }
}
Resources


• To inject Java EE resources, persistence contexts, web
 service references, remote EJB references, etc, use a
 producer field :

 public class UserDatabasePersistenceContext
 {
     @Produces @UserDatabase
     @PersistenceContext
     EntityManager userDatabase;
 }
Resources


• @Resource possible to specify the jndi name


public class PricesTopic {
    @Produces @Prices
    @Resource(
      name=“java:global/env/jms/Prices”)
    Topic pricesTopic;
}
Injecting resources



• Now we’ve eliminated the use of string-based names:


public class UserDatabasePersistenceContext
{
    @UserDatabase
    EntityManager userDatabase;
}
public class PricesTopic {
    @Prices TopicSession topicSession;
    @Prices TopicPublisher topicPublisher;
}
More information



• JCP specification
  http://www.jcp.org/en/jsr/detail?id=299
• GlassFish V3 Java EE 6 Reference Implementation
  http://glassfish.dev.java.net
• The Aquarium
  http://blogs.sun.com/theaquarium

Más contenido relacionado

La actualidad más candente

Hibernate
Hibernate Hibernate
Hibernate Sunil OS
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate TutorialRam132
 
Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesecosio GmbH
 
EclipseCon 2005: Everything You Always Wanted to do with EMF (But were Afraid...
EclipseCon 2005: Everything You Always Wanted to do with EMF (But were Afraid...EclipseCon 2005: Everything You Always Wanted to do with EMF (But were Afraid...
EclipseCon 2005: Everything You Always Wanted to do with EMF (But were Afraid...Dave Steinberg
 
BASTA 2013: Custom OData Provider
BASTA 2013: Custom OData ProviderBASTA 2013: Custom OData Provider
BASTA 2013: Custom OData ProviderRainer Stropek
 
Documenting from the Trenches
Documenting from the TrenchesDocumenting from the Trenches
Documenting from the TrenchesXavier Noria
 
Mike Taulty OData (NxtGen User Group UK)
Mike Taulty OData (NxtGen User Group UK)Mike Taulty OData (NxtGen User Group UK)
Mike Taulty OData (NxtGen User Group UK)ukdpe
 
Hibernate complete Training
Hibernate complete TrainingHibernate complete Training
Hibernate complete Trainingsourabh aggarwal
 
Hibernate working with criteria- Basic Introduction
Hibernate working with criteria- Basic IntroductionHibernate working with criteria- Basic Introduction
Hibernate working with criteria- Basic IntroductionEr. Gaurav Kumar
 
.NET Core, ASP.NET Core Course, Session 14
.NET Core, ASP.NET Core Course, Session 14.NET Core, ASP.NET Core Course, Session 14
.NET Core, ASP.NET Core Course, Session 14aminmesbahi
 
Client-side JavaScript
Client-side JavaScriptClient-side JavaScript
Client-side JavaScriptLilia Sfaxi
 
Skillwise EJB3.0 training
Skillwise EJB3.0 trainingSkillwise EJB3.0 training
Skillwise EJB3.0 trainingSkillwise Group
 
Introduction to OO, Java and Eclipse/WebSphere
Introduction to OO, Java and Eclipse/WebSphereIntroduction to OO, Java and Eclipse/WebSphere
Introduction to OO, Java and Eclipse/WebSphereeLink Business Innovations
 

La actualidad más candente (20)

Hibernate
Hibernate Hibernate
Hibernate
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 
Java Beans
Java BeansJava Beans
Java Beans
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
 
Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examples
 
Deployment
DeploymentDeployment
Deployment
 
EclipseCon 2005: Everything You Always Wanted to do with EMF (But were Afraid...
EclipseCon 2005: Everything You Always Wanted to do with EMF (But were Afraid...EclipseCon 2005: Everything You Always Wanted to do with EMF (But were Afraid...
EclipseCon 2005: Everything You Always Wanted to do with EMF (But were Afraid...
 
BASTA 2013: Custom OData Provider
BASTA 2013: Custom OData ProviderBASTA 2013: Custom OData Provider
BASTA 2013: Custom OData Provider
 
Jpa
JpaJpa
Jpa
 
Documenting from the Trenches
Documenting from the TrenchesDocumenting from the Trenches
Documenting from the Trenches
 
hibernate with JPA
hibernate with JPAhibernate with JPA
hibernate with JPA
 
Mike Taulty OData (NxtGen User Group UK)
Mike Taulty OData (NxtGen User Group UK)Mike Taulty OData (NxtGen User Group UK)
Mike Taulty OData (NxtGen User Group UK)
 
Hibernate complete Training
Hibernate complete TrainingHibernate complete Training
Hibernate complete Training
 
Hibernate working with criteria- Basic Introduction
Hibernate working with criteria- Basic IntroductionHibernate working with criteria- Basic Introduction
Hibernate working with criteria- Basic Introduction
 
.NET Core, ASP.NET Core Course, Session 14
.NET Core, ASP.NET Core Course, Session 14.NET Core, ASP.NET Core Course, Session 14
.NET Core, ASP.NET Core Course, Session 14
 
L0033 - JFace
L0033 - JFaceL0033 - JFace
L0033 - JFace
 
Client-side JavaScript
Client-side JavaScriptClient-side JavaScript
Client-side JavaScript
 
Skillwise EJB3.0 training
Skillwise EJB3.0 trainingSkillwise EJB3.0 training
Skillwise EJB3.0 training
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Introduction to OO, Java and Eclipse/WebSphere
Introduction to OO, Java and Eclipse/WebSphereIntroduction to OO, Java and Eclipse/WebSphere
Introduction to OO, Java and Eclipse/WebSphere
 

Destacado

Java one brazil_keynote_dochez
Java one brazil_keynote_dochezJava one brazil_keynote_dochez
Java one brazil_keynote_dochezJerome Dochez
 
Vicky Morris
Vicky MorrisVicky Morris
Vicky Morrisuofpgal
 
S313557 java ee_programming_model_explained_dochez
S313557 java ee_programming_model_explained_dochezS313557 java ee_programming_model_explained_dochez
S313557 java ee_programming_model_explained_dochezJerome Dochez
 

Destacado (6)

Java one brazil_keynote_dochez
Java one brazil_keynote_dochezJava one brazil_keynote_dochez
Java one brazil_keynote_dochez
 
Corporate action 1
Corporate action 1Corporate action 1
Corporate action 1
 
Bullying
BullyingBullying
Bullying
 
Vicky Morris
Vicky MorrisVicky Morris
Vicky Morris
 
Java onebrazil hk2
Java onebrazil hk2Java onebrazil hk2
Java onebrazil hk2
 
S313557 java ee_programming_model_explained_dochez
S313557 java ee_programming_model_explained_dochezS313557 java ee_programming_model_explained_dochez
S313557 java ee_programming_model_explained_dochez
 

Similar a S313937 cdi dochez

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 PlatformArun Gupta
 
Field injection, type safe configuration, and more new goodies in Declarative...
Field injection, type safe configuration, and more new goodies in Declarative...Field injection, type safe configuration, and more new goodies in Declarative...
Field injection, type safe configuration, and more new goodies in Declarative...bjhargrave
 
OpenWebBeans/Web Beans
OpenWebBeans/Web BeansOpenWebBeans/Web Beans
OpenWebBeans/Web BeansGurkan Erdogdu
 
TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java DevelopersYakov Fain
 
Context and Dependency Injection
Context and Dependency InjectionContext and Dependency Injection
Context and Dependency InjectionWerner Keil
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFXHendrik Ebbers
 
Effective out-of-container Integration Testing
Effective out-of-container Integration TestingEffective out-of-container Integration Testing
Effective out-of-container Integration TestingSam Brannen
 
Aspect oriented programming with spring
Aspect oriented programming with springAspect oriented programming with spring
Aspect oriented programming with springSreenivas Kappala
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...Fabio Franzini
 
Java EE changes design pattern implementation: JavaDays Kiev 2015
Java EE changes design pattern implementation: JavaDays Kiev 2015Java EE changes design pattern implementation: JavaDays Kiev 2015
Java EE changes design pattern implementation: JavaDays Kiev 2015Alex Theedom
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design PatternsStefano Fago
 
Building maintainable app #droidconzg
Building maintainable app #droidconzgBuilding maintainable app #droidconzg
Building maintainable app #droidconzgKristijan Jurković
 
ASP.Net 5 and C# 6
ASP.Net 5 and C# 6ASP.Net 5 and C# 6
ASP.Net 5 and C# 6Andy Butland
 
Java EE revisits design patterns
Java EE revisits design patterns Java EE revisits design patterns
Java EE revisits design patterns Alex Theedom
 
softshake 2014 - Java EE
softshake 2014 - Java EEsoftshake 2014 - Java EE
softshake 2014 - Java EEAlexis Hassler
 

Similar a S313937 cdi dochez (20)

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
 
Field injection, type safe configuration, and more new goodies in Declarative...
Field injection, type safe configuration, and more new goodies in Declarative...Field injection, type safe configuration, and more new goodies in Declarative...
Field injection, type safe configuration, and more new goodies in Declarative...
 
OpenWebBeans/Web Beans
OpenWebBeans/Web BeansOpenWebBeans/Web Beans
OpenWebBeans/Web Beans
 
TypeScript for Java Developers
TypeScript for Java DevelopersTypeScript for Java Developers
TypeScript for Java Developers
 
Context and Dependency Injection
Context and Dependency InjectionContext and Dependency Injection
Context and Dependency Injection
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFX
 
Effective out-of-container Integration Testing
Effective out-of-container Integration TestingEffective out-of-container Integration Testing
Effective out-of-container Integration Testing
 
Aspect oriented programming with spring
Aspect oriented programming with springAspect oriented programming with spring
Aspect oriented programming with spring
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
Java EE changes design pattern implementation: JavaDays Kiev 2015
Java EE changes design pattern implementation: JavaDays Kiev 2015Java EE changes design pattern implementation: JavaDays Kiev 2015
Java EE changes design pattern implementation: JavaDays Kiev 2015
 
L04 base patterns
L04 base patternsL04 base patterns
L04 base patterns
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design Patterns
 
Building maintainable app #droidconzg
Building maintainable app #droidconzgBuilding maintainable app #droidconzg
Building maintainable app #droidconzg
 
ASP.Net 5 and C# 6
ASP.Net 5 and C# 6ASP.Net 5 and C# 6
ASP.Net 5 and C# 6
 
Eclipse e4
Eclipse e4Eclipse e4
Eclipse e4
 
Building maintainable app
Building maintainable appBuilding maintainable app
Building maintainable app
 
Java EE revisits design patterns
Java EE revisits design patterns Java EE revisits design patterns
Java EE revisits design patterns
 
Devoxx 2012 (v2)
Devoxx 2012 (v2)Devoxx 2012 (v2)
Devoxx 2012 (v2)
 
Gwt.create
Gwt.createGwt.create
Gwt.create
 
softshake 2014 - Java EE
softshake 2014 - Java EEsoftshake 2014 - Java EE
softshake 2014 - Java EE
 

S313937 cdi dochez

  • 1.
  • 2. <Insert Picture Here> Contexts and Dependency Injection for Java EE Jérome Dochez GlassFish Architect
  • 3. The following is intended to outline our general product direction. It is intended for information purposes, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle's products remains at the sole discretion of Oracle.
  • 4. What is JSR 299 JSR-299 defines a unifying dependency injection and contextual lifecycle model for Java EE 6 • a completely new, richer dependency management model • designed for use with stateful objects – integrates the “web” and “transactional” tiers – makes it much easier to build applications using JSF and EJB together • includes a complete SPI allowing third-party frameworks to integrate cleanly in the EE 6 environment
  • 5. What can be injected • Certain kinds of things pre-defined by the specification: – (Almost) any Java class – EJB session beans – Objects returned by producer methods – Java EE resources (Datasources, JMS topics/queues, etc) – Persistence contexts (JPA EntityManager) – Web service references – Remote EJBs references • An SPI allows third-party frameworks to introduce new kinds of things
  • 6. Simple Example • A really simple Java class: public class Greeting { public String greet(String name) { return “hello “ + name; } }
  • 7. Bean Types • A Bean type can be almost any Java type • The example below has 4 bean types public class Bookshop extends Business implements Shop<Book> { ... } • Yes 4 ! Bookshop, Business, Shop<Book> and Object... • Restrict bean types with @Typed annotation
  • 8. Bean Constructor • Bean constructor are identified with the @Inject annotation : public class ShoppingCart { final private User user; @Inject public ShoppingCart(User customer) { user = customer; } }
  • 9. Field Injection • A simple client: public class Printer { @Inject Greeting greeting; public void greet() { System.out.println( greeting.greet(“world”) ); } }
  • 10. Constructor Injection public class Printer { private final Greeting greeting; @Inject public Printer(Greeting greeting) { this.greeting = greeting; } public void greet() { System.out.println( greeting.greet(“world”) ); } }
  • 11. Method Injection public class Printer { private Greeting greeting; @Inject void init(Greeting greeting) { this.greeting = greeting; } public void greet() { System.out.println( greeting.greet(“world”) ); } }
  • 12. Producer methods • A producer method acts as a source of objects to be injected • Use the @Produces annotation Public class Shop { @Produces PaymentProcessor getPaymentProcessor(); @Produces List<Product> getProducts(); }
  • 13. Qualifiers A qualifier is an annotation that lets a client choose between multiple implementations of a certain type (class or interface) • Qualifiers replace lookup via string-based names • @Default is the default qualifier
  • 14. Defining new Qualifier To define a new qualifier : • Write a new annotation • Annotate it with @Qualifier @Qualifier @Retention(RUNTIME) @Target({TYPE, METHOD, FIELD, PARAMETER}) public @interface Informal {}
  • 15. Specifying qualifiers • On injected field : @Inject @Informal Greeting greeting; • In method or constructor parameter public void setGreeting(@Informal Greeting greeting)
  • 16. Scope and Context Extensible context model • A scope type is an annotation • A context implementation can be associated with the scope type Dependent scope, @Dependent • this is the default • it means that an object exists to serve exactly one client, and has the same lifecycle as that client
  • 17. Built-in Scopes Any web request, web service request, RMI call, EJB timeout: • @ApplicationScoped • @RequestScoped Any servlet, JSF requests: • @SessionScoped • @ConversationScoped Custom scopes – provided by third-party frameworks via an SPI
  • 18. Scoped Objects • A session scoped object @SessionScoped public class Login { private User user; public void login() { user = … } public User getUser() { return user; } }
  • 19. Injecting a scoped object public class Printer { @Default Greeting greeting; @SessionScoped Login login; public void greet() { System.out.println( greeting.greet( login.getUser().getName() ) ); } }
  • 20. Producing a scoped object • Annotate the bean with the scope annotation @SessionScoped public class Login { private User user; @Produces User getCurrrentUser() { ... } } • Injectable as @Inject @SessionScoped User user
  • 21. Producing a scoped object • Annotate the producer with the scope annotation @RequestScoped public class Login { private User user; @Produces @SessionScoped User getCurrrentUser() { ... } } • Injectable as @Inject @SessionScoped User user
  • 22. Producer fields Producer fields are just a shortcut: public class Login { @Produces @SessionScoped User user; public void login() { user = ...; } }
  • 23. Custom scopes • Create an annotation for the custom scope – @Target({TYPE, FIELD, METHOD}) – @Retention(Runtime) – @Scope or @NormalScope @Inherited @NormalScope @Target({TYPE, METHOD, FIELD}) @Retention(RUNTIME) public @interface BusinessProcessScoped {...}
  • 24. Names To use our class in Unified EL expressions, give it a name: @Named(“printer”) public class Printer { @Current Greeting greeting; public void greet() { System.out.println( greeting.greet(“world”) ); } }
  • 25. Unified EL • Now we can use the object in a JSF or JSP page: <h:commandButton value=”Say Hello” action=”#{printer.greet}”/> <h:outputText value=”#{products.total}”/>
  • 26. Stateful class If we want our object to hold state, we need to declare the scope of that state: @RequestScoped @Named public class Printer { @Inject Greeting g; private String name; public void setName(String name) { this.name=name; } public String getName() { return name; } public void greet() { System.out.println(g.greet(name) ); }}
  • 27. Unified EL • And now we can use it to process a JSF form: <h:form> <h:inputText value=”#{printer.name}”/> <h:commandButton value=”Say Hello” action=”#{printer.greet}”/> </h:form> • Or a JSP : name : ${printer.name}
  • 28. Events • Beans may produce and consume events • Event is : – A Java object – the event object – A (possibly empty) set of qualifiers types public interface Event<T> { public void fire(T event); public Event<T> select(Annotation... qualifiers); ... more select... }
  • 29. Event producer • Beans fire event via an Event instance @Inject @Admin Event<LoggedInEvent> loggedInEvent; • Use the fire() method to send the events public void login() { ... loggedInEvent.fire( new LoggedInEvent(user); }
  • 30. Observer Method • Use the @Observes annotation public void afterLogin( @Observes LoggedInEvent event) { .... } • Or use some qualifiers public void afterLogin( @Observes @Admin LoggedInEvent event) { .... }
  • 31. Interceptor Binding Types • Interceptors are used to separate cross-cutting concerns from business logic • Can be enabled or disabled at runtime • Interceptor binding type is an annotation annotated with InterceptorBinding @Inherited @InterceptorBinding @Target({TYPE, METHOD}) @Retention(RUNTIME) public @interface Transactional {...}
  • 32. Bindings for an interceptor • Interceptor bindings are specified by annotating the interceptor class with – The binding type (here it's @Transactional) – The Interceptor annotation @Transactional @Interceptor public class TransactionInterceptor { @AroundInvoke public Objet manageTx(InvocationContext ctx) throws Exception {...} }
  • 33. Binding an interceptor • Annotate with the interceptor binding type a bean class : @Transactional public class ShoppingCart { // all business methods are transactional } • Or just a method : Public class ShoppingCart { @Transactional public void placeOrder() {...} }
  • 34. Decorators • Similar to interceptors but only apply to a particular Java type. – Can be easily enabled or disabled at runtime – Strong semantic binding between the decorator and decorated public interface Persistence<T> { void save(T object); T load(); }
  • 35. Decorator definition • Annotate with Decorator annotation @Decorator public class AuthPersistence<T> implements Persistence<T> { @Inject @Delegate Persistence<T> delegate; public void save(T object) { authorize(); delegate.save(object); } }
  • 36. Stereotypes • It is not only interceptor bindings we want to reuse! • We have common architectural “patterns” in our application, with recurring component roles – Capture the roles using stereotypes • A stereotype packages: – A default scope – A set of interceptor bindings – A default scope – Defaulted bean EL names
  • 38. Using a stereotype • Annotating a Bean @Action public class Greeting { public String greet(String name) { return “hello “ + name; } }
  • 39. Resources • To inject Java EE resources, persistence contexts, web service references, remote EJB references, etc, use a producer field : public class UserDatabasePersistenceContext { @Produces @UserDatabase @PersistenceContext EntityManager userDatabase; }
  • 40. Resources • @Resource possible to specify the jndi name public class PricesTopic { @Produces @Prices @Resource( name=“java:global/env/jms/Prices”) Topic pricesTopic; }
  • 41. Injecting resources • Now we’ve eliminated the use of string-based names: public class UserDatabasePersistenceContext { @UserDatabase EntityManager userDatabase; } public class PricesTopic { @Prices TopicSession topicSession; @Prices TopicPublisher topicPublisher; }
  • 42. More information • JCP specification http://www.jcp.org/en/jsr/detail?id=299 • GlassFish V3 Java EE 6 Reference Implementation http://glassfish.dev.java.net • The Aquarium http://blogs.sun.com/theaquarium