76
JSR-299 (CDI), Weld and the Future of Seam Dan Allen Principal Software Engineer JBoss by Red Hat

JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

Embed Size (px)

DESCRIPTION

Introduces JSR-299, the new Java standard for contextual lifecycle management, dependency injection (CDI) and event notification. Covers the core programming model, explains its relationship to EJB 3.1 and JSF 2.0, and clarifies how it unifies and enhances the Java EE platform as a whole (extending to JPA, JAX-RS and JMS). You are then introduced to Weld, the JSR-299 Reference Implementation, and its Servlet container and Java SE extensions. Finally, looks ahead at how a modularized Seam 3 ties into this new foundation as a set of portable CDI extensions.

Citation preview

Page 1: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

JSR-299 (CDI), Weld andthe Future of Seam

Dan AllenPrincipal Software EngineerJBoss by Red Hat

Page 2: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

2 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Agenda

● Java EE today

● Where JSR-299 fits in

● JSR-299 themes

● CDI programming model tour

● CDI extensions

● Weld

● Seam 3

Page 3: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

3 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Technology terminology

● JSR-299 (CDI)● Contexts & Dependency Injection for the

Java EE Platform● Weld

● JSR-299 Reference Implementation & TCK● Extended CDI support (Servlets, Java SE)● Portable CDI enhancements for extension writers

● Seam 3● Portable extensions for Java EE● Portable integrations with non-Java EE technologies

Page 4: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

4 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

What is Java EE?

● Standard platform comprised ofmanaged components & services

● Business logic as components

1. Less code

2. Higher signal-to-noise ratio

3. Powerful mechanisms for free

4. Portable knowledge

Page 5: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

5 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Why reinvest?

Seam 2

Java EE 5

Page 6: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

6 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Stated goal of JSR-299

Web tier(JSF)

Transactional tier(EJB)

Page 7: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

7 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

What CDI provides

● Services for Java EE components● Lifecycle management of stateful beans bound to

well-defined contexts (including conversation context)● A type-safe approach to dependency injection● Interaction via an event notification facility● Reduced coupling between interceptors and beans● Decorators, which intercept specific bean instances● Unified EL integration (bean names)

● SPI for developing extensions for the Java EE platform● Java EE architecture flexible, portable, extensible

Page 8: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

8 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

What CDI provides

● Services for Java EE components● Lifecycle management of stateful beans bound to

well-defined contexts (including conversation context)● A type-safe approach to dependency injection● Interaction via an event notification facility● Reduced coupling between interceptors and beans● Decorators, which intercept specific bean instances● Unified EL integration (bean names)

● SPI for developing extensions for the Java EE platform● Java EE architecture flexible, portable, extensible

Page 9: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

9 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

CDI: The big picture

● Fill in

● Catalyze

● Evolve

Page 10: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

10 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Why dependency injection?

● Weakest aspect of Java EE 5

● Closed set of injectable resources● @EJB● @PersistenceContext, @PersistenceUnit● @Resource (e.g., DataSource, UserTransaction)

● Name-based injection is fragile

● Lacked rules

Page 11: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

11 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Leverage and extend Java’s type system

@Annotation

Type

<TypeParam>

This information is pretty useful!

Page 12: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

12 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

JSR-299 theme

Loose coupling...

...with strong typingstrong typing

@Inject@Observes

@InterceptorBinding

@Qualifier

Event<Order>

@Produces @WishListList<Product> getWishList()

@UserDatabase EntityManager

Page 13: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

13 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Loose coupling

● Decouple server and client● Using well-defined types and “qualifiers”● Allows server implementation to vary

● Decouple lifecycle of collaborating components● Automatic contextual lifecycle management● Stateful components interact like services

● Decouple orthogonal concerns (AOP)● Interceptors & decorators

● Decouple message producer from consumer● Events

Page 14: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

14 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

StrongStrong typing typing

● Type-based injection● Eliminate reliance on string-based names● Refactor friendly

● Compiler can detect typing errors● No special authoring tools required● Casting mostly eliminated

● Semantic code errors detected at application startup

● Tooling can detect ambiguous dependencies (optional)

Page 15: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

15 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Who's bean is it anyway?

● Everyone throwing around this term “bean”● JSF● EJB● Seam● Spring● Guice● Web Beans

● Need a “unified bean definition”

Page 16: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

16 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Managed bean specification

● Common bean definition

● Instances managed by the container

● Common services● Lifecycle callbacks● Resource injections● Interceptors

● Foundation spec

How managed beans evolved: http://www.infoq.com/news/2009/11/weld10

JSF EJB CDI JAX-RS

ManagedBeans

Page 17: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

17 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

CDI bean ingredients

● Set of bean types

● Set of qualifiers

● Scope

● Bean EL name (optional)

● Set of interceptor bindings

● Alternative classification

● Bean implementation class

Auto-discovered!

Page 18: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

18 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Welcome to CDI, managed beans!

public class Welcome { public String buildPhrase(String city) { return "Welcome to " + city + "!"; } }

Page 19: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

19 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Welcome to CDI, EJB 3.1 session beans!

@Stateless public class Welcome { public String buildPhrase(String city) { return "Welcome to " + city + "!"; } }

Page 20: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

20 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

When is a bean recognized?

● Bean archive (WAR) ● Bean archive (JAR)

beans.xml can be empty!

Page 21: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

21 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Injection 101

public class Greeter { @Inject Welcome w;

public void welcome() { System.out.println( w.buildPhrase("San Francisco")); }}

Page 22: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

22 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Where can it be injected?

● Field

● Method parameter● Constructor*● Initializer● Producer● Observer

Page 23: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

23 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

What can be injected?

Managed bean

Object returned by producer

EJB session bean (local or remote)

Java EE resource (DataSource, JMS destination, etc)

JTA UserTransaction

Persistence unit or context

Security principle

Bean Validation factory

Web service reference

Additional resources introduced through SPI

Page 24: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

24 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

The bean vs “the other implementation”

● Multiple implementations of same interface

● One implementation extends anotherpublic class Welcome { public String buildPhrase(String city) { return "Welcome to " + city + "!"; } }

public class TranslatingWelcome extends Welcome {

@Inject GoogleTranslator translator;

public String buildPhrase(String city) { return translator.translate( "Welcome to " + city + "!"); } }

Page 25: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

25 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Quiz: Which implementation gets injected?

public class Greeter { private Welcome welcome; @Inject void init(Welcome welcome) { this.welcome = welcome; }

...}

It's ambiguous!

Page 26: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

26 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Working out an ambiguous resolution

● Qualifier

● Alternative

● Producer

● Veto (or hide)

Page 27: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

27 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

qualifier

n. an annotation used to resolve an APIimplementation variant at an injection point

Page 28: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

28 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Defining a qualifier

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

@interface means annotation@interface means annotation

Page 29: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

29 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Qualifying an implementation

@Translatingpublic class TranslatingWelcome extends Welcome {

@Inject GoogleTranslator translator;

public String buildPhrase(String city) { return translator.translate( "Welcome to " + city + "!"); } }

● makes type more specific● assigns semantic meaning

Page 30: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

30 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Qualifier as a “binding type”

Page 31: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

31 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Explicitly request qualified interface

public class Greeter {

private Welcome welcome;

@Inject void init(@Translating Welcome welcome) { this.welcome = welcome; }

public void welcomeVisitors() { System.out.println( welcome.buildPhrase("San Francisco")); }}

No reference to implementation class!No reference to implementation class!

Page 32: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

32 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Alternative bean

● Swap replacement implementation per deployment

● Replaces bean and its producer methods and fields

● Disabled by default● Must be activated in /META-INF/beans.xml

In other words, an override

Page 33: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

33 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Defining an alternative

@Alternativepublic class TranslatingWelcome extends Welcome {

@Inject GoogleTranslator translator;

public String buildPhrase(String city) { return translator.translate( "Welcome to " + city + "!"); }}

Page 34: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

34 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Substituting the alternative

● Activated using beans.xml<beans> <alternatives> <class>com.acme.TranslatingWelcome</class> </alternatives></beans>

Page 35: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

35 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Assigning a bean (EL) name

@Named("greeter")public class Greeter { private Welcome welcome;

@Inject void init(Welcome welcome) { this.welcome = welcome; }

public void welcomeVisitors() { System.out.println( welcome.buildPhrase("San Francisco")); }}

Page 36: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

36 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Assigning a bean (EL) name by convention

@Namedpublic class Greeter { private Welcome welcome;

@Inject void init(Welcome welcome) { this.welcome = welcome; }

public void welcomeVisitors() { System.out.println( welcome.buildPhrase("San Francisco")); }}

Bean name is decapitalizedsimple class name Bean name is decapitalizedsimple class name

Page 37: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

37 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Welcome to CDI, JSF!

● Use the bean directly in the JSF view<h:form> <h:commandButton value="Welcome visitors" action="#{greeter.welcomeVisitors}"/></h:form>

Page 38: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

38 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

CDI

JSFmanaged

beans

Page 39: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

39 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Stashing the bean in a context

● Bean saved for the duration of a request@Named@RequestScopedpublic class Greeter { @Inject private Welcome w; private String city;

public String getCity() { return city; }

public void setCity(String city) { this.city = city; }

public void welcomeVisitors() { System.out.println(w.buildPhrase(city)); }}

Page 40: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

40 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Collapsing layers with state management

● Now it’s possible for bean to hold state<h:form> <h:inputText value="#{greeter.city}"/> <h:commandButton value="Welcome visitors" action="#{greeter.welcomeVisitors}"/></h:form>

Prints:Welcome to San Francisco!

San Francisco

Page 41: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

41 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Mission accomplished: We have a deal!

Web tier(JSF)

Business tier(managed bean)

Page 42: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

42 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Scope types and contexts

● Default scope - @Dependent● Bound to lifecycle of bean holding reference

● Servlet scopes● @ApplicationScoped● @RequestScoped● @SessionScoped

● JSF conversation scope - @ConversationScoped

● Custom scopes● Define scope type annotation (e.g., @FlashScoped)● Implement the context API in an extension

Page 43: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

43 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Scope transparency

● Scopes not visible to client (no coupling)

● Scoped beans are proxied for thread safety

Page 44: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

44 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Conversation context

● Request ≤ Conversation ≪ Session

● Boundaries demarcated by application

● Optimistic transaction● Conversation-scoped persistence context● No fear of exceptions on lazy fetch operations

Page 45: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

45 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Controlling the conversation

@ConversationScopedpublic class BookingAgent {

@Inject @BookingDatabase EntityManager em; @Inject Conversation conversation;

private Hotel selected; private Booking booking;

public void select(Hotel h) { selected = em.find(Hotel.class, h.getId()); conversation.begin(); }

...

Page 46: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

46 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Controlling the conversation

...

public boolean confirm() { if (!isValid()) { return false; }

em.persist(booking); conversation.end(); return true; }}

Page 47: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

47 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

producer method

n. a method whose return value producesan injectable object

Page 48: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

48 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Producer method examples

@Produces @RequestScopedpublic FacesContext getFacesContext() { return FacesContext.getInstance();}

@Producespublic PaymentProcessor getPaymentProcessor( @Synchronous PaymentProcessor sync, @Asynchronous PaymentProcessor async) { return isSynchronous() ? sync : async;}

@Produces @SessionScoped @WishListpublic List<Product> getWishList() { return em.createQuery("...").getResultList();}

From non-beanFrom non-bean

Runtime selectionRuntime selection

Dynamic result setDynamic result set

Page 49: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

49 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Injecting producer return values

@Inject FacesContext ctx;

@Inject PaymentProcessor pp;

@Inject @WishList List<Product> wishlist;

Origin of product is hidden at injection point

Page 50: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

50 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Bridging Java EE resources

● Use producer field to expose Java EE resource@Statelesspublic class UserEntityManagerProducer { @Produces @UserRepository @PersistenceContext(unitName = "users") EntityManager em;}

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

Page 51: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

51 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Injecting resources in type-safe way

● String-based resource names are hiddenpublic class UserManager { @Inject @UserRepository EntityManager userEm; ...}

public class StockDisplay { @Inject @Prices Topic pricesTopic; ...}

Page 52: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

52 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Rethinking interceptors

@Interceptors( SecurityInterceptor.class, TransactionInterceptor.class, LoggingInterceptor.class)@Stateful public class BusinessComponent { ...}

Um, what's the point?

Page 53: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

53 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Define an interceptor binding type

@InterceptorBinding@Retention(RUNTIME)@Target({TYPE, METHOD})public @interface Secure {}

Page 54: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

54 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Mark the interceptor implementation

@Secure@Interceptorpublic class SecurityInterceptor {

@AroundInvoke public Object aroundInvoke(InvocationContext ctx) throws Exception { // enforce security... ctx.proceed(); }

}

Page 55: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

55 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Interceptor wiring with proper semantics

@Securepublic class AccountManager {

public boolean transfer(Account a, Account b) { ... }

}

Page 56: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

56 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Enabling and ordering interceptors

● Bean archive has no enabled interceptors by default

● Interceptors activated in beans.xml of bean archive● Referenced by binding type● Ordering is per-module● Declared in module in which the interceptor is used

<beans> <interceptors> <class>com.acme.SecurityInterceptor</class> <class>com.acme.TransactionInterceptor</class> </interceptors></beans>

Interceptors applied in order listedInterceptors applied in order listed

Page 57: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

57 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Annotation jam!

@Secure@Transactional@RequestScoped@Namedpublic class AccountManager {

public boolean transfer(Account a, Account b) { ... }

}

Page 58: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

58 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

stereotype

n. an annotation used to group commonarchitectural patterns (recurring roles)

Page 59: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

59 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Define a stereotype to bundle annotations

@Secure@Transactional@RequestScoped@Named@Stereotype@Retention(RUNTIME)@Target(TYPE)public @interface BusinessComponent {}

Page 60: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

60 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Using a stereotype

@BusinessComponentpublic class AccountManager {

public boolean transfer(Account a, Account b) { ... }

}

Page 61: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

61 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Portable extensions

● SPI – Service Provider Interface

● Automatically discovered

● Application-scoped instance

● Observes events from CDI event bus● Before/after bean discovery● After deployment validation● etc...

● Can override, augment, replace or veto beans

Page 62: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

62 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Page 63: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

63 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Weld: JSR-299 Reference Implementation

● Implementation & TCK

● Weld (portable) extensions

● Apache software licensed (version 2.0)

Page 64: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

64 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Page 65: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

65 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Seam’s mission statement

To provide a fully integrated development

platform for building rich Internet applicationsbased upon the Java EE environment.

Page 66: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

66 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Seam’s new modular ecosystem

Page 67: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

67 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Portable modules

● Module per domain or integration

● Independently...● lead● versioned● released

● Per-module structure● Based on CDI● API & implementation● Reference documentation & examples

Page 68: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

68 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Stack releases

Page 69: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

69 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

What's on the menu so far?

● Drools

● jBPM

● JMS

● Faces

● International

● Persistence

● JavaScript remoting

● Security

● Servlet

● Wicket

● XML configuration

● Exception handling

...and more http://github.com/seam

Page 70: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

70 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

XML-based configuration

<beans ... xmlns:app="java:urn:com.acme"> <app:TranslatingWelcome> <app:Translating/> <app:defaultLocale>en-US</app:defaultLocale> </app:TranslatingWelcome></beans>

● Define, specialize or override beans

● Add annotations (qualifiers, interceptor bindings, ...)

● Assign initial property values

Page 71: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

71 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Cross-field validator in Seam Faces

@FacesValidator("addressValidator")public class AddressValidator implements Validator {

@Inject Directory directory; @Inject @InputField String city; @Inject @InputField String state; @Inject @InputField ZipCode zip;

public void validate(FacesContext ctx, UIComponent c, Object v) throws ValidatorException { if (!directory.exists(city, state, zip) { throw new ValidatorException("Bad address"); } }}

Page 72: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

72 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Wiring the validator to the inputs

<h:form id="address"> City: <h:inputText id="city" value="#{bean.city}"/> State: <h:inputText id="state" value="#{bean.state}"/> Zip: <h:inputText id="zipCode" value="#{bean.zip}"/> <h:commandButton value="Update" action="#{addressController.update}"/> <s:validateForm validatorId="addressValidator" fields="zip=zipCode"></h:form>

Page 73: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

73 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Arquillian: Container-oriented testing for Java EE

@RunWith(Arquillian.class)public class GreeterTestCase {

@Deployment public static Archive<?> createDeployment() { return ShrinkWrap.create(JavaArchive.class) .addClasses(Greeter.class, GreeterBean.class); } @EJB private Greeter greeter; @Test public void shouldBeAbleToInvokeEJB() throws Exception { assertEquals("Hello, Earthlings", greeter.greet("Earthlings")); }}

Throwing complexity over the wallWed @ 4:45Hilton, Golden Gate 4/5

Throwing complexity over the wallWed @ 4:45Hilton, Golden Gate 4/5

Page 74: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

74 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

Summary

● Java EE 6 is leaner and more productive

● JSR-299 (CDI) provides a set of services for Java EE● Bridges JSF and EJB● Offers loose coupling with strong typingstrong typing● Provides a type-based event bus● Catalyzed managed bean & interceptor specifications● Extensive SPI for third-party integration with Java EE

● Weld: JSR-299 reference implementation & add-ons

● Seam 3: Portable extensions for Java EE

Page 75: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

75 JSR-299 (CDI), Weld and the Future of Seam | Dan Allen

How do I get started?

● Download a Java EE 6 container● JBoss AS 6 – http://jboss.org/jbossas● GlassFish V3 – http://glassfish.org

● Generate a Java EE project using a Maven archetype● http://tinyurl.com/goweld

● Read the Weld reference guide● http://tinyurl.com/weld-reference-101

● Browse the CDI JavaDoc● http://docs.jboss.org/cdi/api/latest/

● Check out the Seam 3 project● http://seamframework.org/Seam3

Page 76: JSR-299 (CDI), Weld & the Future of Seam (JavaOne 2010)

Q & A

Dan AllenPrincipal Software EngineerJBoss by Red Hat

http://seamframework.org/Weldhttp://seamframework.org/Seam3