Top Banner
CDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat
77

CDI (JSR-299), Weld and the future of · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

Mar 11, 2018

Download

Documents

trinhdan
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: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

CDI (JSR-299), Weldand the future of Seam

Dan AllenSenior Software EngineerJBoss, by Red Hat

Page 2: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

2 CDI, Weld and Seam | Dan Allen

Who am I?

● Author of Seam in Action, Manning 2008

● Seam and Weld project member

● JSR-314 (JSF 2.0) EG member

● Champion for openness

Page 3: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

3 CDI, Weld and Seam | Dan Allen

Project terminology

● CDI (JSR-299)● Contexts & dependency injection for the Java EE platform

● Weld● JSR-299 Reference Implementation● Bootstrap outside of Java EE (Servlet, Java SE)

● Seam● Portable extensions for the Java EE platform● Integrations with non-Java EE technologies● Akin to ecosystem of JSF component libraries

Page 4: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

4 CDI, Weld and Seam | Dan Allen

What JSR-299 (CDI) provides

● A powerful new set of services for Java EE components● Lifecycle management for stateful components bound to

well-defined contexts (+ new conversation context)● A type-safe approach to dependency injection● Interaction via an event notification facility● Reduced coupling between interceptors and beans● Decorators—interceptors better suited for solving

business concerns● Unified EL integration (named beans)● An SPI for developing portable extensions for the

Java EE platform

Page 5: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

5 CDI, Weld and Seam | Dan Allen

JSR-299: The big picture

● Fills a major hole in the Java EE platform

● A catalyst for emerging Java EE specs

● Excels at solving goal

Page 6: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

6 CDI, Weld and Seam | Dan Allen

Stated goal of JSR-299

Web tier(JSF)

Transactional tier(EJB)

Page 7: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

7 CDI, Weld and Seam | Dan Allen

Going beyond Seam

● Solve JSF-EJB integration problem at platform level

● Get an expert group (EG) involved● Buy-in from broader Java EE community ● Formulate a more robust design● Establish foundation for an

ecosystem of extensions

Page 8: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

8 CDI, Weld and Seam | Dan Allen

Your bean is my bean

● Everyone trying to solve the same problem● JSF, EJB, CDI (JSR-299), Seam, Spring, Guice, etc.

● Need a “unified bean definition”

Page 9: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

9 CDI, Weld and Seam | Dan Allen

Managed bean

● Common bean definition

● Life cycle of instance managed by container

● Basic set of services● Resource injection● Lifecycle callbacks● Interceptors

● Foundation on which other specs can build

Managed bean

JSF EJB CDI

Read about how managed beans evolved: http://www.infoq.com/news/2009/11/weld10

Page 10: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

10 CDI, Weld and Seam | Dan Allen

CDI replaces JSF managed beans

CDI

JSF managed beans

Facelets

JSP

Page 11: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

11 CDI, Weld and Seam | Dan Allen

Why injection?

● Injection is the weakest aspect of Java EE

● Existing annotations pertain to specific components● @EJB● @PersistenceContext, @PersistenceUnit● @Resource (e.g., DataSource, UserTransaction)

● Third-party solutions rely on name-based injection● Not type-safe● Fragile● Requires special tooling to validate

Page 12: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

12 CDI, Weld and Seam | Dan Allen

Leverage and extend Java’s type system

● JSR-299 introduces creative use of annotations

● Annotations considered part of type

● Comprehensive generics support

● Why augment type?● Can’t always rely on class extension (e.g., primitives)● Avoid hard dependency between client and impl● Don’t rely on weak association of field bean name● Validation can be done at startup

Page 13: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

13 CDI, Weld and 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 14: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

14 CDI, Weld and 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 message consumer● Events

Page 15: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

15 CDI, Weld and Seam | Dan Allen

StrongStrong typing typing

● Eliminate reliance on string-based names

● Compiler can detect typing errors● No special authoring tools required for code completion● Casting virtually eliminated

● Semantic code errors detected at application startup● Tooling can detect ambiguous dependencies

Page 16: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

16 CDI, Weld and Seam | Dan Allen

What can be injected?

● Defined by the specification● Almost any plain Java class (managed beans)● EJB session beans● Objects returned by producer methods or fields● Java EE resources (e.g., Datasource, UserTransaction)● Persistence units and persistence contexts● Web service references● Remote EJB references

● SPI allows third-party frameworks to introduce additional injectable objects

● Annotations aligned with JSR-330

Page 17: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

17 CDI, Weld and Seam | Dan Allen

CDI bean

● Set of bean types (non-empty)

● Set of qualifiers (non-empty)

● Scope

● Bean EL name (optional)

● Alternatives

● Set of interceptor bindings

● Bean implementation

Page 18: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

18 CDI, Weld and Seam | Dan Allen

Bean services with CDI

● @ManagedBean annotation not required (implicit)

● Transparent create/destroy and scoping of instance

● Type-safe resolution at injection or lookup

● Name-based resolution when used in EL expression

● Lifecycle callbacks

● Method interception and decoration

● Event notification

Page 19: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

19 CDI, Weld and Seam | Dan Allen

Welcome to CDI (managed bean version)

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

● When is a bean recognized?

/META-INF/beans.xml in same classpath entry

Page 20: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

20 CDI, Weld and Seam | Dan Allen

Welcome to CDI (session bean version)

public@Statelessclass WelcomeBean implements Welcome { public String buildPhrase(String city) { return "Welcome to " + city + "!"; }}

Page 21: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

21 CDI, Weld and Seam | Dan Allen

A simple client: field injection

public class Greeter { @Inject Welcome welcome;

public void welcome() { System.out.println( welcome.buildPhrase("Orlando")); }}

@Default qualifier implied@Default qualifier implied

Page 22: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

22 CDI, Weld and Seam | Dan Allen

A simple client: constructor injection

public class Greeter { Welcome welcome;

@Inject public Greeter(Welcome welcome) { this.welcome = welcome; }

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

Designates the constructorCDI should invokeDesignates the constructorCDI should invoke

Page 23: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

23 CDI, Weld and Seam | Dan Allen

A simple client: initializer injection

public class Greeter { Welcome welcome;

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

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

Designates the initializermethod CDI should invokeDesignates the initializermethod CDI should invoke

Page 24: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

24 CDI, Weld and Seam | Dan Allen

Multiple implementations

● Two scenarios:● Multiple implementations of same interface● One implementation extends another

public class TranslatingWelcome extends Welcome {

@Inject GoogleTranslator translator;

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

● Which implementation should be selected for injection?

Page 25: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

25 CDI, Weld and Seam | Dan Allen

Qualifier

An annotation used to resolve a implementation

variant of an API at an injection point

Page 26: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

26 CDI, Weld and Seam | Dan Allen

Defining a qualifier

● A qualifier is an annotationpublic@Qualifier@Retention(RUNTIME)@Target({TYPE, METHOD, FIELD, PARAMETER})@interface Translating {}

Page 27: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

27 CDI, Weld and Seam | Dan Allen

Qualifying an implementation

● Add qualifier annotation to make type more specificpublic@Translatingclass TranslatingWelcome extends Welcome {

@Inject GoogleTranslator translator;

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

● Resolves ambiguity at injection point● There can never been an ambiguity when resolving!

Page 28: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

28 CDI, Weld and Seam | Dan Allen

Using a specific implementation

● Must request to use qualified implementation explicitly● Otherwise you get unqualified implementation

public class Greeter { Welcome welcome;

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

public void welcomeVisitors() { System.out.println( welcome.buildPhrase("Mountain View")); }}

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

Page 29: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

29 CDI, Weld and 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

Put simply: an override

Page 30: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

30 CDI, Weld and Seam | Dan Allen

Defining an alternative

public@Alternative@Specializesclass TranslatingWelcome extends Welcome {

@Inject GoogleTranslator translator;

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

Page 31: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

31 CDI, Weld and Seam | Dan Allen

Substituting the alternative

● Implementation activated using deployment-specific /META-INF/beans.xml resource

<beans> <alternatives> <class>com.acme.TranslatingWelcome</class> </alternatives></beans>

● Could also enable alternative by introducing and activating an intermediate annotation

Page 32: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

32 CDI, Weld and Seam | Dan Allen

Assigning a bean name

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

@Inject public Greeter(Welcome welcome) { this.welcome = welcome; }

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

Same as default name whenno annotation value specified Same as default name whenno annotation value specified

Page 33: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

33 CDI, Weld and Seam | Dan Allen

Assigning a bean name

public@Namedclass Greeter { Welcome welcome;

@Inject public Greeter(Welcome welcome) { this.welcome = welcome; }

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

Page 34: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

34 CDI, Weld and Seam | Dan Allen

Collapsing layers

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

● But we still need the bean to be stored in a scope

Page 35: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

35 CDI, Weld and Seam | Dan Allen

A stateful bean

● Declare bean to be saved for duration of requestpublic@RequestScoped@Named("greeter")class Greeter { Welcome welcome; private String city; // getter and setter hidden

@Inject public Greeter(Welcome welcome) { this.welcome = welcome }

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

Page 36: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

36 CDI, Weld and 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>

● Satisfies initial goal of integrating JSF and EJB● Except in this case, it extends to plain managed beans

Page 37: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

37 CDI, Weld and Seam | Dan Allen

Scope types and contexts

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

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

● JSF conversation scope - @ConversationScoped

● Custom scopes● Define scope type annotation (i.e., @FlashScoped)● Implement context API

Page 38: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

38 CDI, Weld and Seam | Dan Allen

Scope transparency

● Scopes are not visible to client● No coupling between scope and use of type● Scoped beans are proxied for thread safety

Page 39: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

39 CDI, Weld and Seam | Dan Allen

Scoping a collaborating bean

public@SessionScopedclass Profile { private Identity identity;

public void register() { identity = ...; }

public Identity getIdentity() { return identity; }}

Page 40: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

40 CDI, Weld and Seam | Dan Allen

Collaboration between stateful beans

public@Named @RequestScopedclass Greeter { Welcome welcome; Profile profile; private String city;

@Inject public Greeter(Welcome welcome, Profile profile) { this.welcome = welcome; this.profile = profile; } ...

public void welcomeVisitors() { System.out.println(welcome.buildPhrase( profile.getIdentity(), city)); }}

No awareness of scopeNo awareness of scope

Page 41: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

41 CDI, Weld and 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 42: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

42 CDI, Weld and Seam | Dan Allen

Controlling the conversation

public@ConversationScopedclass 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 43: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

43 CDI, Weld and Seam | Dan Allen

Controlling the conversation

...

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

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

Page 44: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

44 CDI, Weld and Seam | Dan Allen

Producer method

A method whose return value is an injectable object

Used for:● Types which you cannot modify● Runtime selection of a bean instance● When you need to do extra and/or conditional setup of a

bean instance

● Roughly equivalent to Seam’s @Factory annotation

Page 45: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

45 CDI, Weld and Seam | Dan Allen

Producer method examples

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

@Produces @SessionScoped @WishListpublic List<Product> getWishList() { ... }

Page 46: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

46 CDI, Weld and Seam | Dan Allen

Bridging Java EE resources

● Use producer field to expose Java EE resourcepublic@Statelessclass UserEntityManagerFactory { @Produces @UserRepo @PersistenceUnit(unitName = "userPU") EntityManagerFactory emf;}

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

Java EE 6 global JNDI nameJava EE 6 global JNDI name

Java EE resource annotationsJava EE resource annotations

Page 47: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

47 CDI, Weld and Seam | Dan Allen

Injecting resource in type-safe way

● String-based resource names are hiddenpublic class UserManager { @Inject @UserRepo EntityManagerFactory emf; ...}

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

Page 48: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

48 CDI, Weld and Seam | Dan Allen

Promoting state

● Producer methods can promote state as injectable objectpublic@RequestScopedclass Profile { private Identity identity;

public void register() { identity = ...; }

@Produces @SessionScoped public Identity getIdentity() { return identity; }}

Could also declarequalifiers and/or EL nameCould also declarequalifiers and/or EL name

Page 49: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

49 CDI, Weld and Seam | Dan Allen

Using promoted state

public@RequestScoped @Namedclass Greeter { Welcome welcome; Identity identity; private String city;

@Inject public Greeter(Welcome welcome, Identity ident) { this.welcome = welcome; this.identity = ident; } ...

public void welcomeVisitors() { System.out.println( welcome.buildPhrase(identity, city)); }}

No awareness of scopeNo awareness of scope

Page 50: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

50 CDI, Weld and Seam | Dan Allen

Rethinking interceptors

● Interceptors handle orthogonal concerns

● Java EE 5 interceptors bound directly to component● @Interceptors annotation on bean type

● What’s the problem?● Shouldn’t be coupled to implementation

● Requires level of indirection

● Should be deployment-specific● Tests vs production● Opt-in best strategy for enabling

● Ordering should be defined centrally

Page 51: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

51 CDI, Weld and Seam | Dan Allen

Interceptor wiring in JSR-299 (1)

● Define an interceptor binding typepublic@InterceptorBinding@Retention(RUNTIME)@Target({TYPE, METHOD})@interface Secure {}

Page 52: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

52 CDI, Weld and Seam | Dan Allen

Interceptor wiring in JSR-299 (2)

● Marking the interceptor implementationpublic@Secure@Interceptorclass SecurityInterceptor {

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

}

Page 53: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

53 CDI, Weld and Seam | Dan Allen

Interceptor wiring in JSR-299 (3)

● Applying interceptor to class with proper semanticspublic@Secureclass AccountManager {

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

}

Page 54: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

54 CDI, Weld and Seam | Dan Allen

Interceptor wiring in JSR-299 (4)

● Applying interceptor to method with proper semanticspublic class AccountManager {

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

}

Page 55: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

55 CDI, Weld and Seam | Dan Allen

Multiple interceptors

● Application developer only worries about semanticspublic@Transactionalclass AccountManager {

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

}

Page 56: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

56 CDI, Weld and Seam | Dan Allen

Enabling and ordering interceptors

● Interceptors referenced by binding type

● Specify binding type in /META-INF/beans.xml to activate<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: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

57 CDI, Weld and Seam | Dan Allen

Composite interceptor bindings

● Interceptor binding types can be meta-annotationspublic@Secure@Transactional@InterceptorBinding@Retention(RUNTIME)@Target(TYPE)@interface BusinessOperation {}

Order does not matterOrder does not matter

Page 58: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

58 CDI, Weld and Seam | Dan Allen

Multiple interceptors (but you won’t know it)

● Interceptors inherited from composite binding typespublic@BusinessOperationclass AccountManager {

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

}

Page 59: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

59 CDI, Weld and Seam | Dan Allen

Wrap up annotations using stereotypes

● Common architectural patterns – recurring roles

● A stereotype packages:● A default scope● A set of interceptor bindings● The ability to that beans are named● The ability to specify that beans are alternatives

Page 60: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

60 CDI, Weld and Seam | Dan Allen

Annotation jam

● Without stereotypes, annotations pile uppublic@Secure@Transactional@RequestScoped@Namedclass AccountManager {

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

}

Page 61: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

61 CDI, Weld and Seam | Dan Allen

Defining a stereotype

● Stereotypes are annotations that group annotationspublic@Secure@Transactional@RequestScoped@Named@Stereotype@Retention(RUNTIME)@Target(TYPE)@interface BusinessComponent {}

Page 62: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

62 CDI, Weld and Seam | Dan Allen

Using a stereotype

● Stereotypes give a clear picture, keep things simplepublic@BusinessComponentclass AccountManager {

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

}

Page 63: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

63 CDI, Weld and Seam | Dan Allen

Decorators

● Intercept invocations for a particular Java interface

● Aware of semantics

● Complement interceptors

● Enabled in same way as interceptors

Page 64: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

64 CDI, Weld and Seam | Dan Allen

Decorator example

public@Decoratorabstract class LargeTxDecorator implements Account { @Inject @Delegate @Any Account account; @PersistenceContext EntityManager em;

public void withdraw(BigDecimal amount) { account.withdraw(amount); if (amount.compareTo(LARGE_AMOUNT) > 0) { em.persist(new LoggedWithdrawl(amount)); } }

}

Page 65: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

65 CDI, Weld and Seam | Dan Allen

Events

● Completely decouples action and reactions

● Observers can use selectors to tune which event notifications are received

● Events can be observed immediately, at end of transaction or asynchronously

Page 66: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

66 CDI, Weld and Seam | Dan Allen

Firing an event

public class GroundController { @Inject @Landing Event<Flight> flightLanding;

public void clearForLanding(String flightNum) { flightLanding.fire(new Flight(flightNum)); }}

Event instance withtype-safe payloadEvent instance withtype-safe payload

Page 67: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

67 CDI, Weld and Seam | Dan Allen

An event observer

public class GateServices { public void onIncomingFlight( @Observes @Landing Flight flight, Greeter greeter, CateringService cateringService) { Gate gate = ...; flight.setGate(gate); cateringService.dispatch(gate); greeter.welcomeVisitors(); }}

Takes event API type withadditional binding typeTakes event API type withadditional binding type

Additional parameters areinjected by the containerAdditional parameters areinjected by the container

Page 68: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

68 CDI, Weld and Seam | Dan Allen

Weld

● JSR-299 reference implementation

● Developed under the Seam project umbrella

● Version 1.0.0 available, including Maven archetypes!

● Bundled in JBoss AS 6 and GlassFish V3

● Runs on Tomcat, Jetty and Java SE

WeldWeld

CoreCore

OSGiOSGi

TCK execution

TCK execution

Integrator SPI

Integrator SPI

ExtensionsExtensions

ServletServlet

Java SEJava SECDI TCKCDI TCK

TCKTCK

Page 69: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

69 CDI, Weld and 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 70: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

70 CDI, Weld and Seam | Dan Allen

Seam is our future

“The future for all of our projects and platform is Seam.”

“[Developers] won't have to worry about learning a new component model when they move between platforms.”

- Mark Little, JBoss CTO

http://blogs.jboss.org/blog/mlittle/2009/11/11/The_future_of_component_models.txt

Page 71: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

71 CDI, Weld and Seam | Dan Allen

Seam framework stack

● CDI foundation

● Enhanced, declarative security

● Support for multiple view layers (JSF 2, Wicket, Flex)

● JavaScript remoting (a la DWR)

● RESTeasy integration

● Bridges to Seam 2, Spring and Guice

● Email, graphics, PDF and XLS

● Pageflows and business processes

● JBoss Toolshttp://in.relation.to/Bloggers/HowToStartLearningJavaEE6

Page 72: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

72 CDI, Weld and Seam | Dan Allen

Ecosystem architecture

Seam FrameworkSeam Framework

Seam SecuritySeam

Security Seam MailSeam Mail Drools 5 support

Drools 5 support

RESTEasyintegration

RESTEasyintegration

WicketsupportWicketsupport ......

CDI 1.0

WeldWeld

CoreCore CDI TCKCDI TCK

OSGiOSGi

Tomcat/Jetty

JBoss AS > 5.2

GlassFish V3

IntegrationIntegration

ServletServlet

IntegrationIntegration

OpenOpenWeb BeansWeb Beans

Page 73: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

73 CDI, Weld and Seam | Dan Allen

Seam 3: Key themes

● Modularity● Seam à la carte

● Portability● Run on any CDI implementation

● Full stack● Similar to Eclipse’s coordinated release

Page 74: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

74 CDI, Weld and Seam | Dan Allen

Drawing the line

● Unportable extension (UE)● Integrates with proprietary SPIs in Weld

● Portable extension (PE) - Weld● Simple or general purpose● Doesn’t pull in extra dependencies

● Portable extension (PE) - Seam● Everything else

Page 75: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

75 CDI, Weld and Seam | Dan Allen

End-to-end testing

● SeamTest modularized

● ShrinkWrap● Declarative creation of archives, made simple

JavaArchive archive = Archives.create("archive.jar", JavaArchive.class) .addClasses(MyClass.class,MyOtherClass.class) .addResource("mystuff.properties");

● Arquillian● Pluggable unit test● Standalone and in-container POJO tests● @RunWith(Arquillian.class)

Page 76: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

76 CDI, Weld and Seam | Dan Allen

Summary

● JSR-299 provides a set of services for Java EE● Satisfies original goal to bridge JSF and EJB● Offers loose coupling with strong typingstrong typing● Catalyzed the managed bean specification

● Other problems needed to be solved● Robust dependency injection and context model● Event notification facility, furthering the loose coupling● Extensive SPI for third-parties to integrate with Java EE

● Weld: JSR-299 Reference Implementation

● Seam: Portable extensions for Java EE

Page 77: CDI (JSR-299), Weld and the future of  · PDF fileCDI (JSR-299), Weld and the future of Seam Dan Allen Senior Software Engineer JBoss, by Red Hat

Q & A

Dan AllenSenior Software EngineerJBoss, by Red Hat

http://in.relation.tohttp://seamframework.org