pro spring 3 : [a comprehensive reference and practical guide to … · evolutionof...

21
Contents Contents at a Glance - v About the Authors xxix About the Technical Reviewer xxx Chapter 1: Introducing Spring 1 What is Spring? - - 1 Inverting Control or Injecting Dependencies? 2 Evolution of Dependency Injection 3 Beyond Dependency Injection 4 Aspect-Oriented Programming with Spring 4 Spring Expression Language (SpEL) 5 Validation in Spring , 5 Accessing Data in Spring 6 Object/XML Mapping (OXM) in Spring in Spring 6 Managing Transactions 7 Simplifying and Integrating with JEE 7 MVC in the Web Tier 7 Remoting Support 8 Mail Support 8 Job Scheduling Support 9 Dynamic Scripting Support 9 Simplified Exception Handling 9 The Spring Project »9 Origins of Spring 10 The Spring Community 10 Spring for Microsoft .NET 10 The SpringSource Tool Suite/Spring IDE 10 The Spring Security Project 11 Spring Batch and Integration 11 Many Other Projects 11

Upload: others

Post on 09-Jun-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

Contents

Contents at a Glance - v

About the Authors xxix

About the Technical Reviewer xxx

Chapter 1: Introducing Spring 1

What is Spring? - - 1

Inverting Control or Injecting Dependencies? 2

Evolution of Dependency Injection 3

Beyond Dependency Injection 4

Aspect-Oriented Programming with Spring 4

Spring Expression Language (SpEL) 5

Validation in Spring ,

5

Accessing Data in Spring 6

Object/XML Mapping (OXM) in Spring in Spring 6

Managing Transactions 7

Simplifying and Integrating with JEE 7

MVC in the Web Tier 7

Remoting Support 8

Mail Support 8

Job Scheduling Support 9

Dynamic Scripting Support 9

Simplified Exception Handling 9

The Spring Project »9

Origins of Spring 10

The Spring Community 10

Spring for Microsoft .NET 10

The SpringSource Tool Suite/Spring IDE 10

The Spring Security Project 11

Spring Batch and Integration 11

Many Other Projects 11

Page 2: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

Alternatives to Spring 11

JBoss Seam Framework 12

Google Guice, 12

PicoContainer 12

JEE 6 Container 12

Summary 12

Chapter 2: Getting Started .13

Obtaining the Spring Framework 14

Downloading a Standard Distribution 14

Checking Spring Out of GitHub 14

Understanding Spring Packaging.. 14

Spring Modules 15

Choosing Modules for Your Application 17

Spring Modules on the Maven Repository 18

Analyzing Spring Dependencies 20

The Sample Applications 23

Obtaining Spring Samples Source Code 23

The Petclinic Application 24

The Petclinic Groovy Application 24

The jPetStore Application 25

Spring Configuration Basic Application 25

Spring Task and Scheduling Application 25

The Spring MVC Showcase Application 25

The Spring MVC Basic and Ajax Application 26

The Spring Petcare Application 26

Spring Webflow Sample Applications 26

Spring Documentation 27

Putting a Spring into "Hello World!" 27

Building the Sample "Hello World!"Application 27

Create Spring Project in STS 32

Refactoring with Spring 33

Summary 35

viii

Page 3: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

Chapter 3: The Sample Application 37

Requirements of the SpringBlog Application ...37

Security and Authentication 38

Viewing Blog Entries 39

Posting Blog Entries 40

Commenting on a Blog Entry 41

Filtering Out Obscenities 42

Attaching Files to a Blog Entry or Comment 44

Auditing Blog Actions ,44

RSS Feed 45

Upload Blog from an XML File 45

Implementing SpringBlog 45

Development Tool and Dependency Management 46

Application Design -46

Application Configuration Management 46

SpringBlog's Layered Application Architecture 47

Implementing the Persistence Layer 48

Implementing the Service Layer 49

Using AOP for Obscenity Filtering 49

Using Spring Transaction Support 49

Bean Validation 50

Two Different Service Layers Implementation 50

Implementing the Batch and Integration Layer 50

Implementing the Presentation Layer 50

Spring MVC 51

Using Tiles 51

RESTful-WS and OXM .-51

Implementing the Security Layer 51

Summary 51

Chapter 4: Introducing loC and Dl in Spring 53

Inversion of Control and Dependency Injection •54

Types of Inversion of Control 54

Dependency Pull 54

ix

Page 4: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

"

m CONTENTS

Contextualized Dependency Lookup 55

Constructor Dependency Injection 57

Setter Dependency Injection 57

Injection vs. Lookup 58

Setter Injection vs. Constructor Injection 59

Inversion of Control in Spring 61

Dependency Injection with Spring 62

Beans and BeanFactories 62

BeanFactory Implementations : 63

ApplicationContext 65

Configuring ApplicationContext 65

Spring Configuration Options (XML and Java Annotations) 65

Basic Configuration Overview 65

Declare Spring Components 67

Using Setter Injection 70

Using Constructor Injection 72

Avoiding Constructor Confusion 74

Injection Parameters 76

Injecting Simple Values 76

Injecting Values Using SpEL 79

Injecting Beans in the Same XML Unit 82

Injection and ApplicationContext Nesting 84

Using Collections for Injection 87

Using Method Injection 93

Lookup Method Injection 93

Method Replacement 97

Understanding Bean Naming 101

Bean Name Aliasing 102

Bean Instantiation Mode 103

Choosing an Instantiation Mode 105

Bean Scopes 106

Resolving Dependencies 107

Autowiring Your Bean 107

Different Modes of Autowiring 108

X

Page 5: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

When to Use Autowiring 110

Bean Inheritance .110

Summary .112

Chapter 5: Spring Configuration in Detail 113

Spring's impact on Application Portability ....114

Bean Life-Cycle Management 115

Hooking into Bean Creation 116

Execute a Method When a Bean Is Created 117

Implementing the InitializingBean Interface 120

Using JSR-250 ©PostConstruct Annotation 122

Order of Resolution 123

Hooking into Bean Destruction 123

Executing a Method When a Bean Is Destroyed 124

Implementing the DisposableBean Interface ,,...126

Using JSR-250 ©PreDestroy Annotation 128

Order of Resolution 130

Using a Shutdown Hook 130

Making Your Beans "Spring Aware" 131

Using the BeanNameAware Interface 131

Using the ApplicationContextAware Interface 133

Use FactoryBeans . •135

Custom FactoryBean Example: The MessageDigestFactoryBean 135

Accessing a FactoryBean Directly 138

Using the factory-bean and factory-method Attributes 139

JavaBeans PropertyEditors 141

The Built-in PropertyEditors 141

Creating a Custom PropertyEditor 147

More Spring ApplicationContext Configuration 150

Internationalization with MessageSource 151

Using ApplicationContext and MessageSource 151

The getMessageO Method 153

Why Use ApplicationContext As a MessageSource? 154

Using MessageSource in Stand-Alone Applications 154

The MessageSourceResolvable Interface 154

Page 6: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

Application Events 154

Using Application Events 155

Considerations for Event Usage 157

Accessing Resources 157

Configuration Using Java Classes 159

ApplicationContext Configuration in Java 159

Java or XML Configuration? ,165

Profiles 165

An Example of Using the Spring Profiles Feature 165

Considerations for Using Profiles 171

Environment and PropertySource Abstraction 172

Configuration Using JSR-330 Annotations 176

Summary 179

fl Chapter 6: Introducing Spring AOP 181

AOP Concepts 182

Types of AOP 183

Static AOP 183

Dynamic AOP 183

Choosing an AOP Type 184

AOP in Spring 184

The AOP Alliance 184

"Hello World!" in AOP 185

Spring AOP Architecture 187

Joinpoints in Spring 188

Aspects in Spring 188

About the ProxyFactory Class 188

Creating Advice in Spring 189

Interfaces for Advice 190

Create Before Advice 191

Securing Method Access Using Before Advice 192

Creating After-Returning Advice 196

Creating Around Advice 199

Creating Throws Advice 202

Choosing an Advice Type 204

xii

Page 7: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

Advisors and Pointcuts in Spring .205

The Pointcut Interface 205

Available Pointcut Implementations 207

Using DefaultPointcutAdvisor 209

Creating a Static Pointcut Using StaticMethodMatcherPointcut 209

Creating a Dynamic Pointcut Using DyanmicMethodMatcherPointcut 212

Using Simple Name Matching 214

Creating Pointcuts with Regular Expression 216

Creating Pointcuts with AspectJ Pointcut Expression 218

Creating Annotation Matching Pointcuts 219

Convenience Advisor Implementations 221

Understanding Proxies 222

Using JDK Dynamic Proxies 222

Using CGLIB Proxies 223

Comparing Proxy Performance 223

Which Proxy to Use? 228

Summary ...228

Chapter 7: More Spring AOP and Annotations.. 229

Advanced Use of Pointcuts 230

Use Control Flow Pointcuts 230

Using Composable Pointcut 233

Composition and the Pointcut Interface 236

Pointcutting Summary 236

Getting Started with Introductions 237

Introduction Basics 237

Object Modification Detection with Introductions 240

The IsModified Interface 240

Creating a Mixin 241

Creating an Advisor 243

Putting It All Together 243

Introduction Summary 245

Framework Services for AOP •245

Configuring AOP Declaratively 245

xiii

Page 8: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

m CONTENTS

Using ProxyFactoryBean 246

ProxyFactoryBean in Action 246

Using ProxyFactoryBean for Introductions 249

ProxyFactoryBean Summary 251

Using the aop Namespace 251

Using @AspectJ-Style Annotations 256

Considerations for Declarative Spring AOP Configuration 260

AspectJ Integration -261

About AspectJ ,261

Using Singleton Aspects 262

AOP in the Sample Application 267

Filtering Obscenities in SpringBlog 267

The BlogPosting Interface 267

Implementing ObscenityFilter 268

Obscenity Filter Summary 268

Summary 268

U Chapter 8: Spring JDBC Support 269

Sample Data Model for Example Code 270

Exploring the JDBC Infrastructure .274

Spring JDBC Infrastructure 279

Overview and Used Packages 279

Database Connections and DataSources 280

Embedded Database Support 283

Using DataSources in DAO Classes 284

Exception Handling 286

The JdbcTemplate Class 287

Initializing JdbcTemplate in a DAO Class. 287

Retrieving Single-Value-Use JdbcTemplate Class 288

Using Named Parameters with NamedParameterJdbcTemplate 290

Retrieving Domain Objects with RowMapper<T> 291

Retrieving Nested Domain Objects with ResultSetExtractor 292

Spring Classes That Model JDBC Operations 294

Setting Up JDBC DAO Using Annotations 295

xiv

Page 9: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

Querying Data Using MappingSqlQuery<T> ,299

Updating Data Using SqlUpdate 303

Inserting Data and Retrieving the Generated Key 305

Batching Operations with BatchSqIUpdate 308

Calling Stored Functions Using SqIFunction 311

Using the Java Configuration .....314

Spring Data Project: JDBC Extensions 315

Considerations for Using JDBC 316

Summary ...316

Chapter 9: Using Hibernate in Spring..... 317

Create a Hibernate Utility Project in STS 318

Sample Data Model for Example Code 321

Configuring Hibernate SessionFactory 323

ORM Mapping Using Hibernate Annotations. .326

Simple Mappings 327

One-to-Many Mappings 331

Many-to-Many Mappings 332

The Hibernate Session Interface 334

Database Operations with Hibernate 334

Query Data Using Hibernate Query Language 335

Simple Query with Lazy Fetching 335

Query with Associations Fetching ....338

Inserting Data 340

Updating Data 341

Deleting Data 342

Considerations of Using Hibernate .....343

Summary .....344

11 Chapter 10: Data Access in Spring with JPA2 345

Introducing JPA 2 .....346

Creating a Simple Spring JPA Utility Project in STS 347

Sample Data Model for Example Code 349

Configuring JPA EntityManagerFactory 350

XV

Page 10: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

ORM Mapping Using JPA Annotations 352

Eliminating the DAO Layer , 352

Injecting EntityManager into Service Layer Classes 352

Database Operations with JPA 353

Query Data Using the Java Persistence Query Language 354

Query with Untyped Results 358

Query for a Custom Result Type with a Constructor Expression 360

Inserting Data 362

Updating Data 364

Deleting Data,

365

Native Query 366

Simple Native Query 366

Native Query with SQL Resultset Mapping 367

Criteria Query Using the JPA 2 Criteria API 368

Introducing Spring Data JPA 374

Adding Spring Data JPA Library Dependencies 374

Database Operations Using Spring Data JPA Repository Abstraction 376

Keeping Track of Changes on the Entity Class 381

Keeping Entity Versions by Using Hibernate Envers 388

Adding Hibernate Envers Dependencies 389

Adding Tables for Entity Versioning 389

Configuring EntityManagerFactory for Entity Versioning 390

Coding Changes for Entity Versioning and History Retrieval 392

Testing Entity Versioning 394

Considerations When Using JPA 395

Using JPA in the Sample Application 395

Database Backend 396

Using JPA for Persistence Layer Implementation 396

Auditing and Entity Versioning 396

Summary ..396

M Chapter 11: Using MyBatis in Spring 397

Getting Started with MyBatis in Spring... 397

Introducing MyBatis 398

xvi

Page 11: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

Creating a Simple Utility Project with MyBatis Support in STS 398

Sample Data Model for Example Code 400

Configuring MyBatis SqISessionFactory and MapperScannerConfigurer 402

SQL Mapping in MyBatis -403

Mapper Interfaces and SQL Mapping Files 404

SQL Mapping XML Configuration 406

Database Operations with MyBatis ....407

Querying Data 407

Simple Selects 407

Defining the Mapping Using MyBatis Annotations ..411

One-to-Many and Many-to-Many Selects in MyBatis 412

Selects in MyBatis with Named Parameters 419

Selects in MyBatis Using Dynamic SQL 421

Inserting Data 423

Updating Data 427

Deleting Data 432

Considerations When Using MyBatis 434

Using MyBatis in the Sample Application. 434

Database Backend 435

Using MyBatis for Persistence Layer Implementation 435

Auditing and Entity Versioning 435

Summary »-435

Chapter 12: Designing and Implementing Spring-Based Applications 437

Designing to Interfaces ......438

Why Design to Interfaces 438

The Factory Pattern 439

Drawbacks of the Basic Factory Pattern 440

Externally Configurable Factories 440

Supporting Multiple Implementations Transparently 440

Supporting Multiple Instantiation Modes 442

Impact of Spring on Interface-Based Design 442

Building a Domain Object Model <442

Spring and the Domain Object Model 442

xvii

Page 12: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

il CONTENTS

The DOM Is Not the Same As a Value Object 443

Why Create a Domain Object Model 444

Modeling Domain Objects 444

Database Modeling and Domain Object Modeling 445

Modeling Domain Object Relationships 445

To Encapsulate Behavior or Not? 445

The SpringBlog Domain Object Model 446

Inheritance in the SpringBlog DOM 448Domain Behavior in SpringBlog 451Domain Object Relationships 451

Domain Object Model Summary 452

Designing and Building the Data Access Layer 453

Practical Design Considerations...., 454

Domain Objects or Data Transfer Objects?.... 454

DAO Interfaces 455DAO Granularity 455

Data Access Layer Summary 455

Designing the Service Layer , 456

Why Have a Service Layer 456

Designing Business Interfaces 457

Service Layer Dependencies 457Service Object Granularity 457

Service Layer Summary 458

Summary 458

II Chapter 13: Transaction Management 459

Exploring the Spring Transaction Abstraction Layer 460

Transaction Types 460

Implementations of the PlatformTransactionManager 461

Analyzing Transaction Properties, ., 462

The TransactionDefinition Interface 462

The TransactionStatus Interface 464

Sample Data Model and Infrastructure for Example Code 465

Creating a Simple Spring JPA Utility Project with Dependencies 465

xviii

Page 13: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

Sample Data Model and Common Classes 467

Declarative and Programmatic Transactions with Spring 469

Using Annotations for Transaction Management 470

Using XML Configuration for Transaction Management 478

Using Programmatic Transactions 481

Considerations on Transaction Management 484

Global Transactions with Spring 484

Infrastructure for Implementing the JTA Sample 485

Implementing Global Transactions with JTA .....485

Considerations on Using JTA Transaction Manager 492

Summary 493

B Chapter 14: Validation with Type Conversion and Formatting 495

Creating a Project in STS for Samples 495

Spring Type Conversion System 496

Conversion from a String Using PropertyEditors 496

Introducing Spring 3 Type Conversion 500

Implementing a Custom Converter 500

Configuring ConversionService 501

Converting Between Arbitrary Types 502

Field Formatting in Spring 3 505

Implementing a Custom Formatter 505

Configuring ConversionServiceFactoryBean 507

Validation in Spring 508

Using Spring Validator Interface 509

Using JSR-303 Bean Validation 511

Defining Validation Constraints on Object Properties .....512

Configuring Bean Validation Support in Spring 513

Creating a Custom Validator 516

Using AssertTrue for Custom Validation 518

Considerations for Custom Validation 519

Which Validation API to Use? 519

Validation with Type Conversion and Formatting in the Sample Application 520

Summary »—522

xix

Page 14: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

11 CONTENTS

H Chapter 15: Task Scheduling in Spring 523

Create a Project in STS for the Sample Projects 523

Task Scheduling in Spring 524

Introducing Spring TaskScheduler Abstraction 524

Sample Task 526

Task Scheduling Using task-namespace 530

Task Scheduling Using Annotation 532

Asynchronous Task Execution in Spring 534

Task Scheduling in the Sample Application 537

Summary .538

ffl. Chapter 16: Using Spring Remoting 539

Creating the Project in STS for the Samples 540

Implementing the Service Layer for the Samples 541

Adding Required Dependencies for the JPA Backend 541

Verifying the Project 543

Data Model for Samples , 546

Implementing and Configuring ContactService 546

Implementing ContactService 546

Configuring ContactService 550

Using the Spring HTTP Invoker 551

Exposing the Service 551

Invoking the Service 552

Using JMS in Spring 554

Setting Up ActiveMQ 555

Implementing a JMS Listener in Spring 557

Sending JMS Messages in Spring 559

Using RESTful-WS in Spring 562

Introducing RESTful Web Services 562

Adding Required Dependencies for Samples 563

The Contact RESTful Web Service 564

Using Spring MVC to Expose RESTful Web Services 564

Configuring Castor XML 565

XX

Page 15: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

Implementing the ContactController 567

Configuring the RESTful Sen/let 569

Using curl to Test RESTful-WS 572

Using RestTemplate to Access RESTful-WS 573

Securing RESTful-WS with Spring Security 577

Using JSR-303 with RESTful Web Services 581

Remoting in the Sample Application, ..582

Summary ..........583

Chapter 17: Web Applications with Spring 585

Create Project in STS for Samples 586

Implement the Service Layer for Samples.... 587

Data Model for Samples ,587

Implementing and Configuring ContactService 588

Implementing ContactService 588

Configuring ContactService 591

Introducing MVC and Spring MVC 593

Introducing MVC 593

Introducing Spring MVC ,

595

Spring MVC WebApplicationContext Hierarchy 595

Spring MVC Request Life Cycle 596

Spring MVC Configuration 597

Create the First View in Spring MVC 600

Configure the DispatcherServlet 600

Implement the ContactController 601

Implement the Contact List View 602

Testing the Contact List View .

-603

Spring MVC Project Structure Overview 605

i18n (Internationalization)..... 607

Configure i18n in DispatcherServlet Configuration 607

Modify the Contact List View for M8n Support 608

Theming and Templating ............610

Theming Support 611

View Templating with Apache Tiles 613

xxi

Page 16: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

I CONTENTS

Template Layout Design 613

Implement Page Layout Components 614

Configure Tiles in Spring MVC 617

Implement the Views for Contact Information 619

Mapping of URLs to the Views 619

Implementing the Show Contact View 620

Implementing the Edit Contact View 623

Implementing the Add Contact View 629

Enable JSR-303 Bean Validation 630

Using jQuery and jQuery Ul.. 633

Introducing jQuery and jQuery Ul 634

Enable jQuery and jQuery Ul in a View 634

Rich-Text Editing with CKEditor 636

Data Grid with Pagination using jqGrid , 639

Enable jqGrid in the Contact List View 639Enable Pagination on the Server Side

, 641

File Upload Handling 645

Configuring File Upload Support 646

Modify Views for File Upload Support 647

Modify Controller for File Upload Support 648

Securing a Web Application with Spring Security -651

Configuring Spring Security 652

Adding Login Functions to the Application 653

Using Annotations to Secure Controller Methods 657

Support for Servlet 3 Code-Based Configuration 658

Spring MVC in the Sample Application .660

MVC Implementation for SpringBlog 660

Rich User Interface and Ajax 661

Security Support 661

Servlet 3.0 Support 661

Summary 662

xxii

Page 17: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

Chapter 18: Spring Web Flow and JSF 663

Project for Sample Backend..... 663

The Sample Backend Service Layer 664

Import the Sample Backend in STS 664

Introducing Spring Web Flow 666

Spring Web Flow Modules 667

Spring Web Flow Features 668

Introducing JSF 669

View 670

Model Interaction 670

Navigation670

Application Life Cycle 671

The Sample Spring Web Flow Application 672

Design of the Sample Flow 672

Project Structure 673

Spring Web Flow and JSF Configuration 675

Adding Required Dependencies 675

Configuring JSF 675

Configuring Web Deployment Descriptor 676

Configuring Spring Web Flow and Spring MVC 678

Implementing the Sample Flow 680

Define the Flow Definition 680

Implementing the Template Page 684

Implementing a Custom Converter ,

685

Implementing the Controller and Backing Bean ,686

Implementing the Show Contact View.. -692

Implement the Add Contact Flow 695

Step 1: Enter Basic Information 695

Support of JSR-303 Bean Validation 698

Step 2: Select Hobbies 699

Step 3: Review Information 701

Step 4: Add Contact Complete 703

Summary «706

xxiii

Page 18: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

Chapter 19: Spring Testing 707

Project for Sampie Web Application ....708

Import the Sample Backend in STS 708

Introducing an Enterprise Testing Framework 710

Implementing Logic Unit Test 713

Adding Required Dependencies 713

Unit Testing Spring MVC Controller 713

Implement the Infrastructure Classes 714

Testing the Iist0 Method 715

Testing the createO Method 717

Implementing an Integration Unit Test 719

Adding Required Dependencies 719

Configuring the Profile for Service Layer Testing 719

Implementing the Infrastructure Classes 721

Implementing Custom TestExecutionListener 721

Implementing the Configuration Class 724

Implementing the Base Test Class 725

Unit Testing Service Layer 726

Implementing a Frontend Unit Test 729

Adding Required Dependencies 729

Introducing Selenium 729

Implementing a Test Case for a Frontend Ul 730

Verifying Test Case Code Coverage 733

Summary 735

Chapter 20: Spring Projects: Batch, Integration, and Roo 737

Project for Chapter Samples 738

Import the Sample Backend in STS 738

Introducing Spring Batch 739

Batch Job Flow and Process, 739

Spring Batch Infrastructure Components 740

Spring Batch Metadata 741

Job Execution Policies 743

xxiv

Page 19: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

Implementing a Batch Job 744

Adding Required Dependencies ,744

Spring Batch Infrastructure Configuration 745

Implementing the Import Contact Job 746

Using Spring Batch with Spring Integration 755

Introducing Spring Integration 755

Adding Required Dependencies 756

Implementing the File Polling Mechanism 757

Introducing Spring Roo.... 761

Configure Spring Roo in STS ,,...762

Create a Spring Roo Project 763

Set Up the Persistence Layer and Entity Class 765

Set Up the Service Layer 769

Set Up the Presentation Layer 769

Spring Roo Add-on 772

Conclusion on Spring Roo 774

Spring Batch and Spring Integration in the Sample Application 774

Summary.....774

M Chapter 21: Sample Application in Detail -775

Setting Up the Sample Application 776

Project Setup ,

776

Switching Between the MySQL and H2 Databases 780

Switching Between the JPA and MyBatis Implementations 782

Application Design 784

The Data Model 784

Domain Object Model 785

The UML Model -

786

Create Blog Post Entry 787

RESTful-WS for RSS Feed of Blog Post Entries 788

The Batch Job for Importing Blog Posts from an XML File 791

Configuration Details 793

The Web Deployment Descriptor Configuration...,, 793

The Spring WebApplicationContext Hierarchy 796

XXV

Page 20: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

The AspectJ Load-Time Weaving Configuration 798

Implementation Details 800

Service Layer Implementation 800

JPA Service Implementation 801

MyBatis Service Implementation 803

Obscenity Filter Using AOP 806

Scheduling the Job for Purging Audit Data 809

Presentation Layer , 810

Web Resource Files Folder Structure 811

Controller Class„ 812

Type Conversion and Formatting 815

Summary 817

Chapter 22: Scripting Support in Spring 819

Project for Chapter Samples..... .............820

Create a Simple Spring Utility Project 820

Installing the Groovy Plug-in for Eclipse 821

Scripting Support in Java «... 823

Introducing Groovy 825

Dynamic Typing 825

Simplified Syntax 827

Closure 827

Using Groovy with Spring 829

Adding Required Dependencies 829

The Contact Domain,

829

Implementing the Rule Engine 830

Implement the Rule Factory as a Spring Refreshable Bean 833

Testing the Age Category Rule 835

Summary 838

B Chapter 23: Spring Application Monitoring ....839

Project for Chapter Samples 840

JMX Support in Spring 842

Exporting a Spring Bean to JMX 842

xxvi

Page 21: Pro Spring 3 : [a comprehensive reference and practical guide to … · Evolutionof DependencyInjection 3 BeyondDependencyInjection 4 Aspect-Oriented Programmingwith Spring 4 Spring

CONTENTS

Setting Up VisualVM for JMX Monitoring ,844

Monitoring Logged-ln Users ....847

Monitoring Hibernate Statistics 852

Monitoring Spring Batch Jobs 854

Monitoring an Application with Spring Insight 858

Introducing Spring Insight 859

Configuring Spring Insight 859

Using Spring Insight 864

Summary — 867

9 Appendix A: SpringSource Tool Suite 869

Introducing STS 869

STS Installation 870

Installing the Stand-Alone Version of STS 870

Installing STS to an Existing Eclipse Environment 878

Project Setup and Dependency Management 884

Create a Simple Spring Utility Project... 885

Dependency Management for a Project 889

Using STS 892

Installing STS Extensions 893

Configuring VMware tc Server in STS 894

Summary •.......896

Index 897

xxvii