Pro Spring 6 An In-Depth Guide to the Spring Framework — Sixth Edition — Iuliana Cosmina Rob Harrop Chris Schaefer Clarence Ho
Pro Spring 6 An In-Depth Guide to the Spring Framework Sixth Edition Iuliana Cosmina Rob Harrop Chris Schaefer Clarence Ho
Pro Spring 6: An In-Depth Guide to the Spring Framework ISBN-13 (pbk): 978-1-4842-8639-5 ISBN-13 (electronic): 978-1-4842-8640-1 https://doi.org/10.1007/978-1-4842-8640-1 Copyright © 2023 by Iuliana Cosmina, Rob Harrop, Chris Schaefer, and Clarence Ho This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image, we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Steve Anglin Development Editor: Laura Berendson Coordinating Editor: Mark Powers Copy Editor: Bill McManus Cover designed by eStudioCalamar Cover image by Anton Darius on Unsplash (https://unsplash.com) Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC, and the sole member (owner) is Springer Science + Business Media Finance Inc. (SSBM Finance Inc). SSBM Finance Inc. is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and li- censes are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at https://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the authors in this book is available to readers on GitHub (https://github.com/Apress). For more detailed information, please visit https://www.apress.com/source-code. Printed on acid-free paper Iuliana Cosmina Kirkcaldy, UK Chris Schaefer Venice, FL, USA Rob Harrop Reddish, UK Clarence Ho Hong Kong, China
I dedicate this book to my Evelyn Walker. Thank you for being the friend I never knew I needed. —Iuliana Cosmina
■Chapter 1: Introducing Spring ������������������������������������������������������������������������������� 1 About This Book ��������������������������������������������������������������������������������������������������������������� 1 What Is Spring? ���������������������������������������������������������������������������������������������������������������� 2 Evolution of the Spring Framework �������������������������������������������������������������������������������������������������������� 2 Spring Projects ��������������������������������������������������������������������������������������������������������������������������������������� 4 Inverting Control or Injecting Dependencies? ������������������������������������������������������������������ 6 Evolution of Dependency Injection ��������������������������������������������������������������������������������������������������������� 7 Beyond Dependency Injection ���������������������������������������������������������������������������������������������������������������� 9 The Spring Community ��������������������������������������������������������������������������������������������������� 12 Alternatives to Spring ����������������������������������������������������������������������������������������������������� 13 Summary ������������������������������������������������������������������������������������������������������������������������ 13 ■Chapter 2: Getting Started ����������������������������������������������������������������������������������� 15 Conventions �������������������������������������������������������������������������������������������������������������������� 15 Who This Book Is For ������������������������������������������������������������������������������������������������������ 16 What You Need for This Book ����������������������������������������������������������������������������������������� 17 Prepare Your Development Environment ������������������������������������������������������������������������ 18 Understanding Spring Packaging ����������������������������������������������������������������������������������� 21 Table of Contents About the Authors ���������������������������������������������������������������������������������������������������xix About the Technical Reviewer ��������������������������������������������������������������������������������xxi Acknowledgments ������������������������������������������������������������������������������������������������xxiii Introduction �����������������������������������������������������������������������������������������������������������xxv v
vi Choosing Modules for Your Application �������������������������������������������������������������������������� 25 Accessing Spring Modules on the Maven Repository ��������������������������������������������������������������������������� 25 Accessing Spring Modules Using Gradle ���������������������������������������������������������������������������������������������� 27 Using Spring Boot Dependency Management �������������������������������������������������������������������������������������� 28 Using Spring Documentation ������������������������������������������������������������������������������������������ 32 Putting a Spring into Hello World ����������������������������������������������������������������������������������� 33 Building the Sample Hello World Application ���������������������������������������������������������������������������������������� 33 Refactoring with Spring ������������������������������������������������������������������������������������������������������������������������ 40 Summary ������������������������������������������������������������������������������������������������������������������������ 44 ■Chapter 3: Introducing IoC and DI in Spring �������������������������������������������������������� 45 Inversion of Control and Dependency Injection �������������������������������������������������������������� 46 Types of Inversion of Control ���������������������������������������������������������������������������������������������������������������� 46 Setter Dependency Injection ����������������������������������������������������������������������������������������������������������������� 50 Setter Injection vs� Constructor Injection ��������������������������������������������������������������������������������������������� 51 Inversion of Control in Spring ����������������������������������������������������������������������������������������� 53 Dependency Injection in Spring �������������������������������������������������������������������������������������� 53 Beans and BeanFactory ������������������������������������������������������������������������������������������������������������������������ 54 Configuring ApplicationContext ������������������������������������������������������������������������������������������������������������ 55 Basic Configuration Overview��������������������������������������������������������������������������������������������������������������� 55 Declaring Spring Components �������������������������������������������������������������������������������������������������������������� 57 Using Setter Injection ��������������������������������������������������������������������������������������������������������������������������� 59 Using Constructor Injection ������������������������������������������������������������������������������������������������������������������ 60 Using Field Injection ����������������������������������������������������������������������������������������������������������������������������� 63 Using Injection Parameters �������������������������������������������������������������������������������������������� 65 Injecting Simple Values ������������������������������������������������������������������������������������������������������������������������ 65 Injecting Values Using SpEL������������������������������������������������������������������������������������������������������������������ 66 Injection and ApplicationContext Nesting ��������������������������������������������������������������������������������������������� 68 Injecting Collections ����������������������������������������������������������������������������������������������������������������������������� 73 Using Method Injection ��������������������������������������������������������������������������������������������������� 75 Lookup Method Injection ���������������������������������������������������������������������������������������������������������������������� 75 Considerations for Lookup Method Injection ���������������������������������������������������������������������������������������� 79 ■ Table of ConTenTs
vii Understanding Bean Naming������������������������������������������������������������������������������������������ 80 Default Bean Naming Style for Beans Declared with @Component ����������������������������������������������������� 80 Customizing Bean Naming Style ���������������������������������������������������������������������������������������������������������� 81 Bean Naming Style for Beans Declared with @Bean ��������������������������������������������������������������������������� 83 Explicit Bean Naming���������������������������������������������������������������������������������������������������������������������������� 84 The @AliasFor Annotation �������������������������������������������������������������������������������������������������������������������� 86 Understanding Bean Instantiation Mode ���������������������������������������������������������������������������������������������� 87 Choosing an Instantiation Mode ������������������������������������������������������������������������������������� 90 Additional Bean Scopes�������������������������������������������������������������������������������������������������� 91 Resolving Dependencies ������������������������������������������������������������������������������������������������ 91 Autowiring Your Bean ����������������������������������������������������������������������������������������������������� 93 Constructor Autowiring ������������������������������������������������������������������������������������������������������������������������� 93 byType Autowiring �������������������������������������������������������������������������������������������������������������������������������� 96 byName Autowiring ������������������������������������������������������������������������������������������������������������������������������ 98 Yet Another Pickle ������������������������������������������������������������������������������������������������������������������������������� 100 When to Use Autowiring ��������������������������������������������������������������������������������������������������������������������� 103 Summary ���������������������������������������������������������������������������������������������������������������������� 104 ■Chapter 4: Advanced Spring Configuration and Spring Boot ����������������������������� 105 Spring’s Impact on Application Portability�������������������������������������������������������������������� 106 Bean Life-Cycle Management �������������������������������������������������������������������������������������� 107 Hooking into Bean Creation ������������������������������������������������������������������������������������������ 108 Executing a Method When a Bean Is Created ������������������������������������������������������������������������������������� 109 Implementing the InitializingBean Interface ��������������������������������������������������������������������������������������� 112 Using the JSR-250 @PostConstruct Annotation ��������������������������������������������������������������������������������� 114 Understanding Order of Resolution ���������������������������������������������������������������������������������������������������� 116 Hooking into Bean Destruction ������������������������������������������������������������������������������������� 119 Executing a Method When a Bean Is Destroyed ����������������������������������������������������������� 119 Implementing the DisposableBean Interface �������������������������������������������������������������������������������������� 120 Using the JSR-250 @PreDestroy Annotation �������������������������������������������������������������������������������������� 121 Understanding Order of Resolution ���������������������������������������������������������������������������������������������������� 122 Using a Shutdown Hook ��������������������������������������������������������������������������������������������������������������������� 122 ■ Table of ConTenTs
viii Making Your Beans “Spring Aware” ����������������������������������������������������������������������������� 123 Using the BeanNameAware Interface ������������������������������������������������������������������������������������������������� 123 Using the ApplicationContextAware Interface������������������������������������������������������������������������������������� 125 Use of FactoryBeans ���������������������������������������������������������������������������������������������������� 127 FactoryBean Example: The MessageDigestFactoryBean �������������������������������������������������������������������� 127 Accessing a FactoryBean Directly ������������������������������������������������������������������������������������������������������ 131 JavaBeans PropertyEditors ������������������������������������������������������������������������������������������ 132 Using the Built-in PropertyEditors ������������������������������������������������������������������������������������������������������ 133 Creating a Custom PropertyEditor ������������������������������������������������������������������������������������������������������ 138 More Spring ApplicationContext Configuration ������������������������������������������������������������ 140 Internationalization ������������������������������������������������������������������������������������������������������ 141 Internationalization with MessageSource ������������������������������������������������������������������������������������������ 141 Using the getMessage() Method ��������������������������������������������������������������������������������������������������������� 143 Why Use ApplicationContext As a MessageSource? ��������������������������������������������������������������������������� 144 Using MessageSource in Stand-Alone Applications ��������������������������������������������������������������������������� 144 Events Publication ������������������������������������������������������������������������������������������������������������������������������ 144 Using Application Events �������������������������������������������������������������������������������������������������������������������� 145 Considerations for Event Usage ���������������������������������������������������������������������������������������������������������� 147 Accessing Resources ��������������������������������������������������������������������������������������������������� 147 Advanced Java Configuration Classes�������������������������������������������������������������������������� 149 Profiles ������������������������������������������������������������������������������������������������������������������������� 153 An Example of Using the Spring Profiles Feature ������������������������������������������������������������������������������� 153 Considerations for Using Profiles ������������������������������������������������������������������������������������������������������� 157 Environment and PropertySource Abstraction ������������������������������������������������������������� 158 Testing Spring Applications ������������������������������������������������������������������������������������������ 164 Using Spring Test Annotations ������������������������������������������������������������������������������������������������������������ 165 Implementing Logic Unit Tests ������������������������������������������������������������������������������������������������������������ 167 Implementing an Integration Test ������������������������������������������������������������������������������������������������������� 169 Configuring Profile for Integration Testing �������������������������������������������������������������������� 173 Implementing a Front-End Unit Test ��������������������������������������������������������������������������������������������������� 175 Introducing Selenium ������������������������������������������������������������������������������������������������������������������������� 175 ■ Table of ConTenTs
ix Configuration Using Groovy ������������������������������������������������������������������������������������������ 176 Using Spring Boot ��������������������������������������������������������������������������������������������������������� 178 Summary ���������������������������������������������������������������������������������������������������������������������� 186 ■Chapter 5: Spring AOP ��������������������������������������������������������������������������������������� 189 AOP Concepts ��������������������������������������������������������������������������������������������������������������� 190 Types of AOP ����������������������������������������������������������������������������������������������������������������� 191 Static AOP ������������������������������������������������������������������������������������������������������������������������������������������� 191 Dynamic AOP �������������������������������������������������������������������������������������������������������������������������������������� 192 Choosing an AOP Type ������������������������������������������������������������������������������������������������������������������������ 192 AOP in Spring ��������������������������������������������������������������������������������������������������������������� 192 The AOP Alliance ��������������������������������������������������������������������������������������������������������������������������������� 192 Spring AOP Architecture ����������������������������������������������������������������������������������������������� 192 Joinpoints in Spring ���������������������������������������������������������������������������������������������������������������������������� 193 Aspects in Spring �������������������������������������������������������������������������������������������������������������������������������� 193 Choosing an Advice Type �������������������������������������������������������������������������������������������������������������������� 200 Advisors and Pointcuts in Spring ��������������������������������������������������������������������������������� 200 The Pointcut Interface ������������������������������������������������������������������������������������������������������������������������ 201 Available Pointcut Implementations ��������������������������������������������������������������������������������������������������� 203 Using DefaultPointcutAdvisor ������������������������������������������������������������������������������������������������������������� 204 Using DynamicMethodMatcherPointcut ��������������������������������������������������������������������������������������������� 207 Using Simple Name Matching ������������������������������������������������������������������������������������������������������������ 210 Creating Pointcuts with Regular Expressions ������������������������������������������������������������������������������������� 213 Creating Pointcuts with AspectJ Pointcut Expression ������������������������������������������������������������������������ 214 Creating Annotation Matching Pointcuts �������������������������������������������������������������������������������������������� 215 Understanding Proxies ������������������������������������������������������������������������������������������������� 217 Using JDK Dynamic Proxies ���������������������������������������������������������������������������������������������������������������� 217 Using CGLIB Proxies ��������������������������������������������������������������������������������������������������������������������������� 218 Comparing Proxy Performance ����������������������������������������������������������������������������������������������������������� 220 Choosing a Proxy to Use ��������������������������������������������������������������������������������������������������������������������� 224 ■ Table of ConTenTs
x Advanced Use of Pointcuts ������������������������������������������������������������������������������������������� 224 Using Control Flow Pointcuts �������������������������������������������������������������������������������������������������������������� 224 Using a Composable Pointcut ������������������������������������������������������������������������������������������������������������� 227 Composition and the Pointcut Interface ��������������������������������������������������������������������������������������������� 231 Pointcut Summary ������������������������������������������������������������������������������������������������������������������������������ 231 Getting Started with Introductions ������������������������������������������������������������������������������� 231 Introduction Basics ����������������������������������������������������������������������������������������������������������������������������� 232 Object Modification Detection with Introductions ������������������������������������������������������������������������������ 234 Creating an Advisor ���������������������������������������������������������������������������������������������������������������������������� 236 Putting It All Together�������������������������������������������������������������������������������������������������������������������������� 237 Introduction Summary ������������������������������������������������������������������������������������������������������������������������ 238 Framework Services for AOP ���������������������������������������������������������������������������������������� 239 Configuring AOP Declaratively ������������������������������������������������������������������������������������������������������������ 239 Using ProxyFactoryBean ��������������������������������������������������������������������������������������������������������������������� 239 Using ProxyFactoryBean for Introductions ����������������������������������������������������������������������������������������� 244 ProxyFactoryBean Summary �������������������������������������������������������������������������������������������������������������� 245 Using @AspectJ-Style Annotations ������������������������������������������������������������������������������ 245 Declarative Before Advice with AspectJ Annotations ������������������������������������������������������������������������� 247 Declarative Around Advice with AspectJ Annotations ������������������������������������������������������������������������ 252 Declarative After Advice with AspectJ Annotations ���������������������������������������������������������������������������� 255 Declarative Introductions with AspectJ Annotations �������������������������������������������������������������������������� 259 Aspect Instantiation Models ��������������������������������������������������������������������������������������������������������������� 261 Spring Boot AOP ����������������������������������������������������������������������������������������������������������� 265 Considerations for Declarative Spring AOP Configuration ������������������������������������������������������������������ 267 Summary ���������������������������������������������������������������������������������������������������������������������� 267 ■Chapter 6: Spring Data Access with JDBC �������������������������������������������������������� 269 Sample Data Model for Example Code ������������������������������������������������������������������������� 270 Exploring the JDBC Infrastructure �������������������������������������������������������������������������������� 275 Spring JDBC Infrastructure ������������������������������������������������������������������������������������������ 280 Overview and Used Packages ������������������������������������������������������������������������������������������������������������ 280 Database Connections and DataSources �������������������������������������������������������������������������������������������� 281 ■ Table of ConTenTs
xi Embedded Database Support ������������������������������������������������������������������������������������������������������������� 284 Using DataSources in DAO Classes ���������������������������������������������������������������������������������������������������� 285 Exception Handling ����������������������������������������������������������������������������������������������������������������������������� 288 The JdbcTemplate Class ��������������������������������������������������������������������������������������������������������������������� 290 Spring Classes That Model JDBC Operations ������������������������������������������������������������������������������������� 299 Spring Data Project: JDBC Extensions ������������������������������������������������������������������������������������������������ 316 Spring JDBC Testing Annotations ������������������������������������������������������������������������������������������������������� 316 Introducing Testcontainers ����������������������������������������������������������������������������������������������������������������� 321 Considerations for Using JDBC ����������������������������������������������������������������������������������������������������������� 326 Spring Boot JDBC ������������������������������������������������������������������������������������������������������������������������������� 327 Summary ���������������������������������������������������������������������������������������������������������������������� 334 ■Chapter 7: Spring with Hibernate ���������������������������������������������������������������������� 335 Sample Data Model for Example Code ������������������������������������������������������������������������� 336 Configuring Hibernate’s SessionFactory ���������������������������������������������������������������������� 337 ORM Mapping Using Hibernate Annotations ���������������������������������������������������������������� 342 Simple Mappings ���������������������������������������������������������������������������������������������������������� 344 One-to-Many Mappings ���������������������������������������������������������������������������������������������������������������������� 348 Many-to-Many Mappings ������������������������������������������������������������������������������������������������������������������� 349 The Hibernate Session Interface �������������������������������������������������������������������������������������������������������� 350 Querying Data by Using the Hibernate Query Language ���������������������������������������������� 352 Simple Querying with Lazy Fetching �������������������������������������������������������������������������������������������������� 352 Querying with Associations Fetching ������������������������������������������������������������������������������������������������� 354 Inserting Data ��������������������������������������������������������������������������������������������������������������� 358 Updating Data ��������������������������������������������������������������������������������������������������������������� 362 Deleting Data ���������������������������������������������������������������������������������������������������������������� 364 Executing SQL Native Queries �������������������������������������������������������������������������������������� 366 Executing Projections with Hibernate �������������������������������������������������������������������������� 368 Calling Stored Functions with Hibernate ���������������������������������������������������������������������� 368 Configuring Hibernate to Generate Tables from Entities ���������������������������������������������� 369 ■ Table of ConTenTs
xii Annotating Methods or Fields? ������������������������������������������������������������������������������������ 373 Considerations When Using Hibernate ������������������������������������������������������������������������� 375 Introducing jOOQ ���������������������������������������������������������������������������������������������������������� 376 Summary ���������������������������������������������������������������������������������������������������������������������� 388 ■Chapter 8: Spring with JPA ������������������������������������������������������������������������������� 389 Introducing JPA 3�1 ������������������������������������������������������������������������������������������������������ 390 Sample Data Model for Example Code ����������������������������������������������������������������������������������������������� 391 Configuring JPA’s EntityManagerFactory �������������������������������������������������������������������������������������������� 391 Using JPA Annotations for ORM Mapping ������������������������������������������������������������������������������������������� 394 Performing Database Operations with JPA ������������������������������������������������������������������ 396 Using the Java Persistence Query Language to Query Data��������������������������������������������������������������� 397 Querying with Untyped Results ���������������������������������������������������������������������������������������������������������� 405 Querying for a Custom Result Type with a Constructor Expression ���������������������������������������������������� 407 Inserting Data ������������������������������������������������������������������������������������������������������������������������������������� 409 Updating Data ������������������������������������������������������������������������������������������������������������������������������������� 411 Deleting Data �������������������������������������������������������������������������������������������������������������������������������������� 413 Using a Native Query ���������������������������������������������������������������������������������������������������� 414 Using a Simple Native Query �������������������������������������������������������������������������������������������������������������� 414 Native Querying with SQL ResultSet Mapping ������������������������������������������������������������������������������������ 415 Executing Stored Functions and Procedures �������������������������������������������������������������������������������������� 416 Using the JPA Criteria API for a Criteria Query ������������������������������������������������������������� 417 Summary ���������������������������������������������������������������������������������������������������������������������� 422 ■Chapter 9: Spring Transaction Management ����������������������������������������������������� 423 Exploring the Spring Transaction Abstraction Layer ����������������������������������������������������� 424 Transaction Types ������������������������������������������������������������������������������������������������������������������������������� 424 Implementations of the PlatformTransactionManager ������������������������������������������������� 426 Analyzing Transaction Properties ��������������������������������������������������������������������������������� 427 The TransactionDefinition Interface ���������������������������������������������������������������������������������������������������� 428 The TransactionStatus Interface ��������������������������������������������������������������������������������������������������������� 430 Sample Data Model and Infrastructure for Example Code ������������������������������������������� 431 ■ Table of ConTenTs
xiii Using Declarative Transactions ����������������������������������������������������������������������������������������������������������� 434 Rolling Back Transactions ������������������������������������������������������������������������������������������������������������������� 444 Using Programmatic Transactions ������������������������������������������������������������������������������������������������������ 450 Considerations on Transaction Management �������������������������������������������������������������������������������������� 453 Transactional Configuration with Spring Boot �������������������������������������������������������������� 454 Transactional Tests ����������������������������������������������������������������������������������������������������������������������������� 458 Considerations on Transaction Management �������������������������������������������������������������������������������������� 459 Summary ���������������������������������������������������������������������������������������������������������������������� 459 ■Chapter 10: Spring Data with SQL and NoSQL Databases ��������������������������������� 461 Introducing Spring Data JPA ���������������������������������������������������������������������������������������� 462 Using Spring Data JPA Repository Abstraction for Database Operations ������������������������������������������� 463 Using JpaRepository ��������������������������������������������������������������������������������������������������������������������������� 470 Spring Data JPA with Custom Queries ������������������������������������������������������������������������������������������������ 471 Projection Queries ������������������������������������������������������������������������������������������������������������������������������ 476 Keeping Track of Changes on the Entity Class ������������������������������������������������������������� 477 Keeping Entity Versions by Using Hibernate Envers ����������������������������������������������������� 483 Adding Tables for Entity Versioning ���������������������������������������������������������������������������������������������������� 485 Configuring EntityManagerFactory for Entity Versioning �������������������������������������������������������������������� 486 Enabling Entity Versioning and History Retrieval �������������������������������������������������������������������������������� 489 Custom Implementations for Spring Data Repositories ���������������������������������������������������������������������� 490 Spring Boot Data JPA ��������������������������������������������������������������������������������������������������� 495 Considerations Using Spring Data JPA ����������������������������������������������������������������������������������������������� 502 Spring Data with MongoDB ������������������������������������������������������������������������������������������ 502 MongoDB Concepts ���������������������������������������������������������������������������������������������������������������������������� 503 Considerations Using Spring Data �������������������������������������������������������������������������������� 510 Summary ���������������������������������������������������������������������������������������������������������������������� 511 ■ Table of ConTenTs
xiv ■Chapter 11: Validation, Formatting, and Type Conversion ��������������������������������� 513 Converting String Values Using PropertyEditors ����������������������������������������������������������� 514 Introducing Spring Type Conversion ����������������������������������������������������������������������������� 518 Implementing a Custom Converter ����������������������������������������������������������������������������������������������������� 518 Converting Between Arbitrary Types ��������������������������������������������������������������������������������������������������� 519 Field Formatting in Spring �������������������������������������������������������������������������������������������� 522 Validation in Spring ������������������������������������������������������������������������������������������������������ 526 Using the Spring Validator Interface ��������������������������������������������������������������������������������������������������� 527 Using JSR-349/Jakarta Bean Validation ��������������������������������������������������������������������������������������������� 533 Dependencies ������������������������������������������������������������������������������������������������������������������������������������� 534 Defining Validation Constraints on Domain Object Properties ������������������������������������������������������������ 534 Configuring Bean Validation Support in Spring ����������������������������������������������������������������������������������� 535 Creating a Custom Validator ��������������������������������������������������������������������������������������������������������������� 538 Using AssertTrue for Custom Validation ���������������������������������������������������������������������������������������������� 541 Deciding Which Validation API to Use ������������������������������������������������������������������������������������������������� 542 Configuring Validation in a Spring Boot Application ��������������������������������������������������������������������������� 543 Summary ���������������������������������������������������������������������������������������������������������������������� 545 ■Chapter 12: Task Execution and Scheduling ����������������������������������������������������� 547 Task Executing in Java ������������������������������������������������������������������������������������������������� 548 Task Executing in Spring ���������������������������������������������������������������������������������������������� 552 Task Scheduling in Spring �������������������������������������������������������������������������������������������� 555 Introducing the Spring TaskScheduler Abstraction ����������������������������������������������������������������������������� 555 Exploring a Sample Task ��������������������������������������������������������������������������������������������������������������������� 556 Asynchronous Task Execution in Spring ��������������������������������������������������������������������������������������������� 564 Summary ���������������������������������������������������������������������������������������������������������������������� 571 ■Chapter 13: Spring Remoting ���������������������������������������������������������������������������� 573 Communication via HTTP Using Spring REST ��������������������������������������������������������������� 574 Using JMS in Spring ����������������������������������������������������������������������������������������������������� 589 Working with Apache ActiveMQ Artemis ��������������������������������������������������������������������������������������������� 590 ■ Table of ConTenTs
xv Using Spring for Apache Kafka ������������������������������������������������������������������������������������� 597 Summary ���������������������������������������������������������������������������������������������������������������������� 609 ■Chapter 14: Spring MVC ������������������������������������������������������������������������������������ 611 Setting Up the Data and Lower-Level Layers ��������������������������������������������������������������� 612 Introducing MVC ��������������������������������������������������������������������������������������������������������������������������������� 616 Introducing Spring MVC ���������������������������������������������������������������������������������������������������������������������� 617 Enabling Internationalization (i18n) ���������������������������������������������������������������������������������������������������� 634 Using Theming and Templating����������������������������������������������������������������������������������������������������������� 637 Implementing More Complex Views ��������������������������������������������������������������������������������������������������� 641 Enabling JSR-349 (Bean Validation) ��������������������������������������������������������������������������������������������������� 660 Exception Handling ����������������������������������������������������������������������������������������������������������������������������� 664 Switching to Spring Boot �������������������������������������������������������������������������������������������������������������������� 669 Summary ���������������������������������������������������������������������������������������������������������������������� 678 ■Chapter 15: Spring REST Support ���������������������������������������������������������������������� 679 Introducing RESTful Web Services ������������������������������������������������������������������������������� 679 Using Spring MVC to Expose RESTful Web Services ��������������������������������������������������������������������������� 680 Implementing SingerController ����������������������������������������������������������������������������������������������������������� 681 Testing the RESTful-WS Application ��������������������������������������������������������������������������������������������������� 688 REST Exception Handling Using ResponseEntity<t> �������������������������������������������������������������������������� 694 REST Exception Handling Using @RestControllerAdvice �������������������������������������������������������������������� 699 RESTful-WS with Spring Boot �������������������������������������������������������������������������������������� 703 Summary ���������������������������������������������������������������������������������������������������������������������� 708 ■Chapter 16: Spring Native and Other Goodies ��������������������������������������������������� 709 Spring Native Images ��������������������������������������������������������������������������������������������������� 710 The Application ����������������������������������������������������������������������������������������������������������������������������������� 712 The Configuration ������������������������������������������������������������������������������������������������������������������������������� 715 Spring for GraphQL ������������������������������������������������������������������������������������������������������� 726 Spring Kotlin Applications �������������������������������������������������������������������������������������������� 744 ■ Table of ConTenTs
xvi The Configuration ������������������������������������������������������������������������������������������������������������������������������� 746 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 749 Summary ���������������������������������������������������������������������������������������������������������������������� 757 ■Chapter 17: Securing Spring Web Applications ������������������������������������������������� 759 Configuring Spring Security: The Classic Way �������������������������������������������������������������� 760 JDBC Authentication ��������������������������������������������������������������������������������������������������������������������������� 780 Testing Secured Web Applications ������������������������������������������������������������������������������������������������������ 784 Configuring Spring Security: The Spring Boot Way ������������������������������������������������������ 789 Summary ���������������������������������������������������������������������������������������������������������������������� 791 ■Chapter 18: Monitoring Spring Applications ����������������������������������������������������� 793 JMX Support in Spring ������������������������������������������������������������������������������������������������� 794 Exporting a Spring Bean to JMX ��������������������������������������������������������������������������������������������������������� 794 Using VisualVM for JMX Monitoring ���������������������������������������������������������������������������������������������������� 796 Monitoring Hibernate Statistics ���������������������������������������������������������������������������������������������������������� 800 JMX with Spring Boot ��������������������������������������������������������������������������������������������������� 803 Monitoring Applications with Spring Boot Actuator ������������������������������������������������������ 805 Spring Boot Actuator Endpoints ���������������������������������������������������������������������������������������������������������� 806 Using Spring Boot Actuator with Micrometer ������������������������������������������������������������������������������������� 812 Summary ���������������������������������������������������������������������������������������������������������������������� 822 ■Chapter 19: Spring WebSocket Support ������������������������������������������������������������ 823 Introducing WebSocket ������������������������������������������������������������������������������������������������ 823 Using WebSocket with Spring �������������������������������������������������������������������������������������� 824 Using the WebSocket API ��������������������������������������������������������������������������������������������� 825 Using SockJS ��������������������������������������������������������������������������������������������������������������� 832 Sending Messages with STOMP ����������������������������������������������������������������������������������� 836 Spring Boot Equivalent Application ����������������������������������������������������������������������������������������������������� 845 Summary ���������������������������������������������������������������������������������������������������������������������� 846 ■ Table of ConTenTs
xvii ■Chapter 20: Reactive Spring ������������������������������������������������������������������������������ 847 Introduction to Reactive Programming in Spring ��������������������������������������������������������� 848 Introducing Spring WebFlux ����������������������������������������������������������������������������������������� 853 Spring Boot Configuration for a Reactive application ������������������������������������������������������������������������� 856 Reactive Repository and Database ����������������������������������������������������������������������������������������������������� 858 Reactive Services ������������������������������������������������������������������������������������������������������������������������������� 864 Reactive Controllers ��������������������������������������������������������������������������������������������������������������������������� 871 Handler Classes and Functional Endpoints ���������������������������������������������������������������������������������������� 873 Reactive Error Handling ���������������������������������������������������������������������������������������������������������������������� 880 Testing Reactive Endpoints with WebTestClient ��������������������������������������������������������������������������������� 881 Reactive Web Layer ���������������������������������������������������������������������������������������������������������������������������� 884 Handler Functions Validation �������������������������������������������������������������������������������������������������������������� 891 Summary ���������������������������������������������������������������������������������������������������������������������� 899 ■Appendix A �������������������������������������������������������������������������������������������������������� 901 Index ��������������������������������������������������������������������������������������������������������������������� 915 ■ Table of ConTenTs
About the Authors Iuliana Cosmina is a Spring Certified Web Developer and a Spring Certified Core Spring Professional. She is a Spring certified expert, as defined by Pivotal, the makers of Spring Framework, Spring Boot, and other tools. Iuliana has authored books with Apress on Core Spring certification and Spring Certified web development. She is a Lead Engineer at Cloudsoft, located in Edinburgh, Scotland, and is an active coder and software contributor on Apache Brooklyn, GitHub, StackOverflow, and more. Rob Harrop is a software consultant specializing in delivering high-performance, highly scalable enterprise applications. He is an experienced architect with a particular flair for understanding and solving complex design issues. With a thorough knowledge of both Java and .NET, Rob has successfully deployed projects across both platforms. He also has extensive experience across a variety of sectors, retail and government in particular. Rob is the author of five books, including the book you are currently reading, now at its sixth edition, a widely acclaimed, comprehensive resource on the Spring Framework. Chris Schaefer is a principle software developer for Spring projects at Pivotal, the makers of Spring Framework, Boot, and other Spring tools. Clarence Ho is the senior Java architect of a Hong Kong–based software consultancy firm, SkywideSoft Technology Limited. Having worked in the IT field for more than 20 years, Clarence has been the team leader of many in-house application development projects, as well as providing consultancy services on enterprise solutions to clients. xix
About the Technical Reviewer Manuel Jordan Elera is an autodidactic developer and researcher who enjoys learning new technologies for his own experiments and creating new integrations. Manuel won the Springy Award 2013 Community Champion and Spring Champion. In his little free time, he reads the Bible and composes music on his guitar. Manuel is known as dr_pompeii. He has tech-reviewed numerous books, including Pro Spring MVC with WebFlux (Apress, 2020), Pro Spring Boot 2 (Apress, 2019), Rapid Java Persistence and Microservices (Apress, 2019), Java Language Features (Apress, 2018), Spring Boot 2 Recipes (Apress, 2018), and Java APIs, Extensions and Libraries (Apress, 2018). You can read his detailed tutorials on Spring technologies and contact him through his blog at www. manueljordanelera.blogspot.com. You can follow Manuel on his Twitter account, @dr_pompeii. xxi
Acknowledgments Writing this book was difficult. Spring has grown exponentially since the first edition of this book was written, and a small framework providing dependency injection for Java applications became a collection of projects designed to be used to develop a myriad of modern applications, from websites to mobile applications to mini applications run on Arduinos and microservices. This book has a long history; this is the sixth edition, after all. Rob Harrop and Jan Machacheck wrote a comprehensive Spring book named Pro Spring: From Professional to Expert, which was published in 2005; that was the first edition. Spring has changed dramatically over the past 18 years, and the book has evolved accordingly, so much so that there is little left of the original text. What you are reading right now is its sixth edition. In previous editions, a rotating cast of authors have taken the mantle and responsibility of making this book better and upgrading it to keep up with Spring Framework’s growth. I was brought on board for the fifth edition, to upgrade the material from Spring 4 to Spring 5, making this the second time I am involved in upgrading this book. However, I did not produce the sixth edition alone, so in this section I want to thank all the people involved: Steve Anglin for taking a gamble on me in 2014 and giving me a chance to become a technical author; Mark Powers for guiding me through the process of producing a book that is worth publishing; to my technical reviewer, Manual Jordan Elera, for making sure making sure the code in the book is good; all the grammar reviewers for making sure the text in the book is readable, understandable, and meaningful. A big thank you to the people who read the book, ran the code, and found errors and bugs that I and all those previously mentioned people missed: Rafal Nowicki, Carlos Perez, Affid Fedorov, and all the GitHub contributors. And the biggest thank you to my mentor, Achim Wagner, to my dearly departed friend, Evelyn Walker, and to my friends, Mihaela Filipiuc, Agustin Demouselle, and the Bogza-Vlad family, for always being there for me when I needed you. —Iuliana Cosmina xxiii
Comments 0
Loading comments...
Reply to Comment
Edit Comment