JavaOne 2011: Migrating Spring Applications to Java EE 6

72

description

The Spring Framework has no-doubt played a major role in evolving the way we write enterprise applications on the Java platform today. However, it is still a proprietary framework owned by a single company. The age of having to rely on such proprietary frameworks in order to develop decent enterprise applications is now over and Java EE 6 has become an even easier way to develop enterprise applications based on standards which makes it the best choice for any enterprise application. In this session you will experience how to migrate a typical full stack Spring application to a standards based, completely portable, Java EE 6 application including integration tests.

Transcript of JavaOne 2011: Migrating Spring Applications to Java EE 6

Page 1: JavaOne 2011: Migrating Spring Applications to Java EE 6
Page 2: JavaOne 2011: Migrating Spring Applications to Java EE 6

Bert ErtmanFellow at Luminis in the NetherlandsJUG Leader for NLJUG and a Java Champion

Paul BakkerSenior developer at Luminis Technologies

@bertertman

@pbakker

Page 3: JavaOne 2011: Migrating Spring Applications to Java EE 6

Who is this talk for?

You are using old school Spring and wonder how to move forwardJava EE seems to be hot again, should you jump on this train?You love the J2EE Design and Development book; but is it still actual?

Page 4: JavaOne 2011: Migrating Spring Applications to Java EE 6

Why listen to us?

..not employed by an application server vendor

..and we’re no Rod Johnson

groupies either

..worked extensively with

J2EE, Spring and modern Java EE

We’ve had endless

Java EE vs.

Spring

discussions...

Page 5: JavaOne 2011: Migrating Spring Applications to Java EE 6

some disclaimers:

Not looking for another holy warNo shootoutNo silver bullets

Page 6: JavaOne 2011: Migrating Spring Applications to Java EE 6

Why migrate?

Upgrading from old school Spring requires a lot of work anyway Why not take it to

the standard?

Spring is proprietary technology

Page 7: JavaOne 2011: Migrating Spring Applications to Java EE 6

This might have been competitive in the past...

Page 8: JavaOne 2011: Migrating Spring Applications to Java EE 6

But now we want THIS!

Page 9: JavaOne 2011: Migrating Spring Applications to Java EE 6

Let’s get

a couple of misunderstandings

out of the way

first...

Page 10: JavaOne 2011: Migrating Spring Applications to Java EE 6

Isn’t Java EE too fat?

Startup times with application deployedJBoss AS 7 ~2 secondsGlassfish V3 ~4 secondsTomcat 6 + Spring ~4 seconds

Java EE 6 WAR file < 100kb

Page 11: JavaOne 2011: Migrating Spring Applications to Java EE 6

Isn’t Java EE too fat?

Startup times with application deployedJBoss AS 7 ~2 secondsGlassfish V3 ~4 secondsTomcat 6 + Spring ~4 seconds

Java EE 6 WAR file < 100kb

Our favorite quote:

“Nowhere in the Java EE spec does it say that Java EE servers should be heavy

weight and slow...”

Page 12: JavaOne 2011: Migrating Spring Applications to Java EE 6

Isn’t Java EE supposed to be evil?

Based on 2004 rhetorics

Ever heard of the fable of the tortoise and

the hare?

Page 13: JavaOne 2011: Migrating Spring Applications to Java EE 6

But I need dependency injection?

Java EE 6 introduced CDI More powerful,

contextual DI modelMakes the platform

extensible in a standard way

Page 14: JavaOne 2011: Migrating Spring Applications to Java EE 6

But I must have AOP!

Really?

You love getting your code all

asymmetric and unreadable?

Or are you just using AOP light

a.k.a. Spring AOP a.k.a. (Java EE)

Interceptors?

Page 15: JavaOne 2011: Migrating Spring Applications to Java EE 6

Can I still do unit testing?

It took a while to get

there, but yes Java EE

supports testing as well

Page 16: JavaOne 2011: Migrating Spring Applications to Java EE 6

do I need heavy tooling?

Page 17: JavaOne 2011: Migrating Spring Applications to Java EE 6

do I need heavy tooling?

NO!

Page 18: JavaOne 2011: Migrating Spring Applications to Java EE 6

Capabilities comparison

Capability Spring JavaEE

Dependency

InjectionSpring Container CDI

Transactions AOP / annotations EJB

Web framework Spring Web MVC JSF

AOP AspectJ (limited to Spring beans) Interceptors

Messaging JMS JMS / CDI

Data Access JDBC templates / other ORM / JPA JPA

RESTful Web

ServicesSpring Web MVC (3.0) JAX-RS

Integration testing Spring Test framework Arquillian *

* Not part of the Java EE specification

Page 19: JavaOne 2011: Migrating Spring Applications to Java EE 6

Apparently,

it can all be done using

plain vanilla light weight

Java EE

Page 20: JavaOne 2011: Migrating Spring Applications to Java EE 6

rm -Rf spring*

Page 21: JavaOne 2011: Migrating Spring Applications to Java EE 6

Sure it would be fun, but not very

realistic

Page 22: JavaOne 2011: Migrating Spring Applications to Java EE 6

We need a recipe that moves us forward

step by step

Page 23: JavaOne 2011: Migrating Spring Applications to Java EE 6

Old school Spring app

lots of complex XML, no annotations

old / outdated ORM solution

JDBC Templates, Kodo, Toplink, etc.

deprecated extension based Web MVC

(SimpleFormController, etc.)

Page 24: JavaOne 2011: Migrating Spring Applications to Java EE 6

Migration path

1.Upgrade Spring version

2.Replace old frameworks

(ORM, web framework)

within Spring 3.Run Spring and Java EE

container side by side

4.Replace Spring entirely

5.Remove Spring container

Page 25: JavaOne 2011: Migrating Spring Applications to Java EE 6

Migration path

1.Upgrade Spring version

2.Replace old frameworks

(ORM, web framework)

within Spring 3.Run Spring and Java EE

container side by side

4.Replace Spring entirely

5.Remove Spring container

Page 26: JavaOne 2011: Migrating Spring Applications to Java EE 6

Upgrade Spring version

Upgrade Spring runtime (replace JAR files)

No code / configuration changes

Page 27: JavaOne 2011: Migrating Spring Applications to Java EE 6

Migration path

1.Upgrade Spring version

2.Replace old frameworks

(ORM, web framework)

within Spring 3.Run Spring and Java EE

container side by side

4.Replace Spring entirely

5.Remove Spring container

Page 28: JavaOne 2011: Migrating Spring Applications to Java EE 6

pres

enta

ton

lay

erD

ata

/ In

teg

rati

onla

yer

Web MVC

Tasks

Spring JDBC TemplatesKodo

@AutoWired

busi

nes

sla

yer

JMS beans

@AutoWired

@AutoWired

Spring beans

replace old frameworkswithin Spring

Page 29: JavaOne 2011: Migrating Spring Applications to Java EE 6

pres

enta

ton

lay

erD

ata

/ In

teg

rati

onla

yer

Web MVC

Tasks

Spring JDBC Templates

@AutoWired

busi

nes

sla

yer

JMS beans

@AutoWired

@AutoWired

JPA

Spring beans

replace old frameworkswithin Spring

Page 30: JavaOne 2011: Migrating Spring Applications to Java EE 6

pres

enta

ton

lay

erD

ata

/ In

teg

rati

onla

yer

Tasks

Spring JDBC Templates

@AutoWired

busi

nes

sla

yer

JMS beans

@AutoWired

@AutoWired

JPA

JSF

Spring beans

replace old frameworkswithin Spring

Page 31: JavaOne 2011: Migrating Spring Applications to Java EE 6

pres

enta

ton

lay

erD

ata

/ In

teg

rati

onla

yer

Tasks

Spring JDBC Templates

@AutoWired

busi

nes

sla

yer

JMS beans

@AutoWired

@AutoWired

JPA

JSFDon’t touch Spring

specific APIs yet

Spring beans

replace old frameworkswithin Spring

Page 32: JavaOne 2011: Migrating Spring Applications to Java EE 6

What about Spring APIs we

don’t want to change yet?

Jdbc TemplatesREST TemplatesTask Schedulingetc.

Page 33: JavaOne 2011: Migrating Spring Applications to Java EE 6

Migration path

1.Upgrade Spring version

2.Replace old frameworks

(ORM, web framework)

within Spring 3.Run Spring and Java EE

container side by side

4.Replace Spring entirely

5.Remove Spring container

Page 34: JavaOne 2011: Migrating Spring Applications to Java EE 6

Using Spring and Java EE

side-by-side

Disclaimer 1: this is just to give an idea, this is not a production ready framework!

Disclaimer 2: this is a migration path. Not

the way you should build your next application...

Page 35: JavaOne 2011: Migrating Spring Applications to Java EE 6

Spring application

myapp.warSpring container

Servlet Container

Spring  beans

Spring  beansT

X m

anag

er

AO

P

OR

M

Page 36: JavaOne 2011: Migrating Spring Applications to Java EE 6

Java EE applicationJava EE 6 application server

CDI / EJB container

myapp.war

CDI  beans

Session  beansTX

man

ager

Secu

rity

Inte

rcep

tors

JPA

Page 37: JavaOne 2011: Migrating Spring Applications to Java EE 6

MixedJava EE 6 application server

CDI / EJB container

myapp.warCDI  beansSession  beans

TX

man

ager

Secu

rity

Inte

rcep

tors

JPA Spring

Spring  beans

Spring  beansT

X m

anag

er

AO

P

OR

M

Page 38: JavaOne 2011: Migrating Spring Applications to Java EE 6

pres

enta

ton

lay

erD

ata

/ In

teg

rati

onla

yer

busi

nes

sla

yer

run Spring withinJava EE container

Spring beans Tasks

Spring JDBC Templates JMS beans

@AutoWired

@AutoWired

JPA

JSF

Add stuff

here...

Page 39: JavaOne 2011: Migrating Spring Applications to Java EE 6

pres

enta

ton

lay

erD

ata

/ In

teg

rati

onla

yer

busi

nes

sla

yer

add Java EE codekeep old Spring code

Spring beans Tasks

Spring JDBC Templates JMS beans

@AutoWired

@AutoWired

JPA

JSF

EJB CDI

@Inject

@Inject

Page 40: JavaOne 2011: Migrating Spring Applications to Java EE 6

Dependency Injection

Spring already supports

JSR-330 Use @Inject anywhere you use

@AutoWiredCDI could be almost a drop-in

replacement for Spring DI

but opens up much more

powerful options

Page 41: JavaOne 2011: Migrating Spring Applications to Java EE 6

Encapsulate Spring with CDI

Write a CDI extension that

bootstraps the Spring

containerlooks up Spring Beans in the

Spring container and

publish in CDI context

Page 42: JavaOne 2011: Migrating Spring Applications to Java EE 6

The Spring DAO

Page 43: JavaOne 2011: Migrating Spring Applications to Java EE 6

JSF / CDI bean

Here we don’t

want to know

about Spring

Page 45: JavaOne 2011: Migrating Spring Applications to Java EE 6

Migration path

1.Upgrade Spring version

2.Replace old frameworks

(ORM, web framework)

within Spring 3.Run Spring and Java EE

container side by side

4.Replace Spring entirely

5.Remove Spring container

Page 46: JavaOne 2011: Migrating Spring Applications to Java EE 6

pres

enta

ton

lay

erD

ata

/ In

teg

rati

onla

yer

busi

nes

sla

yer

entirely replace Spring

Spring beans Tasks

Spring JDBC Templates JMS beans

@AutoWired

@AutoWired

JPA

JSF

EJB CDI

@Inject

@Inject

Page 47: JavaOne 2011: Migrating Spring Applications to Java EE 6

pres

enta

ton

lay

erD

ata

/ In

teg

rati

onla

yer

busi

nes

sla

yer

entirely replace Spring

EJB Timers

MDB

@Inject

@Inject

JPA

JSF

EJB CDI

@Inject

@Inject

Spring JDBC Templates

Page 48: JavaOne 2011: Migrating Spring Applications to Java EE 6

The TX layer

Migrate Spring TX and DAOs to EJB

The TX manager is in the app server

An EJB is transactional by default

EJB has JPA integration

Page 49: JavaOne 2011: Migrating Spring Applications to Java EE 6

You bet!

EJBs are just container managed

POJOs

Come on, are you telling me EJBs are cool now?

Just like Spring beans,

but without the container

configuration

Page 50: JavaOne 2011: Migrating Spring Applications to Java EE 6

The Spring DAO

Page 51: JavaOne 2011: Migrating Spring Applications to Java EE 6

The Java EE alternative

btw - this is an

EJB! :)

Page 52: JavaOne 2011: Migrating Spring Applications to Java EE 6

Dealing with lazy loading

Many Spring apps use the Open-

EntityManager-In-View pattern

EJB has the Extended Persistence

Context

Page 53: JavaOne 2011: Migrating Spring Applications to Java EE 6

LazyInitializationException

Page 54: JavaOne 2011: Migrating Spring Applications to Java EE 6

Fixing lazy loading

Page 55: JavaOne 2011: Migrating Spring Applications to Java EE 6

Detached entities

Spring DAOs are stateless (all singletons)

Load > edit > save needs a merge

With an Extended PU we don’t

Page 56: JavaOne 2011: Migrating Spring Applications to Java EE 6

Migration path

Page 57: JavaOne 2011: Migrating Spring Applications to Java EE 6

Hooked to JDBC Templates!?

Phase 1: denial

Phase 2: migrate ;-)

Template addict?

Hmm, let’s start

the old discussion

whether or not to

use ORM...

Page 58: JavaOne 2011: Migrating Spring Applications to Java EE 6

Using JDBC Templates

1.Can be injected with simple

Producer method

2.It is not relying on Spring

container

3.some extra dependencies though

Page 59: JavaOne 2011: Migrating Spring Applications to Java EE 6

Template producer example

Page 60: JavaOne 2011: Migrating Spring Applications to Java EE 6

Migration path

1.Upgrade Spring version

2.Replace old frameworks

(ORM, web framework)

within Spring 3.Run Spring and Java EE

container side by side

4.Replace Spring entirely

5.Remove Spring container

Page 61: JavaOne 2011: Migrating Spring Applications to Java EE 6

Removing Dependencies

Our classpath just has to

contain APIs, no framework

classesFrom ~40 dependencies to just:

Page 62: JavaOne 2011: Migrating Spring Applications to Java EE 6

What about testing?

Spring is pretty good at this

Java EE was not

Page 63: JavaOne 2011: Migrating Spring Applications to Java EE 6

How to test this?

Page 64: JavaOne 2011: Migrating Spring Applications to Java EE 6

CDI alternatives

Page 65: JavaOne 2011: Migrating Spring Applications to Java EE 6

What about JPA code?

Page 66: JavaOne 2011: Migrating Spring Applications to Java EE 6

Arquillian

Create micro deployments using an API

Run tests in real containers

Page 67: JavaOne 2011: Migrating Spring Applications to Java EE 6

Arquillian example

Page 68: JavaOne 2011: Migrating Spring Applications to Java EE 6

Wrap up

Page 69: JavaOne 2011: Migrating Spring Applications to Java EE 6

Is it all worth it?

blissful ignorance or painful reality ?!

Page 70: JavaOne 2011: Migrating Spring Applications to Java EE 6

Is there life after Spring?

We believe that there is always

room for innovationIdeally through Open Source

When it flies, bring it back to

the spec!

Page 71: JavaOne 2011: Migrating Spring Applications to Java EE 6

Other sessions of interestArquillian: The Extendable Enterprise Test

Platform (24121)

Beginning Java EE 6

(23421)

CDI Today and Tomorrow

(19941)

Enterprise JavaBeans

Technology 3.2 (23180)

Hacking Java EE: CDI

Extension Writing n00b to

l33t (23805)

Introducing Contexts and

Dependency Injection 1.1

(22480)

Java EE 6: The Cool Parts

(21641)

Java EE and Spring/MVC

Shoot-out (24161)

Rethinking Best Practices

with Java EE 6 (21622)