november 2009 - jsr-299 context & dependency injection

47
1 JSR-299 Context & Dependency Injection Max Rydahl Andersen JBossian, Red Hat 27th. November 1

Upload: jbug-italy

Post on 05-Dec-2014

2.030 views

Category:

Technology


0 download

DESCRIPTION

JSR-299 Context & Dependency InjectionMax Andersen - Red Hat -JBug Roma - November 2009

TRANSCRIPT

Page 1: November 2009 - JSR-299 Context & Dependency Injection

1

J SR-299Cont ex t & Dependenc y

In jec t ion

Max Rydahl AndersenJBossian, Red Hat27th. November

1

Page 2: November 2009 - JSR-299 Context & Dependency Injection

2 Max Rydahl Andersen

Road Map

BackgroundConceptsStatus

Page 3: November 2009 - JSR-299 Context & Dependency Injection

3 Max Rydahl Andersen

J ava EE 6

•The EE 6 web profile removes most of the “cruft” that has developed over the years

•mainly totally useless stuff like web services, EJB 2 entity beans etc.

•some useful stuff (e.g. JMS) is missing, but vendors can include it

3

Page 4: November 2009 - JSR-299 Context & Dependency Injection

4 Max Rydahl Andersen

J ava EE 6

•EJB 3.1 - asynch, no-interface views, embeddable•JPA 2.0 - typesafe criteria API, many more O/R mapping options

•JSF 2.0 - Ajax, easy component creation, bookmarkable URLs

•Bean Validation 1.0 - annotation-based validation API •Servlet 3.0 - async support, better support for frameworks

•Standard global JNDI names •Managed Beans

4

Page 5: November 2009 - JSR-299 Context & Dependency Injection

5 Max Rydahl Andersen

Managed Beans

•Container-managed POJOs with minimal requirements•support a set of basic services

• resource injection• lifecycle callbacks• interceptors

5

Page 6: November 2009 - JSR-299 Context & Dependency Injection

6 Max Rydahl Andersen

Managed Beans

•the foundation for all other component types in the platform

•core services centralized under Managed Beans

•Other specifications will add support for additional services

• remoting• instance pooling•web services

6

Page 7: November 2009 - JSR-299 Context & Dependency Injection

7 Max Rydahl Andersen

Goals

•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

7

Page 8: November 2009 - JSR-299 Context & Dependency Injection

8 Max Rydahl Andersen

Loose c oupl ing

•Events, interceptors and decorators enhance the loose-coupling that is inherent in this model:

•event notifications decouple event producers from event consumers

• interceptors decouple technical concerns from business logic

•decorators allow business concerns to be compartmentalized

8

Page 9: November 2009 - JSR-299 Context & Dependency Injection

9 Max Rydahl Andersen

Going beyond t he spec

•Weld provides extra integrations•Tomcat/Jetty/Google App Engine support•Java SE support•OSGi containers•???

9

Page 10: November 2009 - JSR-299 Context & Dependency Injection

10 Max Rydahl Andersen

Going beyond t he spec

•and features which can be used in any CDI environment

•Seam2 bridge•Spring bridge•Wicket support•Logger•Prototype new ideas for the next version of the spec

10

Page 11: November 2009 - JSR-299 Context & Dependency Injection

11 Max Rydahl Andersen

Seam 3

•Use the CDI core•Provide a development environment

•JBoss Tools•Seam-gen (command line tool)

11

Page 12: November 2009 - JSR-299 Context & Dependency Injection

12 Max Rydahl Andersen

Seam 3

•include a set of modules for any container which includes CDI

• jBPM integration•Seam Security•Reporting (Excel/PDF)•Mail•etc.

12

Page 13: November 2009 - JSR-299 Context & Dependency Injection

13 Max Rydahl Andersen

Road Map

BackgroundConceptsStatus

Page 14: November 2009 - JSR-299 Context & Dependency Injection

14 Max Rydahl Andersen

Essent ia l ing red ient s

•API types•Qualifier annotations•Scope •Alternatives•A name (optional)•Interceptors and Decorators•The implementation

14

Page 15: November 2009 - JSR-299 Context & Dependency Injection

15 Max Rydahl Andersen

public class Hello { public String sayHello(String name) { return "hello" + name; } }

Sim ple Ex am ple

Any Managed Bean can use these services

@Statelesspublic class Hello { public String sayHello(String name) { return "hello" + name; } }

So can EJBs

Page 16: November 2009 - JSR-299 Context & Dependency Injection

16 Max Rydahl Andersen

Sim ple Ex am ple

public class Printer {

@Inject Hello hello;

public void printHello() { System.out.println( hello.sayHello("world") ); } }

@Inject defines an injection point. @Default qualifier is assumed

Page 17: November 2009 - JSR-299 Context & Dependency Injection

17 Max Rydahl Andersen

Const r uc t or in jec t ion

public class Printer { private Hello hello;

@Inject public Printer(Hello hello) { this.hello=hello; }

public void printHello() { System.out.println( hello.sayHello("world") ); } }

Constructors are injected by default; @Default is the default qualifier

Mark the constructor to be called by the container @Inject

Page 18: November 2009 - JSR-299 Context & Dependency Injection

18 Max Rydahl Andersen

Web Bean Nam es

@Named("hello") public class Hello { public String sayHello(String name) { return "hello" + name; } }

By default not available through EL.

@Named public class Hello { public String sayHello(String name) { return "hello" + name; } }

If no name is specified, then a default name is used. Both these Managed Beans have the same name

18

Page 19: November 2009 - JSR-299 Context & Dependency Injection

19 Max Rydahl Andersen

J SF Page

<h:commandButton value=“Say Hello” action=“#{hello.sayHello}”/>

Calling an action on a bean through EL

Page 20: November 2009 - JSR-299 Context & Dependency Injection

20 Max Rydahl Andersen

Qual i f ier

A qualifier is an annotation that lets a client choose between multiple implementations of an API at runtime

20

Page 21: November 2009 - JSR-299 Context & Dependency Injection

21 Max Rydahl Andersen

Def ine a qua l i f ie r

@Retention(RUNTIME) @Target({TYPE, METHOD, FIELD, PARAMETER}) @Qualifierpublic @interface Casual {} Creating a qualifer

is really easy!

Page 22: November 2009 - JSR-299 Context & Dependency Injection

22 Max Rydahl Andersen

Using a qual i f ie r

@Casual public class Hi extends Hello { public String sayHello(String name) { return "hi" + name; } }

We also specify the @Casual qualifier. If no qualifer is specified on a bean, @Default is assumed

Page 23: November 2009 - JSR-299 Context & Dependency Injection

23 Max Rydahl Andersen

Using a qual i f ie r

public class Printer { @Inject @Casual Hello hello; public void printHello() { System.out.println( hello.sayHello("JBoss") ); } }

Here we inject the Hello bean, and require an implementation which is qualified by @Casual

Page 24: November 2009 - JSR-299 Context & Dependency Injection

24 Max Rydahl Andersen

Al t er nat ives

•An alternative bean is one which must be specifically enabled for a particular deployment

• It replaces the managed or session bean for which it is an alternative

•May also completely replace it • all producers and observers defined on original bean are

disabled for this deployment)

•Alternatives enabled in XML deployment descriptor

24

Page 25: November 2009 - JSR-299 Context & Dependency Injection

25 Max Rydahl Andersen

Def in ing an a l t er nat ive

@Alternativepublic class Hola extends Hello { public String sayHello(String name) { return "hola " + name; } }

Same API, different implementation

Page 26: November 2009 - JSR-299 Context & Dependency Injection

26 Max Rydahl Andersen

Enabl ing an a l t er nat ive

<beans> <alternatives> <class>com.acme.Hola</class> <sterotype>com.acme.SouthernEuropean</stereotype> </alternatives> </beans>

Can also define a sterotype as an alternatives. Any stereotyped beans will be an alternative

Page 27: November 2009 - JSR-299 Context & Dependency Injection

27 Max Rydahl Andersen

St ereot ypes

•We have common architectural “patterns” in our application, with recurring roles

•Capture the roles using stereotypes

27

Page 28: November 2009 - JSR-299 Context & Dependency Injection

28 Max Rydahl Andersen

St ereot ypes

•A stereotype encapsulates any combination of: •a default scope, and •a set of interceptor bindings.

•A stereotype may also specify that: •all beans with the stereotype have defaulted bean EL names

•all beans with the stereotype are alternatives

28

Page 29: November 2009 - JSR-299 Context & Dependency Injection

29 Max Rydahl Andersen

Creat ing a s t ereot ype

@RequestScoped @Named@Alternative@Stereotype@Retention(RUNTIME) @Target(TYPE) public @interface AlternativeAction{}

Scope

Has a defaulted name

All stereotyped beans become alternatives

Page 30: November 2009 - JSR-299 Context & Dependency Injection

30 Max Rydahl Andersen

@AlternativeActionpublic class Hello { public String sayHello(String name) { return "hi " + name; } }

Using a s t ereot ype

Page 31: November 2009 - JSR-299 Context & Dependency Injection

31 Max Rydahl Andersen

Sc opes and Cont ex t s

•Built-in scopes: •Any servlet - @ApplicationScoped, @RequestScoped, @SessionScoped

•JSF requests - @ConversationScoped •Dependent scope (Defau l t ): @Dependent

•Custom scopes•A scope type is an annotation, can write your own context implementation and scope type annotation

31

Page 32: November 2009 - JSR-299 Context & Dependency Injection

32 Max Rydahl Andersen

Sc opes

@SessionScopedpublic class Login { private User user; public void login() { user = ...; } public User getUser() { return user; } }

Session scoped

Page 33: November 2009 - JSR-299 Context & Dependency Injection

33 Max Rydahl Andersen

Sc opes

public class Printer {

@Inject Hello hello; @Inject Login login;

public void printHello() { System.out.println( hello.sayHello( login.getUser().getName() ) ); } }

No coupling between scope and use of implementation

Page 34: November 2009 - JSR-299 Context & Dependency Injection

34 Max Rydahl Andersen

Produc er m et hods

•Producer methods allow control over the production of a bean where:

• the objects to be injected are not managed instances• the concrete type of the objects to be injected may vary at runtime

• the objects require some custom initialization that is not performed by the bean constructor

34

Page 35: November 2009 - JSR-299 Context & Dependency Injection

35 Max Rydahl Andersen

Produc er m et hods

@SessionScoped public class Login { private User user; public void login() { user = ...; } @Produces User getUser() { return user; } }

Page 36: November 2009 - JSR-299 Context & Dependency Injection

36 Max Rydahl Andersen

Produc er m et hods

public class Printer { @Inject Hello hello; @Inject User user; public void hello() { System.out.println( hello.sayHello( user.getName() ) ); } }

Much better, no dependency on Login!

Page 37: November 2009 - JSR-299 Context & Dependency Injection

37 Max Rydahl Andersen

•Simpler alternative to Producer methods

@SessionScopedpublic class Login {

@Produces @LoggedIn @RequestScoped private User user;

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

Produc er Fie lds

Similar to outjection in Seam

37

Page 38: November 2009 - JSR-299 Context & Dependency Injection

38 Max Rydahl Andersen

J ava EE Resourc es

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

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

Page 39: November 2009 - JSR-299 Context & Dependency Injection

39 Max Rydahl Andersen

Event s

•Event producers raise events that are then delivered to event observers by the Web Bean manager.

•not only are event producers decoupled from observers; observers are completely decoupled from producers

•observers can specify a combination of "selectors" to narrow the set of event notifications they will receive

•observers can be notified immediately, or can specify that delivery of the event should be delayed until the end of the current transaction

39

Page 40: November 2009 - JSR-299 Context & Dependency Injection

40 Max Rydahl Andersen

public class Hello {

@Inject @Any Event<Greeting> greeting;

public void sayHello(String name) { greeting.fire( new Greeting("hello " + name) ); }

}

Inject an instance of Event. Additional qualifiers can be specified to narrow the event consumers called. API type specified as a parameter on Event

“Fire” an event, the producer will be notified

Event s

Page 41: November 2009 - JSR-299 Context & Dependency Injection

41 Max Rydahl Andersen

public class Printer {

void onGreeting(@Observes Greeting greeting, @Default User user) { System.out.println(user + “ says “ + greeting); }

}

Observer methods, take the API type and additional qualifiers

Additional parameters can be specified and will be injected by the container

Event s

Page 42: November 2009 - JSR-299 Context & Dependency Injection

42 Max Rydahl Andersen

Road Map

BackgroundConceptsStatus

Page 43: November 2009 - JSR-299 Context & Dependency Injection

43 Max Rydahl Andersen

J SR-299: Cont ex t s and Dependenc y In jec t ion for J ava EE

•Proposed Final Draft 2 published•Reference Manual being written now

•http://www.seamframework.org/Weld• look for an update soon!

•Send feedback to [email protected]

43

Page 44: November 2009 - JSR-299 Context & Dependency Injection

44 Max Rydahl Andersen

Weld

•The Reference implementation•Feature complete preview of PFD2. Download it, try it out, give feedback!

•http://seamframework.org/Download

•Working on second release candidate

44

Page 45: November 2009 - JSR-299 Context & Dependency Injection

45 Max Rydahl Andersen

Weld

•Integrated into:•JBoss 5.1.0.GA and above (6.0.0.Beta1 coming soon)•GlassFish V3 (get a recent build)

•Available as an addon for:•Tomcat 6.0.x•Jetty 6.1.x•Java SE•Google App Engine

45

Page 46: November 2009 - JSR-299 Context & Dependency Injection

46

J Boss CDI Tools (Wor k in prog ress)

•Code completion for @Named components•Validation of CDI constructs (”red squigly lines”)

•Ambigious injection points•Non-matching injection •…

•CDI component explorer•Show possible injection points for a given line•Show matching event types•Refactoring of EL result in updates to @Named•...

Page 47: November 2009 - JSR-299 Context & Dependency Injection

47 Max Rydahl Andersen

Q & A

http://in.relation.to/Bloggers/Max

http://www.seamframework.org/Weld

http://jcp.org/en/jsr/detail?id=299

47