cdi, weld and the future of seam
DESCRIPTION
Recorded on 2010-04-30 at the Northern Virginia Software Symposium, a stop on the NFJS 2010 tour, this talk introduces JSR-299: Contexts and Dependency Injection for the Java EE platform (CDI), the new Java standard for dependency injection and contextual lifecycle management. It also covers Weld, the JSR-299 reference implementation, and explains how CDI will be used as the foundation for the next version of Seam (Seam 3).TRANSCRIPT
![Page 1: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/1.jpg)
CDI (JSR-299), Weld andthe future of Seam
Dan AllenSenior Software EngineerJBoss by Red Hat
![Page 2: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/2.jpg)
2 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Who am I?
● Author of Seam in Action, Manning 2008
● Seam Community Liaison
● Weld, Seam & Arquillian project member
● JSR-314 (JSF 2) EG representative
● Open Source advocate
mojavelinux
![Page 3: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/3.jpg)
3 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Agenda
● Terminology
● Why JSR-299?
● JSR-299 themes
● Brief tour of programming model
● Weld
● Seam
![Page 4: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/4.jpg)
4 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Technology terminology
● CDI (JSR-299)● Contexts & dependency injection for Java EE
● Weld● JSR-299 Reference Implementation & TCK● Extended CDI support (Servlets, Java SE)● CDI enhancements for extension writers● Maven archetypes for CDI and Java EE
● Seam 3● Portable extensions for Java EE● Integrations with non-Java EE technologies
![Page 5: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/5.jpg)
5 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Stated goal of JSR-299
Web tier(JSF)
Transactional tier(EJB)
![Page 6: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/6.jpg)
6 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Why reinvest?
Seam 2
Java EE 5
![Page 7: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/7.jpg)
7 CDI (JSR-299), 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: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/8.jpg)
8 CDI (JSR-299), 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: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/9.jpg)
9 CDI (JSR-299), Weld and the future of Seam | Dan Allen
CDI: The big picture
● Fill in
● Catalyze
● Evolve
![Page 10: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/10.jpg)
10 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Why injection?
● Weakest aspect of Java EE 5
● Fixed set of injectable resources● @EJB● @PersistenceContext, @PersistenceUnit● @Resource (e.g., DataSource, UserTransaction)
● Name-based injection is fragile● Not “refactor friendly”● Requires special tooling to validate
![Page 11: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/11.jpg)
11 CDI (JSR-299), 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 12: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/12.jpg)
12 CDI (JSR-299), 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 13: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/13.jpg)
13 CDI (JSR-299), Weld and the future of Seam | Dan Allen
StrongStrong typing typing
● Type-based injection● Eliminate reliance on string-based names
● 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
![Page 14: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/14.jpg)
14 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Leverage and extend Java’s type system
@Annotation
Type
<TypeParam>
This information is pretty useful!
![Page 15: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/15.jpg)
15 CDI (JSR-299), 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● CDI
● Need a “unified bean definition”
![Page 16: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/16.jpg)
16 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Managed bean specification
● Common bean definition
● Instances are managed by the container
● Common services● Interceptors● Resource injections● Lifecycle callbacks
● Foundation spec
How managed beans evolved: http://www.infoq.com/news/2009/11/weld10
JSF EJB CDI JAX-RS
ManagedBeans
![Page 17: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/17.jpg)
17 CDI (JSR-299), Weld and the future of Seam | Dan Allen
CDI bean ingredients
● Auto-discovered
● Set of bean types
● Set of qualifiers
● Scope
● Bean EL name (optional)
● Set of interceptor bindings
● Alternative classification
● Bean implementation class
![Page 18: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/18.jpg)
18 CDI (JSR-299), 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: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/19.jpg)
19 CDI (JSR-299), 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: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/20.jpg)
20 CDI (JSR-299), 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: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/21.jpg)
21 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Injection 101
public class Greeter { @Inject Welcome w;
public void welcome() { System.out.println( w.buildPhrase("Reston")); }}
@Default qualifier implied@Default qualifier implied
![Page 22: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/22.jpg)
22 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Where can it be injected?
● Field
● Method parameter● Constructor*● Initializer● Producer● Observer
![Page 23: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/23.jpg)
23 CDI (JSR-299), 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: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/24.jpg)
24 CDI (JSR-299), 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: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/25.jpg)
25 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Quiz: Now which implementation gets injected?
public class Greeter { private Welcome welcome; @Inject void init(Welcome welcome) { this.welcome = welcome; }
...}
It's ambiguous!
![Page 26: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/26.jpg)
26 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Settling an ambiguous resolution
● Qualifier
● Alternative
● Hide bean types
![Page 27: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/27.jpg)
27 CDI (JSR-299), Weld and the future of Seam | Dan Allen
qualifier
n. an annotation used to resolve an APIimplementation variant at an injection point
![Page 28: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/28.jpg)
28 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Defining a qualifier
● A qualifier is an annotation@Qualifier@Retention(RUNTIME)@Target({TYPE, METHOD, FIELD, PARAMETER})public @interface Translating {}
![Page 29: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/29.jpg)
29 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Qualifying an implementation
● Adding a qualifier annotation:● makes type more specific● assigns semantic meaning
@Translatingpublic class TranslatingWelcome extends Welcome {
@Inject GoogleTranslator translator;
public String buildPhrase(String city) { return translator.translate( "Welcome to " + city + "!"); } }
![Page 30: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/30.jpg)
30 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Qualifier as a “binding type”
![Page 31: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/31.jpg)
31 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Using a specific implementation
● Qualified implementation must be requested explicitly● Akin to the factory method pattern
● Resolves ambiguity at injection point!public class Greeter { private Welcome welcome;
@Inject void init(@Translating Welcome welcome) { this.welcome = welcome }
public void welcomeVisitors() { System.out.println( welcome.buildPhrase("Reston")); }}
No reference to implementation class!No reference to implementation class!
![Page 32: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/32.jpg)
32 CDI (JSR-299), 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
Put simply: an override
![Page 33: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/33.jpg)
33 CDI (JSR-299), 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: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/34.jpg)
34 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Substituting the alternative
● Implementation must be activated using beans.xml<beans> <alternatives> <class>com.acme.TranslatingWelcome</class> </alternatives></beans>
![Page 35: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/35.jpg)
35 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Assigning a bean 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("Reston")); }}
![Page 36: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/36.jpg)
36 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Assigning a bean 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("Reston")); }}
Bean name is decapitalizedsimple class name Bean name is decapitalizedsimple class name
![Page 37: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/37.jpg)
37 CDI (JSR-299), 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: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/38.jpg)
38 CDI (JSR-299), Weld and the future of Seam | Dan Allen
CDI
JSFmanaged
beans
Facelets
JSP
![Page 39: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/39.jpg)
39 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Stashing the bean in a context
● Bean saved for the duration of a request@RequestScoped@Named("greeter")public class Greeter { private Welcome welcome; private String city; // getter and setter hidden
@Inject void init(Welcome welcome) { this.welcome = welcome }
public void welcomeVisitors() { System.out.println(welcome.buildPhrase(city)); }}
![Page 40: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/40.jpg)
40 CDI (JSR-299), 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>
● Satisfies initial goal of JSR-299● ...in fact, integrates JSF and any managed bean
![Page 41: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/41.jpg)
41 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Scope types and contexts
● Absence of 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 42: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/42.jpg)
42 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Scope transparency
● Scopes not visible to client● No coupling between scope and use of type● Scoped beans are proxied for thread safety
![Page 43: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/43.jpg)
43 CDI (JSR-299), 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 44: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/44.jpg)
44 CDI (JSR-299), 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 45: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/45.jpg)
45 CDI (JSR-299), 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 46: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/46.jpg)
46 CDI (JSR-299), Weld and the future of Seam | Dan Allen
producer method
n. a method whose return value producesan injectable object
![Page 47: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/47.jpg)
47 CDI (JSR-299), 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
Custom setupCustom setup
![Page 48: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/48.jpg)
48 CDI (JSR-299), 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 = "userPU") EntityManager em;}
@Statelesspublic class PricesTopicProducer { @Produces @Prices @Resource(name = "java:global/env/jms/Prices") Topic pricesTopic;}
![Page 49: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/49.jpg)
49 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Injecting resource in type-safe way
● String-based resource names are hiddenpublic class UserManager { @Inject @UserRepository EntityManager userEm; ...}
public class StockDisplay { @Inject @Prices Topic pricesTopic; ...}
![Page 50: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/50.jpg)
50 CDI (JSR-299), 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 51: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/51.jpg)
51 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Interceptor wiring in JSR-299 (1)
● Define an interceptor binding type@InterceptorBinding@Retention(RUNTIME)@Target({TYPE, METHOD})public @interface Secure {}
![Page 52: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/52.jpg)
52 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Interceptor wiring in JSR-299 (2)
● Marking the interceptor implementation@Secure@Interceptorpublic class SecurityInterceptor {
@AroundInvoke public Object aroundInvoke(InvocationContext ctx) throws Exception { // ...enforce security... ctx.proceed(); }
}
![Page 53: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/53.jpg)
53 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Interceptor wiring in JSR-299 (3)
● Applying interceptor with proper semantics@Securepublic class AccountManager {
public boolean transfer(Account a, Account b) { ... }
}
![Page 54: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/54.jpg)
54 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Multiple interceptors
● Application developer only concerned about semantics@Transactionalpublic class AccountManager {
@Secure public boolean transfer(Account a, Account b) { ... }
}
![Page 55: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/55.jpg)
55 CDI (JSR-299), 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 56: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/56.jpg)
56 CDI (JSR-299), Weld and the future of 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 57: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/57.jpg)
57 CDI (JSR-299), Weld and the future of 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 58: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/58.jpg)
58 CDI (JSR-299), Weld and the future of 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 59: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/59.jpg)
59 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Annotation jam!
● Without stereotypes, annotations pile uppublic@Secure@Transactional@RequestScoped@Namedclass AccountManager {
public boolean transfer(Account a, Account b) { ... }
}
![Page 60: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/60.jpg)
60 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Defining a stereotype
● Stereotypes are annotations that group annotationspublic@Secure@Transactional@RequestScoped@Named@Stereotype@Retention(RUNTIME)@Target(TYPE)@interface BusinessComponent {}
![Page 61: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/61.jpg)
61 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Using a stereotype
● Stereotypes give a clear picture, keep things simplepublic@BusinessComponentclass AccountManager {
public boolean transfer(Account a, Account b) { ... }
}
![Page 62: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/62.jpg)
62 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Events
● Completely decouple action and reactions
● Selectors tune which event notifications are received
● Can be observed:● immediately,● after transaction completion or● asynchronously (via extension)
![Page 63: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/63.jpg)
63 CDI (JSR-299), Weld and the future of 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 64: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/64.jpg)
64 CDI (JSR-299), Weld and the future of 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 65: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/65.jpg)
65 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Weld: JSR-299 Reference Implementation
● Developed under Seam project umbrella
● Version 1.0.1 available
● Bundled in JBoss AS 6 & GlassFish V3
● Support for Tomcat, Jetty & Java SE
● CDI enhancements & utilities for extension writers
● Maven archetypes for CDI & Java EE
![Page 66: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/66.jpg)
66 CDI (JSR-299), 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 67: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/67.jpg)
67 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Seam 3: Key themes
● Portability● Portable extensions run on any CDI implementation
● Modularity (i.e., Seam à la carte)● Module per integration● Individual module leads● Independent release cycles
● “Stack” releases
● Strong tooling● Java compiler● JBoss Tools
![Page 68: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/68.jpg)
68 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Seam's new modular ecosystem
![Page 69: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/69.jpg)
69 CDI (JSR-299), Weld and the future of Seam | Dan Allen
What's on the menu so far?
● Drools
● JMS
● Faces
● International
● Persistence
● JavaScript Remoting
● Security
● Servlet
● Wicket
● XML configuration
...and more http://sfwk.org/Seam3
![Page 70: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/70.jpg)
70 CDI (JSR-299), 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: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/71.jpg)
71 CDI (JSR-299), Weld and the future of Seam | Dan Allen
End-to-end testing for Java EE
● SeamTest replacement
● ShrinkWrap● Fluent API for creating Java archives
JavaArchive archive = ShrinkWrap.create("archive.jar", JavaArchive.class) .addClasses(MyClass.class, MyOtherClass.class) .addResource("mystuff.properties");
● Arquillian● Integration test harness● Pluggable container support (embedded or remote)● Supports injection into test class● Tests can be run from IDE or build script
![Page 72: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/72.jpg)
72 CDI (JSR-299), Weld and the future of Seam | Dan Allen
![Page 73: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/73.jpg)
73 CDI (JSR-299), Weld and the future of Seam | Dan Allen
Summary
● JSR-299 provides a set of services for Java EE● Bridges JSF and EJB● Offers loose coupling with strong typingstrong typing● Catalyzed managed bean & interceptor specifications
● Extensive SPI for third-party integration with Java EE
● Weld: JSR-299 Reference Implementation
● Seam 3: Portable extensions for Java EE
![Page 74: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/74.jpg)
74 CDI (JSR-299), Weld and the future of Seam | Dan Allen
How do I get started with CDI or Java EE 6?
● Seam 3 projecthttp://seamframework.org/Seam3
● Weld Maven archetypes for CDI and Java EEhttp://tinyurl.com/goweld
● Weld reference guidehttp://tinyurl.com/weld-reference-101
● CDI JavaDochttp://docs.jboss.org/cdi/api/latest/
● Any Java EE 6 container!
![Page 75: CDI, Weld and the future of Seam](https://reader034.vdocuments.site/reader034/viewer/2022052505/554c0d47b4c9058e098b58c8/html5/thumbnails/75.jpg)
Q & A
Dan AllenSenior Software EngineerJBoss, by Red Hat
http://in.relation.tohttp://seamframework.org/Seam3