Table Of ContentBeginning Java™ and Flex
Migrating Java, Spring, Hibernate, and Maven
Developers to Adobe Flex
■ ■ ■
Filippo di Pisa
Beginning Java™ and Flex: Migrating Java, Spring, Hibernate, and Maven Developers to
Adobe Flex
Copyright © 2009 by Filippo di Pisa
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-4302-2385-6
ISBN-13 (electronic): 978-1-4302-2386-3
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of
the trademark owner, with no intention of infringement of the trademark.
President and Publisher: Paul Manning
Lead Editor: Tom Welsh
Technical Reviewer: Bradford Taylor
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell,
Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes,
Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft,
Matt Wade, Tom Welsh
Project Manager: Debra Kelly
Copy Editor: Sharon Terdeman
Compositor: Tricia Bronkella
Indexer: Ann Rogers and Ron Strauss
Artist: April Milne
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com,
or visit http://www.springeronline.com.
For information on translations, please e-mail info@apress.com, or visit http://www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our
Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales.
The information in this book is distributed on an “as is” basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have
any liability to any person or entity with respect to any loss or damage caused or alleged to be caused
directly or indirectly by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com. A "live" version of the
source code is maintained by the author at
https://filippodipisa.svn.cvsdude.com/apress_flexjava/archive/. (Username: apressapress,
Password: FlexAndJava).
ii
■ CONTENTS
I dedicate this book to my father, Pino, my mother, Raffaella, my sister, Beatrice, and my wife,
Soledad, the most important people in my life.
iii
Contents at a Glance
Contents at a Glance......................................................................... iv
Contents ...........................................................................................v
About the Author ............................................................................. xii
About the Technical Reviewer.......................................................... xiii
Acknowledgments............................................................................xiv
Introduction .................................................................................... xv
■Chapter 1: Developing with Java and Flex ........................................1
■Chapter 2: Presenting the Sample Application ................................. 21
■Chapter 3: Configuring Your Development Environment ................... 31
■Chapter 4: Inversion of Control....................................................... 89
■Chapter 5: Spring JDBC and Hibernate .......................................... 131
■Chapter 6: Spring Security .......................................................... 183
■Chapter 7: Flex (The View Layer) .................................................. 195
■Chapter 8: Working with Data in Flex............................................ 251
■Chapter 9: BlazeDS ..................................................................... 303
■Chapter 10: Using Flex, Spring, and Hibernate Together ................. 353
iv
■
Contents
Contents at a Glance..........................................................................iv
Contents ............................................................................................v
About the Author ..............................................................................xii
About the Technical Reviewer...........................................................xiii
Acknowledgments............................................................................ xiv
Introduction ..................................................................................... xv
■Chapter 1: Developing with Java and Flex ........................................1
Why Java?............................................................................................................................................1
Why ActionScript?..........................................................................................................2
Why Java and ActionScript Together?...........................................................................2
Programming Using Lightweight Frameworks...............................................................3
Benefits of Lightweight Frameworks...................................................................................................3
Introduction to Spring....................................................................................................4
Introduction to Hibernate...............................................................................................8
The Benefits of Hibernate....................................................................................................................9
Introduction to BlazeDS...............................................................................................10
BlazeDS vs. Adobe LiveCycle Data Services......................................................................................11
Introduction to Flex......................................................................................................13
Flex vs. Ajax .....................................................................................................................................13
Flex, Flash Cs3, and ActionScript.......................................................................................................14
ActionScript vs. MXML.......................................................................................................................15
v
■ CONTENTS
Introduction to UML......................................................................................................15
Basic Diagrams..................................................................................................................................15
Summary......................................................................................................................18
■Chapter 2: Presenting the Sample Application ................................. 21
Architecture.................................................................................................................21
The Presentation Layer................................................................................................25
The Service Layer........................................................................................................25
The Data Access Layer.................................................................................................27
The Domain Model.......................................................................................................28
Summary......................................................................................................................30
■Chapter 3: Configuring Your Development Environment ................... 31
The Source Code Editor: Eclipse IDE............................................................................31
Eclipse Projects..................................................................................................................................32
Eclipse Plug-ins.................................................................................................................................35
Installing Eclipse................................................................................................................................36
Configure Eclipse for Flex and J2EE..................................................................................................37
Version Control: Subversion.........................................................................................40
Subversion Installation.......................................................................................................................42
Basic SVN Project Structure...............................................................................................................42
Using SVN with the Command-Line Client.........................................................................................42
Installing Subclipse............................................................................................................................48
The Database Tools: MySQL.........................................................................................49
Install MySQL on Windows.................................................................................................................50
Installing MySQL on a Mac.................................................................................................................52
Adding the MySQL GUI Tools..............................................................................................................53
Basic MySQL Operation from the Command Line..............................................................................54
Basic MySQL Operations Using MySQL Query Browser.....................................................................57
vi
■ CONTENTS
The Java Application Container: Tomcat......................................................................63
Installing Tomcat................................................................................................................................64
Tomcat Directories.............................................................................................................................65
Tomcat Configuration Files................................................................................................................65
The Presentation Tools: Flex Builder............................................................................66
Installing the Flex Builder..................................................................................................................66
Installing the Flex SDK 4....................................................................................................................69
The Build, Test and Deploy Tool: Maven......................................................................71
Installing Maven.................................................................................................................................72
Configuring Maven.............................................................................................................................72
Installing the Maven Eclipse Plug-in..................................................................................................73
Creating Your First Maven Project.....................................................................................................73
The POM Document............................................................................................................................75
Building a Project with Maven...........................................................................................................77
Using Maven Plug-ins........................................................................................................................77
Using Maven Dependencies...............................................................................................................79
Using Repositories.............................................................................................................................79
Deploying Your Application................................................................................................................81
Creating a Maven Archetype..............................................................................................................82
Flex Maven Archetypes......................................................................................................................83
Useful Maven Commands..................................................................................................................83
Summary......................................................................................................................87
■Chapter 4: Inversion of Control....................................................... 89
Spring Modules............................................................................................................91
Spring Maven Dependencies.......................................................................................93
Creating a Spring Project.............................................................................................97
Configure the Spring Container..................................................................................108
XML-Based Configuration................................................................................................................108
Injecting Lists and Collections.........................................................................................................112
Annotation-Based Configuration......................................................................................................117
vii
■ CONTENTS
The Bean Factory.............................................................................................................................124
ApplicationContext and WebApplicationContext..............................................................................125
Properties.........................................................................................................................................125
Summary....................................................................................................................129
■Chapter 5: Spring JDBC and Hibernate .......................................... 131
The DAO Design Pattern.............................................................................................131
Introduction to Plain Old JDBC...................................................................................133
Introduction to Spring JDBC.......................................................................................141
JDBC Template.................................................................................................................................141
JDBC DAO Support...........................................................................................................................141
Hibernate and Spring.................................................................................................148
Add Hibernate to your Spring Project...............................................................................................148
Configure Hibernate.........................................................................................................................150
XML-Based Configuration................................................................................................................153
Annotation-Based Configuration......................................................................................................157
Using Hibernate with Spring......................................................................................173
Querying Using HQL.........................................................................................................................174
HQL and Hibernate Support Matrix..................................................................................................176
Use Native SQL.................................................................................................................................179
Introduction to Transactions............................................................................................................179
Summary..........................................................................................................................................182
■Chapter 6: Spring Security........................................................... 183
Introduction to Spring Security..................................................................................183
Web Authorization Using URL Patterns......................................................................184
The Importance of Filters...........................................................................................184
Authentication and Authorization...............................................................................186
Authentication Methods...................................................................................................................187
Decision Managers and Voters........................................................................................................192
Summary....................................................................................................................194
viii
■ CONTENTS
■Chapter 7: Flex (The View Layer) .................................................. 195
The FlashPlayer Overview..........................................................................................195
Flex Components.......................................................................................................195
The Flex Framework Architecture..............................................................................196
Flex Development Overview.......................................................................................198
Flex Builder................................................................................................................198
Create a Project................................................................................................................................199
Flex Builder Perspectives.................................................................................................................205
Build an Application.........................................................................................................................205
Run an Application...........................................................................................................................206
Debug an Application.......................................................................................................................207
Navigate between Classes...............................................................................................................207
Shortcut keys...................................................................................................................................208
Flex Components.......................................................................................................209
Containers........................................................................................................................................213
Layout Containers............................................................................................................................213
Navigation Containers......................................................................................................................214
Control Components.........................................................................................................................215
Using External CSS Styles..........................................................................................219
Use Flex with Flash IDE..............................................................................................222
Create Flash Animations for Flex.....................................................................................................226
Flex Events.................................................................................................................229
Custom Events.................................................................................................................................233
Data Binding...............................................................................................................237
Creating Custom Components...................................................................................239
MXML Custom Components.............................................................................................................240
AS Custom Components...................................................................................................................241
Deploying Custom Components.......................................................................................................245
Summary....................................................................................................................249
ix
Description:Over the past few years, the now-open source Adobe Flex framework has been adopted by the Java community as the preferred framework for Java rich Internet applications (RIAs) using Flash for the presentation layer. Flex helps Java developers to build and maintain expressive web/desktop applications