Introduction To Web Beans
Introduction To Web Beans
JSR-299
Pete Muir
JBoss, a Division of Red Hat
Road Map
Background
Concepts
Demo
Status
Java EE 6
• The EE 6 web profile removes most of the “cruft” that has
developed over the years
– mainly the totally useless stuff like web services, EJB 2 entity beans, etc
– some useful stuff like JMS is also missing, but vendors can include it if
they like
• EJB 3.1 - a whole bunch of cool new functionality!
• JPA 2.0 - typesafe criteria query API, many more O/R
mapping options
• JSF 2.0 - don’t need to say much more!
• Bean Validation 1.0 - annotation-based validation API
• Servlet 3.0 - async support, better support for
frameworks
• Standard global JNDI names
Goals
• JSR-299 defines a unifying dependency
injection and contextual lifecycle model for
Java EE 6
– a completely new, richer dependency
management model
– designed for use with stateful objects
– integrates the “web” and “transactional” tiers
– makes it much easier to build applications
using JSF and EJB together
– includes a complete SPI allowing third-party
frameworks to integrate cleanly in the EE 6
environment
What can be injected?
• Pre-defined by the specification:
– (Almost) any Java class
– EJB session beans
– Objects returned by producer methods
– Java EE resources (Datasources, JMS topics/
queues, etc)
– Persistence contexts (JPA EntityManager)
– Web service references
– Remote EJBs references
• Plus anything else you can think of!
Loose coupling
• Events, interceptors and decorators
enhance the loose-coupling that is inherent
in this model:
– event notifications decouple event producers
from event consumers
– interceptors decouple technical concerns from
business logic
– decorators allow business concerns to be
compartmentalized
Going beyond the spec
• Web Beans will provide extra integrations
– Tomcat/Jetty support
– Wicket support
– ???
• and features which can be used in any
JSR-299 environment
– jBPM integration
– log injection (choose between log4j and jlr,
parameter interpolation
– Seam2 bridge
– Spring bridge
Seam 3?
• Use the JSR-299 core
• Provide a development environment
– JBoss Tools
– Seam-gen (command line tool)
• include a set of modules for any container
which includes JSR-299
– Seam Security
– Reporting (Excel/PDF)
– Mail
Road Map
Background
Concepts
Demo
Status
Essential ingrediants
• API types
• Binding annotations
• Scope
• Deployment type
• A name (optional)
• Interceptor bindings
• The implementation
Simple Example
@Initializer
public Printer(Hello hello) { this.hello=hello; }
public
Creating a binding type is
@BindingType
really easy!
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
@interface Casual {}
Using a binding type
We also specify the @Casual binding
@Casual type. If no binding type is specified on
a bean, @Current is assumed
public class Hi extends Hello {
public String hello(String name) {
return "hi" + name;
}
}
Using a binding type
Here we inject the Hello
bean, and require an
implementation which is
public class Printer { bound to @Casual
@Casual Hello hello;
public void hello() {
System.out.println( hello.hello("JBoss") );
}
}
Deployment Types
• A deployment type is an annotation that
identifies a deployment scenario
– Deployment types may be enabled or disabled,
allowing whole sets of beans to be easily
enabled or disabled at deployment time
– Deployment types have a precedence, allowing
different implementations of an API to be
chosen
– Deployment types replace verbose XML
configuration documents
• Default deployment type: Production
Create a deployment type
public
@DeploymentType
@Retention(RUNTIME)
@Target({TYPE, METHOD})
@interface Espanol {}
Using a deployment type
}
Enabling deployment types
<Beans>
<Deploy>
<Standard />
<Production>
<i8ln:Espanol>
</Deploy> A strongly ordered list of enabled
</Beans> deployment types. Notice how everything
is an annotation and so typesafe!
@SessionScoped
public class Login {
private User user;
public void login() {
user = ...;
}
@Produces
User getUser() { return user; }
}
Producer methods
Producer method can a
scope (otherwise inherited
@SessionScoped
from the declaring
public class Login { component)
private User user;
@Produces
String getWelcomeMessage(@Current Hello hello) {
return hello.hello(user);
} You can inject parameters
}
Producer methods
@Produces @UserDatabase
EntityManager create(EntityManagerFactory emf) {
return emf.createEntityManager();
} Same binding types
Same API type
@Produces
User getUser() { return new DummyUser(); }
}
Road Map
Background
Concepts
Demo
Status
Road Map
Background
Concepts
Demo
Status
JSR-299
• Public Review Draft 2 published
• Currently working on EE6 integration
• Web Beans “Book” (a less formal guide to
JSR299)
– http://www.seamframework.org/WebBeans
• Send feedback to jsr-299-
[email protected]
Web Beans
• The Reference implementation
– Feature complete preview released in next few
days
• Download it, try it out, give feedback!
– http://seamframework.org/Download
• Supported in upcoming release:
– JBoss 5.1.CR1
– GlassFish V3 build 46
– Tomcat 6.0.x
– Jetty 6.1.x
Q&A
http://in.relation.to/Bloggers/Pete
http://www.seamframework.org/WebBeans
http://jcp.org/en/jsr/detail?id=299