java ee architecture with the spring framework · presentation service persistence database...

24
1 Java EE Architecture with the Spring Framework Peter Thomas Peter Thomas Satyam Computer Services Ltd.

Upload: others

Post on 07-Jul-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

1

Java EE Architecture with the Spring Framework

Peter Thomas

Peter ThomasSatyam Computer Services Ltd.

Page 2: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

2

Overview● Spring: quick intro● JTrac – a real-life Spring web-app● Architecture

− DI / IoC

− Spring DAO / Hibernate support

− Spring AOP / Declarative TX

− Spring MVC / Webflow

− Acegi Security Framework

− Spring Modules

● Spring implications● Selected best practices

Page 3: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

3

Spring – History

Nov 2002 Jul 2004

Feb 2003: SourceForgeProject founded

Aug 2003: 1.0 M1

Mar 2004: 1.0

Page 4: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

4

JTrac – http://jtrac.info● JTrac: highly customizable issue tracking

Page 5: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

5

JTrac Architecture

Presentation

Service

Persistence

Database

Hibernate 3

Spring DAO / Hibernate Support

Spring TX

Spring Security(Acegi)

Spring AOP

Spring Context(DI / IoC container)

POJO POJO POJO

Spring WebFlow

Spring MVC

JSP / JSTL

Page 6: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

6

Dependency Injection

JtracImpl.java

Page 7: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

7

Dependency Injection (2)

Service Implementation

Controllers

Service Interface

DAO Interface

DAO Implementation

Page 8: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

8

Hibernate with Spring● One liners● Template pattern● No need to deal with TX, Session etc.

Page 9: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

9

Spring DAO – Exception Hierarchy

Page 10: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

10

Spring AOP / Declarative TX Mgmt.

Service Implementation

DAO Interface

DAO Implementation

TX Proxy

Controllers

Service Interface

“jtrac”

“jtracTarget”

Page 11: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

11

Spring AOP (contd…)

Service LayerMethod call

SpringTX Proxy (AOP)

MVC Action

DAO method

Page 12: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

12

Spring Web FlowLogical

View Name

StateTransitions

backend logic call (normal Java method)

Flow Decision

Re-usableFlow invocation

userForm

userAllocate

Flow

userListView

cancel

submit userFormHandler

Space ==

null ?

yes

no

Page 13: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

13

Security with Acegi

any URL request withinthe “/admin/”

directory or path can only be allowed for

“ROLE_ADMIN”Any other request has

to be allowed onlyfor authenticated users

Page 14: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

14

Spring Modules example: Lucene

Page 15: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

15

JTrac: Spring implications● Application deployed as a WAR file

● Portable: Tomcat, Jetty, JBoss or any Java EE app server

● Easily unit-testable

● Faster build-deploy-test cycles

● Clean OO design, designed to interfaces

● No EJBs

● No Singletons

● No Service Locator / JNDI lookup

● No custom “Factory Pattern” implementation

● No DTOs / VOs

● No Annotations (optional)

● JSTL / JSP

Page 16: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

16

Why not RoR :)● Spring● Hibernate

− Lazy Loading

● HSQLDB− Embedded database

● Jetty− Small footprint

− Embedded app-server + web server !

● Libraries− JavaSVN, JFreeChart, Lucene, Apache POI

● i18n

Page 17: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

17

Notable in Spring 2.0● Async task execution● Portlet MVC● Use Groovy, JRuby etc. for config● MVC: Custom taglib for form controls (like

Struts)● Message driven POJOs ● JPA support● Simplified XML config option● AspectJ integration

Page 18: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

18

Agile Development with Spring + Hibernate

● Spring− No need of container

− JUnit support classes

− POJOs inherently easy to test

● Hibernate− Completely abstracts DB

− HSQLDB can be used for testing

− Unit tests can assume that DB exists, no mocks reqd.

Delete all previous build artifacts / database files

from file system

Compile all code and unit tests

Connect to the database and forward-generate the schema from the mapping

files using Hibernate.

Boot a fresh instance of the HSQLDB database

server

Run unit-tests.

Shutdown database.

Generate Reports

clean

compile

db-start

db-create

test

db-stop

reports

Page 19: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

19

DEMO● Unit + Integration Testing

Page 20: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

20

Watij [ http://watij.com/ ]● A new class of web test automation tools

− Watir

− Web Application Testing in Ruby

− Selenium

− Watij

− Web Application Testing in Java

● Focus on using a real browser, not simulating one

● Especially important when lots of Javascript / AJAX

Page 21: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

21

Watij Features● Write tests in pure Java● Interactively script tests using BeanShell● Possible to drive tests using JUnit● No setup required on the Server Under Test● Support for nested tables, frames● Good “popup” and multi-window support● And also “brute-force” keystroke support…

Page 22: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

22

Watij in conjunction with JUnit

Page 23: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

23

DEMO● Watij● Driving functional tests with JUnit

Page 24: Java EE Architecture with the Spring Framework · Presentation Service Persistence Database Hibernate 3 Spring DAO / Hibernate Support Spring TX Spring Security (Acegi) Spring AOP

24

Thank you

http://jtrac.info