Agile Java Dev With Spring Hibernate Eclipse
Agile Java Dev With Spring Hibernate Eclipse
Agile Java Dev With Spring Hibernate Eclipse
4
My Background (details at VisualPatterns.com)
20 years of experience in the IT
Working with Java Technology since late 1995 as a developer,
entrepreneur, author, and trainer.
Helped several U.S. based Fortune 100 companies (some smaller
organizations)
Published a book and 30 articles
Presented at conferences and seminars around the world
Awards:
"Outstanding Contribution to the Growth of the Java Community"
"Best Java Client" for BackOnline (a Java-based online backup
product)
Nominated for a Computerworld-Smithsonian award by Scott McNealy
Founder of:
Isavix Corporation – successful IT solutions company (now
InScope Solutions)
Isavix Community (now DeveloperHub.com) - award-winning online
developer community (grew to over 100,000 registered members)
Application
Financial application process billions of $ every week
6
Agenda
1. Introduction to Agile Java Development
2. Agile Processes
3. Agile Modeling
4. Agile Development
Environment Setup: Directory Structure, JDK, Ant and
JUnit
Using Hibernate For Persistent Objects
The Spring Framework
The Eclipse Phenomenon!
Logging, Debugging, Monitoring, and Profiling
5. Beyond The Basics
7
Introduction to Agile Java Development
Assume simplicity.
Travel light.
9
Agile Processes
Requirements change.
Design evolves.
13
Term “Agile” Incorporates a Wide Range of Methods
AM - Agile Modeling
ASD - Adaptive Software Development
AUP - Agile Unified Process
Crystal
FDD - Feature Driven Development
DSDM - Dynamic Systems Development Method
Lean Software Development
Scrum
Xbreed
XP - eXtreme Programming
Others…
14
“Agility” - All About Smaller Chunks (Shorter/Frequent
Cycles)
Release 1 Release 2
Iteration
0
Iteration
1
...Iteration
n
Iteration
0
Iteration
1
...Iteration
n
...
16
Agile Method: Extreme Programming (XP)
Shorter and Frequent Cycles (smaller chunks!)
Release - Quarterly Cycles (set a theme)
Iteration - Weekly Cycles (e.g. aim for last day of
week)
10-minute builds
Continuous integration (multiple times per day; manual
or automatic)
Incremental Design and Planning (defer investment till
needed)
Development in small increments using Test-First
development
18
Agile Modeling
- Scott W. Ambler
Quick Poll
20
Agile Modeling Values, Practices & Principles
(agilemodeling.com)
Values
Communic
ation, simplicity, feedback, courage and humility.
Practices Principles
Requirements modeling
Usage models
Domain models
UI models
Let’s apply
this to a
Architecture modeling
sample
Free-form diagrams
Change cases
application
, next ...
23
Initiating A New Software Application Project
24
Problem Statement
26
Choices Of Release (High) Level Models
Release
scope table, UI prototype
Level domain model user stories architecture
Models glossary, etc. & flow map
Iteration
acceptance application UML database
Level CRC cards
Models tests flow map diagrams model
27
Sample Scope Table
Scope Functionality
Include Time Expression will provide the capability to enter, approve, and
pay for hours worked by employees.
Defer Time Expression will not calculate deductions from paychecks, such
as federal/state taxes and medical expenses.
Defer Time Expression will not track vacation or sick leave.
28
Domain Model
Description
This process allows User to log into the System
Actors/I nterfaces
XP Style User Story • FM Trader
Card
• The System
Trigger
Userperforms aLogin action
Preconditions
• N/A
Web HTTP
Controller Model JDBC
Spring Business objects,
RDBMS
Browser DispatcherServlet Hibernate beans (Oracle)
Spring
View Schedul
JSP/HTML er
Release
scope table,
UI prototype
Level domain model user stories architecture
Models glossary, etc. & flow map
Iteration
acceptance application UML database
Level CRC cards
Models tests flow map diagrams model
35
Iteration Level Details - Acceptance Tests & Active
Stakeholders
Sign In
The employee id can be up to 6 characters. The
password must be between 8 and 10 characters.
Only valid users can sign in.
Timesheet List
Only a user's personal timesheets can be
accessed.
Enter Hours
Hours must contain numeric data.
Daily hours cannot exceed 16 hours. Weekly
hours cannot exceed 96 hours.
Hours must be billed to a department.
Hours can be entered as two decimal places.
Employees can only view and edit their own
timesheets.
36
Exploring Classes Using CRC Cards
First, let's reflect on Second, let's explore classes
what we know, domain model, on CRC cards using both as
UI and architecture Cinput
l a s s N a m emodels
(N o un)
Timesheet List
screen
T im e s h e e t L is tC o n tr o lle r
C o n t r o lle r ( in M V C ) fo r T im e s h e e t M a n a g e r
d is p la y in g a lis t o f t im e s h e e t s .
T im e s h e e t M a n a g e r
F e t c h e s t im e s h e e t ( s ) fr o m T im e s h e e t
free-form
architecture d a ta b a s e
S a v e s t im e s h e e t t o d a t a b a s e
T im e s h e e t
K n o w s o f p e r io d e n d in g d a t e
domain model K n o w s o f t im e
K n o w s o f d e p a rtm e n t c o d e
37
Application Flow Map (Home Grown Artifact)
38
UML Class and Package Diagrams
39
Focus Is On Working Software vs. Comprehensive
Documentation
Conceptual Models
problem
statement
scope
domain model table
user stories
UI glossary
prototypes Model in Small Increments
architecture
tests model
CRC cards
Prove it with code
application UML
flow map diagrams
- agilemodeling.com
Implementation THE FINAL AND LASTING ARTIFACTS!
40
Shifting Some Upfront Design to Refactoring
41
Shifting Some Upfront Design To Refactoring (Continuous
Design)
Refactoring is not a new concept; the term is
relatively new
refactoring.com
“Refactoring is a disciplined technique for
restructuring an existing body of code, altering its
internal structure without changing its external
behavior.”
- Martin Fowler
42
Agile Draw - Elegantly Simple Modeling Technique
High-Level
Architecture
UI Flow Map
Visit AgileDraw.org
Conceptual Class
Diagram
43
Presentation Outline
Introduction to Agile Java Development
Agile Processes
Agile Modeling
Agile Development
Environment Setup: Directory Structure, JDK, Ant and
JUnit
Using Hibernate For Persistent Objects
The Spring Framework
The Eclipse Phenomenon!
Logging, Debugging, Monitoring, and Profiling
Beyond The Basics
44
Agile Java Development:
Environment Setup (Directory Structure, JDK, Ant, and
JUnit)
Quick Poll
46
Personal Opinion:
Early Environment
Setup Is Essential
➔controller/TimesheetListController.java
➔model/Timesheet.java
➔model/TimesheetManager.java
➔test/TimesheetListControllerTest.java
➔test/TimesheetManagerTest.java
➔view/timesheetlist.jsp
48
Ant (ant.apache.org)
Originally written by
Erich Gamma (Gang of Four, Design Patterns)
Kent Beck (author of Extreme Programming and Test Driven
Development)
50
JUnit GUI Based Testing
Console Runner
Eclipse Plug-in
51
Agile Method: Test Driven Development (TDD) w/ JUnit
A term coined by Kent Beck
Also, a XP practice (test-first)
“Red - Green - Refactor”
52
Presentation Outline
Introduction to Agile Java Development
Agile Processes
Agile Modeling
Agile Development
Environment Setup: Directory Structure, JDK, Ant and
JUnit
Using Hibernate For Persistent Objects
The Spring Framework
The Eclipse Phenomenon!
Logging, Debugging, Monitoring, and Profiling
Beyond The Basics
53
Agile Java Development:
Using Hibernate For Persistent Objects
Quick Poll
What persistence
solution does your
project use (e.g.
JDBC, ORM, entity
bean)?
55
Where Hibernate Fits Into Our Architecture
56
An Overview of Object-Relational Mapping (ORM)
ORM - Java object to database table/record mapping
Java = objects
database = relational
Relationships
unidirectional and bidirectional
relations in a relational database are bidirectional by
definition
Cardinality (OO term is multiciplicity)
One-to-one
one-to-many
many-to-one
and many-to-many
Object Identity
Cascade
Others… 57
Hibernate Basics
Dialect
(DB2, Microsoft SQL Server, MySQL, Oracle,
PostgreSQL, SAP DB, Sybase, TimesTen…)
59
Working With Hibernate - Simple Example Using
Department
• hibernate.cfg.xml – Hibernate configuration file
(DB configuration)
<property name="connection.url">
jdbc:hsqldb:hsql://localhost:9005/timex</property>
<mapping resource="Department.hbm.xml" />
Transaction tx = session.beginTransaction();
Department department = (Department)
session.get(Department.class, "IT");
System.out.println("Name for IT = " + department.getName());
...
...
sessionFactory.close();
61
Other Hibernate Features
Saving (save, merge, saveOrUpdate)
session.saveOrUpdate(timesheet)
Deleting records
session.delete(Object), or
session.createQuery("DELETE from Timesheet")
63
Agile Java Development:
The Spring Framework
Spring Modules
65
Spring Java Packaging (org.springframework.)
66
Quick Poll
Many others
No Singletons
Builds on top of existing technologies (e.g. JEE,
Hibernate)
Robust MVC web framework
Consistent database exception hierarchy (e.g. wrap 69
Where Spring Framework Fits Into Our Architecture
Optional
Hibernate
integration
70
Quick Poll
71
Spring Web MVC
Easier testing – mock classes, dependency injection
72
Spring MVC Java Concepts
1.Controller
2.ModelAndView
3.Command (Form
Backing)
Object
4.Validator
5.Spring Tag
Library
(spring:bind)
73
Spring MVC Configuration
<servlet>
<servlet-name>timex</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>timex</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
web.xml timex-
<bean id="urlMapAuthenticate” servlet.xml
class=
"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<prop key="/enterhours.htm">enterHoursController</prop>
...
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean> 74
Sample End-To-End Flow Using Spring and Hibernate
75
Timesheet List: A No-Form Controller Example
mockHttpServletRequest =
new MockHttpServletRequest("GET",
"/timesheetlist.htm");
ModelAndView modelAndView =
timesheetListController.handleRequest(
mockHttpServletRequest, null);
assertNotNull(modelAndView);
assertNotNull(modelAndView.getModel());
76
Enter Hours: A Form Screen
EnterHoursController.jav
a
EnterHoursValidator.java
enterhours.jsp
77
View/JSP Code – Spring and JSTL Tag Libraries
<spring:bind path="command.employeeId">
<input name='<c:out value="${status.expression}"/>'
value='<c:out value="${status.value}"/>'
type="text" size="6" maxlength="6">
</spring:bind>
78
Sign In (Authentication) - Spring HandlerInterceptor
79
Other Spring Web Stuff
View with no controllers (e.g. only JSP files)
<bean id="urlFilenameController"
class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
<prop key="/help.htm">urlFilenameController</prop>
81
Spring ORM Module: Support for Hibernate (cont’d)
Session session =
HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
try
{
session.saveOrUpdate(timesheet);
session.getTransaction().commit();
}
catch (HibernateException e)
{
session.getTransaction().rollback();
throw e;
}
getHibernateTemplate().saveOrUpdate(timesheet);
Less lines of
code 82
More Spring…
Scheduling Jobs (with Quartz or JDK
timers)
<bean id="reminderEmailJobDetail" class=
"org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="reminderEmail" />
<property name="targetMethod" value="sendMail" />
</bean>
<bean id="reminderEmailJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="reminderEmailJobDetail" />
<property name="cronExpression" value="0 0 14 ? * 6" />
</bean>
Much more
JEE support
Sub-projects (Acegi, BeanDoc, Spring
IDE, etc.)
83
Presentation Outline
Introduction to Agile Java Development
Agile Processes
Agile Modeling
Agile Development
Environment Setup: Directory Structure, JDK, Ant and
JUnit
Using Hibernate For Persistent Objects
The Spring Framework
The Eclipse Phenomenon!
Logging, Debugging, Monitoring, and Profiling
Beyond The Basics
84
Agile Java Development:
The Eclipse Phenomenon!
Quick Poll
86
The Eclipse Foundation, Platform and Projects
Foundation
Originally developed by Object Technology International
(OTI), purchased by IBM ($40 million) and donated it to
open source!
Recruited various corporations; from eclipse.org:
Industry leaders Borland, IBM, MERANT, QNX Software Systems,
Rational Software, Red Hat, SuSE, TogetherSoft and Webgain
formed the initial eclipse.org Board of Stewards in November
2001. By the end of 2003, this initial consortium had grown to
over 80 members.
My view: Eclipse foundation is similar to Apache foundation for
GUI tools
Platform objectives
robust platform for highly integrated dev tools
enable view and/or editing of any content type
attract a large community of developers to develop plug-
ins
87
Personal Opinion:
The Java versus
Microsoft Thing
88
How Eclipse Can Help With Our Application
89
Eclipse Basic Concepts
• Workspace (directory of
projects)
• Workbench
• Perspectives
• Editors and Views
• Project
• Wizards (hundreds)
• Plug-ins (galore!)
sample
workspace
90
Eclipse Plug-in: Java Development Tools (JDT)Java Browsing
JUnit
Ant Assist
Java Compile 91
Errors/Warnings
JDT: Other Notable Features
Compile during save (within the blink of an eye)
Formatting options
Scrapbook
TODO lists
Others
Powerful search
Code refactoring (some based on Fowler's
refactoring.com)
Export feature (create zip files, etc.)
92
Eclipse Plug-In: Web Tools Platform (WTP; eclipse.org)
Tools for developing JEE Web applications
Editors
Source - HTML, JavaScript, CSS, JSP, SQL, XML, DTD, XSD,
and WSDL
Graphical - XSD and WSDL
Much more…
93
WTP: Notable Features
Servers
JSP Assist
Database
Web Services
94
CVS (Eclipse Team Sharing)
95
Hibernate and Spring Plug-Ins
Hibernate
Spring
IDE
96
Startup Time Comparison To IntelliJ and NetBeans
IntellIJ - 1 minute, 5
seconds!
NetBeans - 42
seconds.
Eclipse with JDT, WTP, Hibernate, Eclipse... 19
seconds!
97
Presentation Outline
Introduction to Agile Java Development
Agile Processes
Agile Modeling
Agile Development
Environment Setup: Directory Structure, JDK, Ant and
JUnit
Using Hibernate For Persistent Objects
The Spring Framework
The Eclipse Phenomenon!
Logging, Debugging, Monitoring, and Profiling
Beyond The Basics
98
Agile Java Development:
Logging, Debugging, Monitoring, and Profiling
Quick Poll
100
Logging Basics and Frameworks
Types Logging Frameworks
1.Audit logAlternative to println statements
2.Tracing Key benefit - Output control (destination, format, log le
3.Error Most popular - Apache Log4J and JDK Logging
reportingJakarta Commons Logging -- bridge to frameworks
hit
Can clutter 101
Headaches of Finding and Fixing Bugs!
102
Debugging Java Code With Eclipse
Debug
perspectives “consolidated debugging”
and views
Breakpoints
Step through
code
Variable
inspection
Hotswap
Remote
debugging
103
Debugging Web User Interfaces Using Mozilla Firefox
JavaScript
debugger
Web Developer
collection
Management of
MBeans and JDK
logging level,
etc …
Profiling
Memory usage and
leaks
CPU utilization
Trace objects and
methods
Determine 105
Presentation Outline
Introduction to Agile Java Development
Agile Processes
Agile Modeling
Agile Development
Environment Setup: Directory Structure, JDK, Ant and
JUnit
Using Hibernate For Persistent Objects
The Spring Framework
The Eclipse Phenomenon!
Logging, Debugging, Monitoring, and Profiling
Beyond The Basics
106
Beyond The Basics
Security, Reliability and Scalability Considerations
108
Application Security Considerations
Authentication (user and application levels)
Authorization (roles, groups, etc.)
Encryption (wire protocol, configuration files)
User-level
authentication
& authorization
Wire protocol
(HTTP/S) Application-level
authentication
109
Other Considerations
Exception Handling
1.Checked exceptions (e.g. IOException) – required catch
or throw
2.Unchecked exceptions (e.g. NullPointerException) - no
catch/throw needed
3.Errors (e.g. OutOfMemoryError)
111
Wrap Up!
Presentation Outline
“A
generalizing
specialist
is someone
with a good
grasp of how
everything
fits
together.”
114
Most Important… Don’t Forget To Have Fun! :-)
code.google.com/webtoolkit/
getahead.ltd.uk/dwr/
116
Thank you!