Top Banner
1 Why Why Context and Context and Dependency Injection (CDI) Dependency Injection (CDI) is So Cool? is So Cool? 1 Sang Shin Sang Shin JRebel Evangelist from ZeroTurnaround.com JRebel Evangelist from ZeroTurnaround.com Founder and Chief Instructor from JPassion.com Founder and Chief Instructor from JPassion.com Learn with Passion!” Learn with Passion!”
64

Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

Jun 17, 2019

Download

Documents

duongthu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

1

Why Why Context and Context and Dependency Injection (CDI)Dependency Injection (CDI)

is So Cool?is So Cool?

1

Sang ShinSang ShinJRebel Evangelist from ZeroTurnaround.comJRebel Evangelist from ZeroTurnaround.com

Founder and Chief Instructor from JPassion.comFounder and Chief Instructor from JPassion.com““Learn with Passion!”Learn with Passion!”

Page 2: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

2

• CDI Basics (20 minutes)• CDI Advanced (25 minutes)• CDI Extension • JRebel demo (10 minutes)

This is usually 2-hour talk but is reduced to 1 hour.

Complete slides and the demo's are available as ready-to-run Maven projects from http://www.javapassion.com/jugtalks

Topics

Page 3: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

3

Context and Context and Dependency Injection (CDI)Dependency Injection (CDI)

(JSR 299) Basics (JSR 299) Basics

3

Page 4: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

4

• What is and Why Dependency Injection?• What is and Why CDI (JSR 299)?• CDI theme – Loose coupling with string typing• Bean definition (in the context of CDI)• Basic dependency injection• Qualifier• @Named built-in qualifier• Stateful objects (scoped objects)• CDI for Java SE application

Topics of “CDI Basics”

Page 5: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

What is & Why What is & Why CDI (JSR 299)?CDI (JSR 299)?

Page 6: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

6

What is CDI (JSR 299)? (Basic)• Provides a unifying Dependency Injection and contextual

life-cycle model for Java EE> Unified existing Dependency Injection schemes – Spring, Guice,

Seam> A completely new, richer dependency management model> Type-safe dependency injection> Designed for use with stateful objects (scoped objects)

• Makes it much easier to build applications using JSF and EJB together> Let you use EJBs directly as JSF managed beans

Page 7: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

7

What is CDI (JSR 299)? (Extensibility)• Includes a SPI extending Java EE platform> Java EE is now flexible, portable, and extensible architecture > You can change the characteristics of existing platform services> You can provide new platform-level portable services through new

annotations at the time of deployment• You can now build your own next generation Java EE

platform> You don't have to wait Java EE 7 for the platform features you

need

Page 8: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

8

Why CDI (JSR 299) for Java EE? • Reason #1: We need general-purpose dependency

injection scheme> Java EE 5 provides resource injection of only known resources to

the container (@EJB, @PersistenceContext, @PersistenceUnit, @Resource )

> In other words, Java EE 5 does not provide general-purpose dependency injection scheme

• Reason #2: We need type-based injection> String name or XML based injection is fragile > Type-based injection enables better tooling in general

Page 9: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

Bean DefinitionBean Definition(in the context of CDI)(in the context of CDI)

Page 10: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

10

What is a Bean anyway?• Many forms of a “bean” already exist. So which bean are

we talking about?> JSF bean> EJB bean> Spring bean> Seam bean> Guice bean> CDI bean

• Java EE needs a unified bean definition> Managed Bean 1.0 specification in Java EE 6 provides it

Page 11: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

11

Managed Bean 1.0: What is it?• Managed Beans are container-managed POJOs> Lightweight component model> Instances are managed by the container

Page 12: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

12

What about EJB, REST, CDI. etc Bean?

• You could see everything as a Managed Bean with extra services

• An EJB is a Managed Bean with > Transaction support> Security> Thread safety> Persistence

• A REST service is a Managed Bean with> HTTP support

• A CDI bean is a Managed Bean with > CDI services (explained in the next slide)

Page 13: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

13

• Auto-discovered – by the container• Set of qualifiers – solves ambiguity • Scope – context of a bean• Bean EL name – support non-type based invocation• Set of interceptor bindings• Alternative – replace bean at deployment time

CDI Bean Services

Page 14: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

14

CDI Bean Example• No annotation required• No bean declaration in XML file required

// This is a CDI bean public class Greeting {

public String greet(String name) { return "Hello, " + name; }}

Page 15: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

15

Automatic Bean Discovery• How does container discover beans?> By scanning the classpath that contains both application and

container archives• How can container scan only the relevant application

archives for bean discovery?> By detecting the presence of “beans.xml” in application archive> For WAR file, the “beans.xml” is under WEB-INF directory> For JAR file, the “beans.xml” is under META-INF directory

• “beans.xml”> It is not for declaring beans (like in Spring)> It can be empty> Used for some other purposes (like declaring an alternative)

Page 16: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

Basic InjectionBasic Injection

Page 17: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

17

How do you inject a Bean? • Use @Inject <Java-Type> <variable> for field injection• <Java-Type> can be Java interface

public class MyGreeter {

// Inject Greeting object for field injection @Inject Greeting greeting;

public sayGreeting(String name){ System.out.println(greeting.greet(name)); }}

Page 18: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

18

Where can you inject a bean?• Bean can be injected at “Injection points”> Field> Method parameter

• Method can be> Constructor (useful for created immutable object)> Initializer> Setter method> Producer (will be covered in “CDI Advanced”)> Observer (will be covered in “CDI Advanced”)

Page 19: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

19

Demo:Demo:inject_bean_*,inject_bean_*,

inject_ejb-to_fieldinject_ejb-to_field4531_javaee6_cdi_basics.zip4531_javaee6_cdi_basics.zip

Page 20: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

QualifierQualifier

Page 21: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

21

• For a given bean type (class or interface), there may be multiple beans which implement the type (in the classpath)> For an interface, there could be multiple implementations> For a class, there could be multiple child types > Ambiguity error will result

• A qualifier is an annotation that lets a client choose one between multiple candidates of a certain type> Make type more specific> Assigns semantic meaning

• Injected type is identified by> Qualifier(s) + Java type> e.g. @Inject @LoggedIn User user;

What is a Qualifier?

Qualifier Java type

Page 22: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

22

• Step #1: Define a qualifier (type)• Step #2: Qualify an implementation class• Step #3: Select a qualified implementation

How to build and use Qualifier? – 3 steps

Page 23: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

23

// Define “Informal” qualifier (type)@Qualifier@Retention(RUNTIME)@Target({TYPE, METHOD, FIELD, PARAMETER})public @interface Informal { }

Step #1: Define a Qualifier (Type)

Page 24: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

24

// Bind the “@Informal” qualifier with “InformalGreeting”// implementation class. (Think of @Informal as an// extended type of the Greeting implementation class.)

@Informalpublic class InformalGreeting extends Greeting { public String greet(String name) { return "hi " + name; }}

Part #2: Qualify an Implementation

Page 25: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

25

• Injected type is identified by> Qualifier(s) + Java type

public class MyGreeter {

// Injected type is identified @Informal qualifier and Greeting type. // So InformalGreeting class (of previous slide) will be chosen. @Inject @Informal Greeting greeting;

public void greet() { System.out.println(greeting.greet("Hello") ); }}

Part #3: Select Qualified Impl. Class

Page 26: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

26

• Qualifier + Java type makes a composite type (extended type)> Again, think of a Qualifier as a type

• Qualifiers make type safe injection possible> Qualifiers replace “look-up via string-based names”

Qualifier and Type Safety (Strong Typing)

Page 27: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

27

Demo:Demo:inject_qualifier_ambiguitycase,inject_qualifier_ambiguitycase,

inject_qualifierinject_qualifier4531_javaee6_cdi_basics.zip4531_javaee6_cdi_basics.zip

Page 28: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

28

• Qualifier definition with attributes@Qualifier@Retention(RUNTIME)@Target({TYPE, METHOD,FIELD,PARAMETER})public @interface VariousGreetings { GreetingType type() default GreetingType.FORMAL;}

• Qualify an implementation@VariousGreetings(type=GreetingType.FORMAL)public class FormalGreeting implements GreetingInterface { public String greet(String name){ return "Formal Hello " + name; }}

Qualifier with Attributes

Page 29: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

29

• Select qualified implementation // Inject GreetingInterface object with qualifier with attribute @Inject @VariousGreetings(type=GreetingType.INFORMAL) GreetingInterface greeting;

Qualifier with Attributes

Page 30: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

30

Demo:Demo:inject_qualifier_attributesinject_qualifier_attributes

4531_javaee6_cdi_basics.zip4531_javaee6_cdi_basics.zip

Page 31: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

@Named@NamedBuilt-in QualiferBuilt-in Qualifer

Page 32: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

32

• In Java code, injected type is identified by> Qualifier(s) + Java type

• How do we identify a bean outside of type-safe Java code, for example in Unified EL expressions (in facelet or JSP), in which we cannot use Java type?> We should able to identify a bean via a name (not Java type)

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

Why do we need @Named Annotation?

Page 33: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

33

• Give it a name using @Named annotation

public@Named("myprinter")class Printer {

@Inject Greeting greeting;

public void greet() { System.out.println( greeting.greet("world") ); }}

Why do we need @Named Annotation?

Page 34: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

34

Context and Context and Dependency Injection (CDI)Dependency Injection (CDI)

Advanced Advanced

54

Page 35: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

35

• Producer • Events • Alternatives• Interceptor (not covered in this presentation)• Decorator• Stereotype

Topics of “CDI Advanced”

Page 36: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

Producer Producer

Page 37: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

37

• Producer methods allow control over the production of the dependency objects> Runtime polymorphism – Producer can return different object

depending on runtime condition (business logic, time of the day, etc)

> Creation of Non-bean instance - Objects to be injected are not required to be instances of beans

> Custom setup - Objects require some custom initialization that is not performed by the bean constructor

• Enables decoupling of a “producer” from the “consumer”

Producer Methods

Page 38: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

38

// Inject Coder object@Inject@RequestScopedCoder coder;

// Producer method returns a different object depending on a runtime condition@Produces@RequestScopedpublic Coder getCoder(@New TestCoderImpl tci, @New CoderImpl ci) { switch (coderType) { case TEST: return tci;

case SHIFT: return ci;

default: return null; }}

Runtime Polymorphism

Page 39: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

39

// Produce FacesContext object that we don't control@Produces@RequestScopepublic FacesContext getFacesContext () { return FacesContext.getInstance(); }}

Creation of Non-Bean Instance

Page 40: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

40

Demo:Demo:cdi_producer_methods_example1cdi_producer_methods_example1cdi_producer_methods_example2cdi_producer_methods_example24533_javaee6_cdi_advanced.zip4533_javaee6_cdi_advanced.zip

Page 41: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

41

• Use Producer field to expose Java EE resources // Define a qualifer@Qualifier@Retention(RUNTIME)@Target({ METHOD, FIELD, PARAMETER, TYPE})public @interface UserDatabase {}

// Expose Java EE resource (EntityManager) via Producer@Singletonpublic class UserDatabaseEntityManager { @Produces @UserDatabase @PersistenceContext(unitName="producerfieldsPU") private EntityManager em;}

Type-safe Use of Java EE Resources (1)

Page 42: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

42

@ConversationScoped@Statefulpublic class RequestBean {

// Inject Java EE resource in type-safe way, no more string @Inject @UserDatabase EntityManager em;

public ToDo createToDo(String inputString) { ...

try { ... em.persist(toDo);

return toDo; } catch (Exception e) { throw new EJBException(e.getMessage()); } }

Type-safe Use of Java EE Resources (2)

Page 43: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

43

Demo:Demo:cdi_producer_UserDatabase_dbcdi_producer_UserDatabase_db4533_javaee6_cdi_advanced.zip4533_javaee6_cdi_advanced.zip

Page 44: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

EventsEvents

Page 45: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

45

• Completely decouple action (event producer) and reactions (event consumers)

• Qualifiers tune which event notifications are received

CDI Event Observer Pattern

Page 46: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

46

• Define Event Class• Event producer fires an event• Event consumer observes event through @Observes

CDI Event Observer Pattern

Page 47: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

47

• Event class can be any POJO classpublic class LoggedInEvent { private String user;

public LoggedInEvent(String user) { this.user = user; }}

Event Class

Page 48: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

48

• An event is fired by an injected javax.enterprise.event.Event object

public class Login {

@Inject Event<LoggedInEvent> loggedInEvent;

public void login() { loggedInEvent.fire( new LoggedInEvent(credentials.getUsername())); }}

Event Producers

Page 49: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

49

• The only thing event consumer has to do is to use @Observes <Event-class> annotation// This methog gets invoked when LoggedInEvent is firedpublic void afterLogin(@Observes LoggedInEvent event) { System.out.println("afterLogin() method is called, event = " + event);}

Event Consumer (Event Observer)

Page 50: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

50

Demo:Demo:weld-servlet-eventweld-servlet-event

4533_javaee6_cdi_advanced.zip4533_javaee6_cdi_advanced.zip

Page 51: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

51

• Event can be selectively fired and received through qualifierpublic class Login {

@Inject @Admin Event<LoggedInEvent> loggedInEvent;

// Event producer public void login() { loggedInEvent.fire( new LoggedInEvent(credentials.getUsername())); }}

// Event consumerpublic void afterAdminLogin(@Observes @Admin LoggedInEvent event) { System.out.println("----afterAdminLogin() method is called, event = " + event);}

Event with Qualifier

Page 52: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

52

Demo:Demo:weld-servlet-event-qualifierweld-servlet-event-qualifier

4533_javaee6_cdi_advanced.zip4533_javaee6_cdi_advanced.zip

Page 53: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

AlternativeAlternative

Page 54: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

54

• Any bean with @Alternative is not considered for injection > Lets you package multiple beans that match injection type without

ambiguity errors> In order to be considered for injection, it has to be activated in

“beans.xml”• Provide a replacement implementation during deployment> You can apply the @Alternative annotation to two or more beans,

then, based on your deployment, specify the bean you want to use in the “beans.xml” configuration file

> Useful for providing mock objects for testing – mock objects are annotated with @Alternative– In normal operation, alternative bean will not be considered for

injection– In testing operation, activate it via “beans.xml”

What is Alternative Bean?

Page 55: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

55

// Annotate alternative implementation with @Alternative annotation@Alternativepublic class TestCoderImpl implements Coder { public String codeString( String s, int tval) { return ("input string is " + s + ", shift value is " + tval); }}

// Activate it in “beans.xml”<beans> <alternatives> <class>encoder.TestCoderImpl</class> </alternatives></beans>

Alternative Bean

Page 56: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

56

Demo:Demo:cdi_alternative_encodercdi_alternative_encoder

4533_javaee6_cdi_advanced.zip4533_javaee6_cdi_advanced.zip

Page 57: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

DecoratorDecorator

Page 58: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

58

• Decorators implement the Decorator design pattern> Allows implementation of an additional business logic for a bean

• A Decorator decorates interfaces they implement• @Delegate is used to inject the original object> Original object business logic can be be invoked within the

decorator• Decorators must be activated through “beans.xml”

What is a Decorator?

Page 59: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

59

@Decoratorpublic abstract class CoderDecorator implements Coder {

@Inject @Delegate @Any Coder coder;

public String codeString( String s, int tval) { int len = s.length();

// The decorator's codeString method calls the delegate // object's codeString method to perform the actual encoding. return "\"" + s + "\" becomes " + "\"" + coder.codeString(s, tval) + "\", " + len + " characters in length"; }}

Define a Decorator

Page 60: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

60

• Decorator has to be activated through “beans.xml”<beans > <decorators> <class>decorators.CoderDecorator</class> </decorators></beans>

Enable Decorator

Page 61: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

61

Demo:Demo:cdi_decoratorscdi_decorators

4533_javaee6_cdi_advanced.zip4533_javaee6_cdi_advanced.zip

Page 62: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

62

JRebel DemoJRebel Demo

107

Page 63: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

63

• How many of you ever heard of JRebel?• How many of you know what JRebel does?• How many of you ever used JRebel?

• Please fill up feedback form – 1 year free license will be given to a winner

• You can get JRebel 1-month free license > http://zeroturnaround.com

Quick Survey on JRebel

Page 64: Why Context and Dependency Injection (CDI) is So Cool?static.hjug.org/present/jug_cdi_houston.pdf1 Why Context and Dependency Injection (CDI) is So Cool? 1 Sang Shin JRebel Evangelist

64

Thank you!Thank you!

Check JavaPassion.com Codecamps!Check JavaPassion.com Codecamps!http://www.javapassion.com/codecampshttp://www.javapassion.com/codecamps

109