<Insert Picture Here> Exploring Java EE 6 The Programming Model Explained Lee Chuk Munn chuk-munn.lee@oracle.com
The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle s products remains at the sole discretion of Oracle.
What's New in the Java EE 6 Platform? New and updated components EJB 3.1 (+Lite) JPA 2.0 Servlet 3.0 JSF 2.0 JAX-RS 1.1 Bean Validation 1.0 DI 1.0 CDI 1.0 Connectors 1.6 Managed Beans 1.0 Interceptors 1.1 JSP 2.2 EL 2.2 JSR 250 1.1 JASPIC 1.1 JACC 1.5 JAX-WS 2.2 JSR 109 1.3
Changes in Java EE 6 23% 52% 11% 14% Unchanged Completely new Major updates Regular updates http://blog.eisele.net/2010/12/who-is-afraid-of-java-ee-6-get-rid-of.html
Java EE 6 Web Profile CDI Extensio ns ns Web Cont ainer Extensions JSP JAX-RS 1.1 JSF 2.0 2.2 2.2 JSTL 1.2 Servlet 3.0 / EL 2.2 DI 1.0 // CDI 1.0 // Interceptors 1.1 // JSR 250 1.1 Managed Beans 1.0 EJB 3.1 JPA 2.0 // JTA 1.1 Bean Validation 1.0
Simplfied Packaging Java EE 5 Java EE 6 foo.ear foo_web.war WEB-INF/web.xml WEB-INF/classes com.sun.fooservlet com.sun.ticktock foo_ejb.jar com.sun.foobean com.sun.foohelper foo.war WEB-INF/classes com.sun.fooservlet com.sun.ticktock com.sun.foobean com.sun.foohelper
Web Apps Simplified Packaging Web Application WEB-INF/classes com/acme/myservlet.class com/acme/myfilter.class WEB-INF/lib someframework.jar WEB-INF/web.xml index.html main.css jquery-1.4.2.js ui/jquery.ui.core.js ui/jquery.ui.widget.js
Web Apps Simplified Packaging Web Application WEB-INF/classes com/acme/myservlet.class com/acme/myfilter.class WEB-INF/lib someframework.jar @WebServlet(urlPatterns= /foo ) public class MyServlet { } @WebFilter(urlPatterns= /foo ) public class MyFilter { } WEB-INF/web.xml index.html main.css jquery-1.4.2.js ui/jquery.ui.core.js ui/jquery.ui.widget.js
Web Apps Simplified Packaging Web Application WEB-INF/classes com/acme/myservlet.class com/acme/myfilter.class WEB-INF/lib someframework.jar Framework Jar META-INF/web-fragment.xml com/fw/fwservlet.class com/fw/fwfilter.class index.html main.css jquery-1.4.2.js ui/jquery.ui.core.js ui/jquery.ui.widget.js
Web Apps Simplified Packaging Web Application WEB-INF/classes com/acme/myservlet.class com/acme/myfilter.class WEB-INF/lib someframework.jar jquery-ui-1.8.4.jar jquery Resource Jar META-INF/resources/jquery-1.4.2.js META-INF/resources/ui/jquery.ui.core.js META-INF/resources/ui/jquery.ui.widget.js index.html main.css
Web Apps Simplified Packaging Web Application WEB-INF/classes com/acme/myservlet.class com/acme/myfilter.class WEB-INF/lib someframework.jar jquery-ui-1.8.4.jar Simpler packaging Drag and drop self configuring frameworks Resource JARs index.html main.css
Managed Bean as the Foundation Plain Java objects (POJOs) Lifecycle managed by container Foundation for other component types Not something that developers would use directly Supports injections @Resource, @Inject Lifecycle callbacks @PostConstruct, @PreDestroy Optional name Interceptors
Managed Bean Example @ManagedBean( MyCustomer ) public class Customer { @Resource DataSource db; //Inject another managed bean @Resource Address address; } @PostConsturct private void init() { // }
EJB Components as Managed Beans All managed beans core services are available in EJB EJB component model extends managed beans by providing State @Stateful, @Stateless, @Singleton Transaction @TransactionAttribute Security @RolesAllowed, @DenyAll, @PermitAll Access semantics @Remote, @WebService EJBs can now be view as features on managed beans Just as other types of components
Incremental Programming Model Application changes over time Requirement change, functions of classes/objects changes Allows you to evolve your application gracefully Start with POJOs @ManagedBean Use all the basic services lifecycle, resource injection, etc Turn them in to EJBs, Enitites, etc to take advantage of new capabilities No change to (local) client
Incremental Programming Model @ManagedBean public class Customer { @Resource DataSource db; //Inject another managed bean @Resource Address address; } @PostConsturct private void init() { // }
Incremental Programming Model @Stateful public class Customer { @Resource DataSource db; //Inject another managed bean @Resource Address address; } @PostConsturct private void init() { // }
Common Injection Service Framework Injection services available to all objects Not restricted to specific components eg. EJB Context and Dependency Injection Managed beans on steroids Opt-in technology on per-module basis WEB-INF/beans.xml, META-INF/beans.xml Key CDI concepts Strongly typed for typesafety Loose coupling events, interceptors, decorators Context and scope management Integration with EL Bridge EJB (business) to JSF (presentation)
CDI Example @Inject User user;
CDI Example @Inject User user; Type describes the capability of the user
CDI Example What type of User? @Inject User user;
CDI Example @Inject @LoggedIn User user; Qualifier describes the characteristic of the user
Uniformity Annotations are additive New behaviour refine pre-existing ones Unless annotation conflicts with the semantics of component Eg. session scoped for JAX-RS components Eg. JAX-RS resource classes are POJOs Use JAX-RS specific injection capabilities Additional annotations to turn resource classes into CDI managed beans or EJBs Use the new services with REST
JAX-RS Example @Path( root ) public class RootResource { @Context UriInfo info; @Path( {id} ) public SubResource find( @PathParam( id ) String id) { } } return (new SubResource(id));
JAX-RS Example As a managed bean, using @Resource @ManagedBean @Path( root ) public class RootResource { @Context UriInfo info; @Resource DataSource mydb; @Path( {id} ) public SubResource find( @PathParam( id ) String id) { } } return (new SubResource(id));
JAX-RS Example As a CDI bean, using @Inject and scoped annotations @RequestScoped @Path( root ) public class RootResource { @Context UriInfo info; @Inject @CustomerDB DataSource mydb; @Path( {id} ) public SubResource find( @PathParam( id ) String id) { } } return (new SubResource(id));
APIs That Work Better Together Previous versions have a bunch of APIs that are quite independent of each other Java EE 5/6 introduces lots of 'services' API Managed bean, CDI, validation, interceptors, etc Aiming to get API to work better together Eg. Bean Validation integrated into JSF, JPA But not so well with CDI working on it Use of SPI to provide integration to other frameworks
Java EE 6
After Frameworks
Java EE 6 Platform Extensions
Extensibility Integrating 3 rd party frameworks Pluggability contracts make extension look like build-in technologies Two main extension points in Java EE 6 Servlet 3.0 pluggability CDI extensions
Servlet 3.0 Pluggability Primary to address 3 rd party web frames Drag and drop model Dynamic or static registration for frameworks Configuration, initialization, code generation, etc Results in ready to use framework JARs Modular web.xml and web-fragment.xml Framework contains a fragment of web.xml META-INF/web-fragment.xml Extension can discover and process annotated classes
Pluggability Example @MyWebFramework public class DoSomething { Register with service loader framework myframework.jar META-INF/services/ javax.servlet.servletcontainerinitializer myframework.frameworkloader
Pluggability Example @HandlesTypes(MyWebFramework.class) public class FrameworkLoader implements ServletContainerInitializer { public void onstartup(set<class<?>> c, ServletContext ctx) { //Load framework controller ServletRegistration.Dynamic reg = ctx.addservlet( MyFrameworkController, myframework.controller.frameworkcontroller ); reg.setinitparameter( config, /WEB-INF/config.properties ); } } //Other processing eg. Code generation...
Extending CDI Portable extension framework provides standard way of extending CDI Drag and drop Extensions can process any annotated types What can you do with portable extensions Introduce new bean types Satisfy injection points Introduce custom scopes Augment or substitute bean's annotations and/or metadata Limited by your imagination
Portable Extension Example @RequestScope @Message public class HandleMessage { New component type @Inject @From JID from; @PersistenceContext EntityManager em; New bean type Integrates well } @Body( {body} ) public Object handle(@named( body ) int custid) { return (em.find(customer.class, custid)); } Export a named bean to handle method only
Conclusion A Better Platform Less boilerplate code Simpler packaging Uniform use of annotations Consistent programming model Extensible Simple things should be simple complex things should be possible Alan Kay
For More Information search.oracle.com or oracle.com