an introduction to seam 3
DESCRIPTION
An Introduction to Seam 3. Reza Rahman Expert Group Member, Java EE 6 and EJB 3.1 Resin EJB 3.1 Lite Container Lead Author, EJB 3 in Action [email protected]. CDI, Weld and Seam 3. CDI is the dependency injection standard for Java EE Weld is the reference implementation of CDI - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/1.jpg)
An Introduction to Seam 3
Reza RahmanExpert Group Member, Java EE 6 and EJB 3.1Resin EJB 3.1 Lite Container LeadAuthor, EJB 3 in [email protected]
![Page 2: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/2.jpg)
CDI, Weld and Seam 3CDI is the dependency injection standard for Java EE
Weld is the reference implementation of CDI
Seam 3 is a set of CDI portable extension modules
![Page 3: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/3.jpg)
Contexts and Dependency Injectionfor Java EE (CDI)
![Page 4: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/4.jpg)
CDI Overview
Next-generation dependency injection for Java EE
Also known as JSR-299, formerly known as WebBeans
Synthesizes best ideas from Seam 2, Guice and Spring
Many innovative features on its own right
Focus on loose-coupling, Java-centric type-safety,
annotations, expressiveness and ease-of-use
Makes Java EE much more flexible, testable, pluggable
and extensible
![Page 5: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/5.jpg)
CDI Features
Basic dependency injection
• @Inject, @Qualifier, @Stereotype, @Alternative,
Instance, @All, @Any
Component naming
• @Named
Context management
• @Dependent, @RequestScoped, @SessionScoped,
@ConversationScoped, @ApplicationScoped,
@Scope
![Page 6: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/6.jpg)
CDI Features
Custom Object Factories
• @Produces, @Disposes, InjectionPoint
Lightweight Events
• Event, @Observes
Interceptors/Decorators
• @Interceptor, @InterceptorBinding, @AroundInvoke, InvocationContext, @Decorator, @Delegate
Portable extensions SPI*
![Page 7: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/7.jpg)
JBoss Weld
![Page 8: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/8.jpg)
Weld Overview
CDI reference implementation and compatibility test kit
from JBoss
Included in GlassFish and JBoss AS
Can be used with Tomcat, Jetty
Can be used in Java SE
![Page 9: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/9.jpg)
Weld Runtime Environments
![Page 10: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/10.jpg)
Weld Event Based Java SE Bootstrap
@Singletonpublic class AlertListener { ... public void init( @Observes ContainerInitialized event, @Parameters List<String> parameters) { showSplash(); setupUi(); showUi(parameters); } ...}
![Page 11: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/11.jpg)
Weld Java SE Programmatic Bootstrap
public static void main(String[] arguments) { WeldContainer weld = new Weld().initialize();
weld.instance().select( AlertListenerBean.class).startListening();
weld.shutdown();}
![Page 12: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/12.jpg)
JBoss Seam 3
![Page 13: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/13.jpg)
Seam 3Set of CDI portable extensions independent of Weld
Each module developed separately
There are Seam “umbrella” releases
![Page 14: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/14.jpg)
Module Description
Solder General enhancements to CDI
XML Configuration XML configuration for managed beans
Persistence Transactions and persistence for non-EJB managed
beans
Faces JSF enhancements
Servlet Servlet enhancements
JMS JMS integration
REST REST enhancements
Remoting JavaScript remoting of managed beans
Seam Modules
![Page 15: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/15.jpg)
Module Description
Catch Exception handling framework
International Internationalized/localized locale, time, messages
Security Higher level security API for Java EE
Mail JavaMail integration
Cron CDI based scheduling
Document Document generation (PDF, Excel)
Seam 2 Seam 2 backwards compatibility
Spring Spring interoperability
Seam Modules
![Page 16: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/16.jpg)
Module Description
Wicket Wicket Integration
GWT GWT integration
Drools Drools integration
jBPM jBPM integration
JBoss ESB JBoss ESB integration
Clouds Cloud frameworks integration (JClouds, Infinispan)
Forge Rapid application development for Java EE
Seam Modules
![Page 17: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/17.jpg)
Solder
Used to be part of Weld, good laboratory for CDI.next
Generally useful CDI extensions for application, plug-in
and framework developers
Logging
Injecting EL expression evaluator
Extended resource loading
Extended APIs for injection, naming, management
Annotation processing and reflection utilities
![Page 18: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/18.jpg)
Logging
@MessageLoggerpublic interface AccountLog { ... @LogMessage @Message(“Account %s was overdrawn by %10.2f") public void overdrawn(String accountNumber, double amount); ...}
@Inject @Category(“accounts") @Locale("en_GB") AccountLog log;
log.overdrawn(account.getNumber(), amount);
![Page 19: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/19.jpg)
Programmatic EL Evaluation
@Injectprivate Expressions expressions;...Customer customer = expressions.evaluateValueExpression( "#{account.customer}");...Double interest = expressions.evaluateMethodExpression( "#{account.calculateMonthlyInterest}");
![Page 20: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/20.jpg)
Enhanced Resource Handling
@Inject@Resource("WEB-INF/beans.xml")private URL beansXml;
@Inject@Resource("WEB-INF/web.xml")private InputStream webXml;
@Inject@Resource("META-INF/aws.properties")private Properties awsProperties;
![Page 21: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/21.jpg)
XML Configuration
CDI Focuses mainly on annotations, not XML
XML configuration is sometimes needed or preferred
Seam XML configuration allows portable CDI XML
configuration across implementations
More compact, schema-driven and type-safe than
traditional XML configuration
Likely candidate for CDI.next
![Page 22: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/22.jpg)
Seam XML Configurationpublic class MailSender { ... public void setEmail(String email) { ... } ...}
@Qualifier@Retention(RetentionPolicy.RUNTIME)@Target({ ElementType.FIELD, ElementType.METHOD })public @interface Admin {}
@Qualifier@Retention(RetentionPolicy.RUNTIME)@Target({ ElementType.FIELD, ElementType.METHOD })public @interface Support {}
![Page 23: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/23.jpg)
XML Object (Re)-Wiring
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:seam="urn:java:seam:core" xmlns:acme="urn:java:com.acmebank"> ... <acme:MailSender> <seam:ApplicationScoped/> <acme:Admin/> <acme:email>[email protected]</acme:email> </test:MailSender> <acme:MailSender> <seam:ApplicationScoped/> <acme:Support/> <acme:email>[email protected]</acme:email> </test:MailSender> ...</beans>
![Page 24: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/24.jpg)
Injecting XML-Wired Objects
public class CustomerSupportService { ... @Inject @Support private MailSender mail; ... mail.send(); ...}
![Page 25: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/25.jpg)
Refining Beans with XML
@ApplicationScopedpublic class CurrencyConverter { ... private String currency = “us-dollar”; private int precision = 2; ...}
<acme:CurrencyConverter currency=“pound-sterling”> <s:modifies/> <!-- <s:replaces/> --> <acme:precision>4</acme:precision></acme:CurrencyConverter>
![Page 26: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/26.jpg)
Seam Persistence
EJB transactions in CDI managed beans instead of EJB
beans
JPA bootstrap to work with Seam Persistence
Likely standardized in Java EE 7
![Page 27: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/27.jpg)
Persistence and Transactions in Seam
@ApplicationScoped@TransactionAttribute( TransactionAttributeType.REQUIRED)@Auditedpublic class DefaultAccountService implements AccountService { @Inject private EntityManager entityManager;
public void addAccount(Account account) { ... } ...}
![Page 28: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/28.jpg)
Seam Faces
CDI Enhancements for JSF 2
More CDI scopes to match JSF 2: @ViewScoped,
@RenderedScoped, @FlashScoped
Cross-field validation, page pre-load processing
@*Scoped and @Inject in Validators and Converters
Injecting Faces objects
Bridging CDI Events with the JSF life-cycle
Likely standardized in Java EE 7
![Page 29: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/29.jpg)
Cross-Field Validation Form
<h:form id="locationForm"> <h:inputText id="city" value="#{bean.city}"/> <h:inputText id="state" value="#{bean.state}"/> <h:inputText id="zip" value="#{bean.zip}"/> <h:commandButton id="submit" value="Submit“ action="#{bean.submit}"/> <seam:validateForm validatorId="locationValidator"/></h:form>
![Page 30: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/30.jpg)
Cross-Field Validator@FacesValidator @RequestScopedpublic class LocationValidator implements Validator { ... private @Inject Directory directory;
private @Inject @InputField String city; private @Inject @InputField String state; private @Inject @InputField String zip; ... public void validate(FacesContext context, UIComponent component, Object values) throws ValidatorException { if (!directory.exists(city, state, zip)) throw new ValidatorException( new FacesMessage(“Invalid location, try again")); }}
![Page 31: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/31.jpg)
JSF Pre-Load<f:metadata> <f:viewParam name="id" value="#{accountManager.accountId}"/> <s:viewAction action="#{accountManager.loadAccount}"/></f:metadata>
![Page 32: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/32.jpg)
JSF Object Injection@Inject FacesContext facesContext;@Inject ExternalContext externalContext;@Inject NavigationHandler navigationHandler;@Inject Flash flash;
![Page 33: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/33.jpg)
Seam JMS
Injecting connections, sessions, queues, topics,
message producers, message receivers
CDI Event Observers as JMS message listeners
CDI Events sent to JMS destinations
Some features could be standardized in Java EE 7
![Page 34: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/34.jpg)
Seam JMS Resource Injectionpublic class TransferSender { ... @Inject @JmsSession( transacted=true, acknowledgementType=Session.AUTO_ACKNOWLEDGE) private Session session;
@Inject @JmsDestination(jndiName="jms/TransferQueue") private MessageProducer producer; ... public void sendTransfer(Transfer transfer) { ... producer.send(session.createObjectMessage(transfer)); ... } ...}
![Page 35: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/35.jpg)
Event-based exception handling framework
Automatic exception chain handling
Type-based handler callback hierarchy
Catch
![Page 36: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/36.jpg)
Exception Handler@HandlesExceptionspublic class IOExceptionHandler { ... public void handleIOExceptions( @Handles CaughtException<IOException> event) { System.out.println(“I/O Problem: " + event.getException().getMessage()); } ...}
![Page 37: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/37.jpg)
Seam REST
Configure exception handling rules
Integrate with Seam Catch
Integrate Bean Validation with JAX-RS
Templates for JAX-RS output
RESTEasy client integration
Some features could be standardized in Java EE 7
![Page 38: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/38.jpg)
Seam REST Exception Mapping<rest:SeamRestConfiguration> <rest:mappings> <seam:value> <exceptions:Mapping exceptionType="javax.persistence.NoResultException" statusCode="404"> <exceptions:message> Resource does not exist. </exceptions:message> </exceptions:Mapping> </s:value> <s:value> <exceptions:Mapping exceptionType="java.lang.IllegalArgumentException" statusCode="400"> <exceptions:message>Illegal value.</exceptions:message> </exceptions:Mapping> </s:value> </rest:mappings></rest:SeamRestConfiguration>
![Page 39: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/39.jpg)
Seam REST Bean Validation@Stateless@Path("/bid")public class DefaultBidService implements BidService { ... @POST @Consumes("text/xml") @ValidateRequest public void addBid(Bid bid) { ... } ...}
![Page 40: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/40.jpg)
Seam REST Output Templates@GET@Produces({ "application/json", "application/categories+xml", "application/categories-short+xml" })@ResponseTemplate.List({ @ResponseTemplate( value="/freemarker/categories.ftl", produces = "application/categories+xml"), @ResponseTemplate( value="/freemarker/categories-short.ftl", produces = "application/categories-short+xml")})public List<Category> getCategories() {
![Page 41: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/41.jpg)
Seam RESTEasy Client
@Inject@RestClient("http://services.actionbazaar.com/bid")private BidService bidService;...bidService.addBid(bid);
![Page 42: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/42.jpg)
Seam Remoting
Export CDI beans to AJAX via remoting
RPC and domain object models of remoting
Remote context
Client-side bean validation
![Page 43: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/43.jpg)
Seam Remoting on the Server@Named(“bidService”)@Statelesspublic class DefaultBidService implements BidService { ... @WebRemote public void addBid(String bidder, String item, Double amount) { ... } ...}
![Page 44: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/44.jpg)
Seam Remoting on the Client<script type="text/javascript"> function addBid() { ... Seam.createBean(“bidService").addBid(bidder, item, amount); ... }</script>
<button onclick="javascript:addBid()">Bid!</button>
![Page 45: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/45.jpg)
Bridging CDI Events with the Servlet life-cycle
Injecting Servlet objects
Integration with Seam Catch
Some features could be standardized in Java EE 7
Seam Servlet
![Page 46: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/46.jpg)
Servlet Life-Cycle as CDI Eventspublic void onServletContextInitialization( @Observes @Initialized ServletContext context) { ...}
public void onSessionInitialization( @Observes @Initialized HttpSession session) { ...}
public void onRequestInitialization( @Observes @Initialized @Path(“/bid") HttpServletRequest request) { ...}
![Page 47: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/47.jpg)
Servlet Object Injection@Inject ServletConfig configuration;@Inject ServletContext context;@Inject HttpSession session;@Inject HttpServletRequest request;@Inject @RequestParam("id") String bidId;@Inject @HeaderParam("User-Agent") String userAgent;@Inject HttpServletResponse response;@Inject List<Cookie> cookies;
![Page 48: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/48.jpg)
Managing locale/time-zone for the application or session
Internationalized, localized messages
International
![Page 49: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/49.jpg)
Getting and Setting Default Locale@Inject Locale defaultLocale;@Inject DateTimeZone defaultTimeZone;
<locale:DefaultLocaleProducer> <seam:specializes/> <locale:defaultLocaleKey> en_US </locale:defaultLocaleKey></locale:DefaultLocaleProducer><timezone:DefaultTimeZoneProducer> <s:specializes/> <timezone:defaultTimeZoneId> America/New_York </timezone:defaultTimeZoneId></timezone:DefaultTimeZoneProducer>
![Page 50: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/50.jpg)
Getting and Setting User Locale@Inject @UserLocale Locale userlocale;@Inject @UserTimeZone DateTimeZone userTimeZone;
@Inject @Changed Event<Locale> localeEvent;@Inject @Changed Event<DateTimeZone> timeZoneEvent;
public void setUserLocale() { localeEvent.fire(Locale.CANADA);}
public void setUserTimeZone() { timeZoneEvent.fire( DateTimeZone.forID("America/Vancouver"));}
![Page 51: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/51.jpg)
CDI next generation dependency injection for Java EE
Weld reference implementation from JBoss included in
GlassFish and JBoss AS
Seam a large set of CDI portable extensions
Logging, XML, RAD, persistence/tx, JSF, JMS, JAX-RS,
JavaScript remoting, security, i18n & l10n, JavaMail,
scheduling, documents, Servlet, Spring, Wicket, GWT,
Drools, jBPM, JBoss ESB, exceptions, cloud
More on the way!
Summary
![Page 52: An Introduction to Seam 3](https://reader035.vdocuments.us/reader035/viewer/2022062315/568159d1550346895dc720c6/html5/thumbnails/52.jpg)
References
JSR 299: Contexts and Dependency Injection for Java
EE, http://jcp.org/en/jsr/detail?id=299
Weld, the JBoss reference implementation for JSR 299,
http://seamframework.org/Weld
Seam 3, http://seamframework.org