Sunteți pe pagina 1din 34

Java EE 5 – Did You Get

Your Tools With That?


(NetBeans IDE 5.5
Provides “Out-of-the-Box”
Java EE 5 Support!)

Sang Shin
sang.shin@sun.com
www.javapassion.com
Technology Architect
Sun Microsystems
Agenda

• Primary goal Java EE 5


• Web Services 2.0
• EJB 3.0
• New Java Persistence API (JPA)
• JavaServer Faces
• Q/A

Focus of this presentation is NetBeans support of


Java EE 5 Features (not the features themselves)!

NetBeans Day Atlanta 2007


You can try all the demos
yourself by doing the hands-
on lab!
http://www.javapassion.com/hands
onlabs/3400_javaee5basics.zip

Java EE 5 Goal

Make it easier to develop Java EE


applications
Especially when first getting started with Java EE

NetBeans Day Atlanta 2007


How did we make it easier?

Then Now
• Deployment Java language
descriptors annotations
• Required container Plain Old Java
interfaces Objects (POJOs)
• JNDI Lookups Dependency Injection
• Configuration files and More and better
command line options defaults
• No Supported UI Java Server Faces
Framework (JSF)

NetBeans Day Atlanta 2007


Java EE 5 Major Features

• Simplified web services support


• Greatly simplified EJB development
• New Java Persistence API (JPA)
• Dependency Injection
• Easy web application development with JavaServer
Faces

• And fully compatible with J2EE 1.4

NetBeans Day Atlanta 2007


Annotations in Java EE 5

• For defining and using web services


• To greatly simplify EJB development
• To map Java classes to databases (O/R mapping)
• To specify external dependencies (Dependency
injection)
• To map Java classes to XML
• To reduce need for deployment descriptors

NetBeans Day Atlanta 2007


Java EE 5
Web Services
J2EE 1.4 Web Service (Old way)
package endpoint;
<?xml version='1.0' encoding='UTF-8' ?>
<webservices xmlns='http://java.sun.com/xml/ns/j2ee'
version='1.1'>
import java.rmi.*; <webservice-description>
<webservice-description-name>
HelloService</webservice-description-name>
public class HelloServiceImpl <wsdl-file>
WEB-INF/wsdl/HelloService.wsdl</wsdl-file>
implements HelloServiceSEI { <jaxrpc-mapping-file>
WEB-INF/HelloService-mapping.xml
</jaxrpc-mapping-file>
<port-component xmlns:wsdl-port_ns='urn:HelloService/wsdl'>
public String sayHello(String param) <port-component-name>HelloService</port-component-name>
<wsdl-port>wsdl-port_ns:HelloServiceSEIPort</wsdl-port>
throws java.rmi.RemoteException { <service-endpoint-interface>
return “Hello “ + param; endpoint.HelloServiceSEI</service-endpoint-interface>
<service-impl-bean>
} <servlet-link>WSServlet_HelloService</servlet-link>
</service-impl-bean>
} </port-component>
service implementation </webservice-description>
</webservices>

service interface <?xml version='1.0' encoding='UTF-8' ?>


<configuration
package endpoint;
xmlns='http://java.sun.com/xml/ns/jax-rpc/ri/config'>
<service name='HelloService'
targetNamespace='urn:HelloService/wsdl'
import java.rmi.*; typeNamespace='urn:HelloService/types'
packageName='endpoint'>
<interface name='endpoint.HelloServiceSEI'
servantName='endpoint.HelloServiceImpl'>
public interface HelloServiceSEI </interface>
extends java.rmi.Remote { </service>
</configuration>
public String sayHello(String param)

}
throws java.rmi.RemoteException;
Deployment descriptor

NetBeans Day Atlanta 2007


Java EE 5 Web Service (New way)
package endpoint;

import javax.jws.WebService;

@WebService
public class Hello {

public String sayHello(String name) {


return “Hello “ + name;
}
}

NetBeans Day Atlanta 2007


DEMO: Java EE 5 Web Services
1. Make a POJO class Web Service with
“@WebService” Annotation (Code-completion,
Context-sensitive JavaDoc on Annotations)

2. Deploy the Web service and test it right


within the IDE

3. Access WSDL document of the Web Service

4. Create and run Web service client


EJB 3.0
EJB 3.0

• Dramatic simplification of all bean types


• Regular Java classes (POJO)
> @Stateless, @Stateful, @MessageDriven annotations
> Use standard interface inheritance
• Dependency injection
> Instead of using JNDI API to location components and
resources, let the container fetch them for you.
• Entity Beans (CMP) replaced with Java Persistence
API (JPA)
• Interceptors

NetBeans Day Atlanta 2007


Java EE 5 Stateless Session Bean
package endpoint;

import javax.ejb.Stateless;

@Stateless
public class Hello {

public String sayHello(String name) {


return “Hello “ + name;
}
}

NetBeans Day Atlanta 2007


DEMO #1: EJB 3.0 Stateless
Session Bean
1. Build a Stateless Session Bean using
“@Stateless” annotation

2. Build Web application client that accesses


the Stateless Session Bean


DEMO #2: EJB 3.0 Interceptors
1. Build an application that uses Interceptors


Java Persistence API
Java Persistence API (JPA)

• Single persistence API for Java EE & Java SE


• Developed by EJB expert group
> Builds on years of experience with existing technologies
and products (TopLink, Hibernate)
• Much simpler than EJB 2.x CMP
• At least three implementations (all open source):
> Oracle – GlassFish/TopLink Essentials
> JBoss – Hibernate
> BEA – Kodo/OpenJPA

NetBeans Day Atlanta 2007


Persistence Unit

• Used to configure
> Persistence provider
> Supports pluggability of persistence provider
> Data source
> Table Generation Setting
> Create, Drop/Create, Nothing
> Vendor specific properties
• The only XML configuration
• NetBeans will create this for you

NetBeans Day Atlanta 2007


JPA – Persistence Unit

NetBeans Day Atlanta 2007


JPA – Object Relational Mapping

• Developer works with objects


> Database queries return objects
> Object changes persist to the database
• Data transformation is handled by the persistence
provider (TopLink, Hibernate, etc.)
• Annotations define how to map objects to tables
> @Entity marks a regular Java class as an entity.
> Class attributes map to table columns. Can be
customized with @Column.
> Manage relationships: @OneToMany, ...

NetBeans Day Atlanta 2007


JPA – Entity Class
import javax.persistence.*;

@Entity
public class Person {
@Id private String name;
private int visits;

public Person() { }

public Person(String name) {


this.name = name;
}

public int incrementVisit() {


return visits++;
}
}

NetBeans Day Atlanta 2007


JPA – Entity Manager

• EntityManager stores/retrieves data


• Inject EntityManager:
> @PersistenceContext private EntityManager em;
• Create an instance of the entity:
> Person p = new Person(param);
• Use EntityManager methods to persist data
> em.persist(p); em.merge(p); em.delete(p);
• Query using EJB QL or SQL
> Person p = em.find(Person.class, param);

NetBeans Day Atlanta 2007


JPA – Entity Manager
...
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@WebService
@Stateless
public class Hello {
@PersistenceContext private EntityManager em;
public String sayHello(String param) {
Person p = em.find(Person.class, param);
if (p == null) {
p = new Person(param);
em.persist(p);
}
p.incrementVisist();
return “Hello “ + param + “: “ +
} }

NetBeans Day Atlanta 2007


DEMO 1: Java Persistence API

1. Create Persistence Unit

2. Create Entity classes from Database tables

3. Use “TopLink” or “Hibernate” as Persistence


Provider
DEMO 2: Java Persistence API
1. Use different strategy for Inheritance and
see how database tables are created

- SINGLE_TABLE
- JOINED
JavaServer Faces 1.2
JavaServer Faces 1.2

• The Java EE Standard Web Application Framework


> Managed beans support dependency injection
> Easy to use, powerful, extensible Expression Language,
shared with JSP
• Large market of JSF components
> Over 200 components from over 20 vendors, such as...
> Apache, BusinessObjects, ESRI, Oracle, Sun, etc.
> Including AJAX support
• Java BluePrints AJAX Components
> http://blueprints.dev.java.net

NetBeans Day Atlanta 2007


JavaServer Faces 1.2 in NetBeans

• Code Completion for:


> JavaServer Pages (JSP)
> JavaServer Faces (JSF), including expression language
> Java Standard Tag Libraries (JSTL)
> Any tab libraries added by the user
• Integrated documentation
• HTML, JSP and JSF component palette

NetBeans Day Atlanta 2007


JavaServer Faces 1.2 in NetBeans

• Generation of JavaServer Faces applications from


entity classes:
> Pages – list, detail, edit, create
> Managed bean
> Navigation rules
• Code is easy to read/change
• Helps to learn the patterns
• Gets you started quickly
• Code generation similar to Ruby on Rails...

NetBeans Day Atlanta 2007


DEMO: JavaServer Faces
1. Create JSF pages from Entity Classes -
Easily create CRUD (Create, Read, Update
and Delete) database application

2. Create Managed Beans from Entity Classes


Summary

• The developer works less


> No synchronization between Java programming
language and XML
• The container works more
> Annotation processing, dependency injection
• NetBeans 5.5 knows Java EE 5
> Less work to hide the complexity and XML descriptors
> Does more useful things: wizards, hints, code validation,
code generation, etc.
• Java EE 5 - Did You Get Your Tools With That?

NetBeans Day Atlanta 2007


Questions & Answers

NetBeans Day Atlanta 2007


Java EE 5 – Did You Get
Your Tools With That

Sang Shin
sang.shin@sun.com
www.javapassion.com
Technology Architect
Sun Microsystems

S-ar putea să vă placă și