MANNING
Craig Walls
F
OREWORD
BY
Andrew Glover
IN ACTION
Spring Boot in Action
CRAIG WALLS
MANNING
Shelter Island
Licensed to Thomas Snead <n.ordickan@gmail.com>
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Email: orders@manning.com
©2016 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in
any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books are
printed on paper that is at least 15 percent recycled and processed without the use of elemental
chlorine.
Manning Publications Co. Development editor: Cynthia Kane
20 Baldwin Road Technical development editor: Robert Casazza
PO Box 761 Copyeditor: Andy Carroll
Shelter Island, NY 11964 Proofreader: Corbin Collins
Technical proofreader: John Guthrie
Typesetter: Gordan Salinovic
Cover designer: Marija Tudor
ISBN 9781617292545
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – EBM – 20 19 18 17 16 15
Licensed to Thomas Snead <n.ordickan@gmail.com>
iii
contents
foreword vii
preface ix
about this book xii
acknowledgments xv
1
Bootstarting Spring 1
1.1 Spring rebooted 2
Taking a fresh look at Spring 2
■
Examining Spring Boot
essentials 4
■
What Spring Boot isn’t 7
1.2 Getting started with Spring Boot 8
Installing the Spring Boot CLI 8
■
Initializing a Spring Boot
project with Spring Initializr 12
1.3 Summary 22
2
Developing your first Spring Boot application 23
2.1 Putting Spring Boot to work 24
Examining a newly initialized Spring Boot project 26
■
Dissecting
a Spring Boot project build 30
2.2 Using starter dependencies 33
Specifying facet-based dependencies 34
■
Overriding starter
transitive dependencies 35
Licensed to Thomas Snead <n.ordickan@gmail.com>
CONTENTS
iv
2.3 Using automatic configuration 37
Focusing on application functionality 37
■
Running the
application 43
■
What just happened? 45
2.4 Summary 48
3
Customizing configuration 49
3.1 Overriding Spring Boot auto-configuration 50
Securing the application 50
■
Creating a custom security
configuration 51
■
Taking another peek under the covers of
auto-configuration 55
3.2 Externalizing configuration with properties 57
Fine-tuning auto-configuration 58
■
Externally configuring
application beans 64
■
Configuring with profiles 69
3.3 Customizing application error pages 71
3.4 Summary 74
4
Testing with Spring Boot 76
4.1 Integration testing auto-configuration 77
4.2 Testing web applications 79
Mocking Spring MVC 80
■
Testing web security 83
4.3 Testing a running application 86
Starting the server on a random port 87
■
Testing HTML pages
with Selenium 88
4.4 Summary 90
5
Getting Groovy with the Spring Boot CLI 92
5.1 Developing a Spring Boot CLI application 93
Setting up the CLI project 93
■
Eliminating code noise with
Groovy 94
■
What just happened? 98
5.2 Grabbing dependencies 100
Overriding default dependency versions 101
■
Adding dependency
repositories 102
5.3 Running tests with the CLI 102
5.4 Creating a deployable artifact 105
5.5 Summary 106
Licensed to Thomas Snead <n.ordickan@gmail.com>