Book Image

Spring Boot Cookbook

By : Alex Antonov
Book Image

Spring Boot Cookbook

By: Alex Antonov

Overview of this book

Table of Contents (15 chapters)
Spring Boot Cookbook
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Spring Boot gives you the ability to create modern enterprise applications using a convention-over-configuration design principle and building on the power and flexibility of the underlying Spring Framework and its various components.

This book will help readers to create different types of Spring Boot applications, understand how to configure their behavior, and write and add custom components. They will obtain knowledge in how to be more efficient in testing, deploying, and monitoring their applications, which will help them through all the stages of the Software Development Life Cycle. By the end of the book, readers will have gained the skills and expertise to deploy and develop applications using Spring Boot.

What this book covers

Chapter 1, Getting Started with Spring Boot, provides you with an overview of the important and useful Spring Boot starters that are included in the framework. You will learn how to use the http://spring.io/ resources, how to get started with a simple project, configure the build file to contain your desired starters, and finish by creating a simple command-line application configured to execute some scheduled tasks.

Chapter 2, Configuring Web Application, provides you with examples of how to create and add the custom ServletFilters, Interceptors, Converters, Formatters and PropertyEditors to a Spring Boot web application. It will start by creating a new web application and proceed to use it as a base to customize with the above mentioned components.

Chapter 3, Web Framework Behavior Tuning, delves into fine-tuning the behavior of a web application. It will cover configuring the custom routing rules and patterns, adding additional static asset paths, and adding and modifying the servlet container connectors and other properties such as enabling SSL.

Chapter 4, Writing Custom Spring Boot Starters, shows you how to create custom Spring Boot Starters in order to provide additional behaviors and functionalities that might be required for complex enterprise applications. You will learn about the inner workings of the autoconfiguration mechanics and how to use them to selectively enable/disable default functionalities and conditionally load your own.

Chapter 5, Application Testing, explores the different techniques to test Spring Boot applications. It starts by introducing you to the testing of MVC applications, then proceeds with some tips on how to use the in-memory database with prepopulated data in order to mimic the real DB interactions during tests, and concludes with examples of behavior-driven development via testing tools such as Cucumber and Spock.

Chapter 6, Application Packaging and Deployment, shows you how every written application needs to be deployed. You will see examples of configuring their build to produce Docker images and self-executing binary files for the Linux/OSX environments. It will explore the options to configure the external applications using Consul and delve into the details of the Spring Boot environment and configuration functionalities.

Chapter 7, Health Monitoring and Data Visualization, explores the various mechanisms that Spring Boot provides to help us see the data about our application's health. It will start by showing you how to write and expose the custom health metrics and see the data using the http endpoints and JMX. It will then proceed with the overview and creation of the management commands for CRaSH and finish with the integration of the monitoring data with Graphite and Dashing using the Codahale/Dropwizard Metrics framework.

What you need for this book

For this book, you need JDK 1.8 installed in your favorite operating system: Linux, Windows, or OS X.

The rest of the software such as the Gradle build tool, all the necessary Java libraries such as Spring Boot and Spring Framework, and their dependencies as well as Docker, Consul, Graphite, Grafana, and Dashing will all be installed through the recipes in this book.

Who this book is for

This book is targeted at the Java developers who have a good knowledge level and an understanding of the Spring and Java application development, are familiar with the notions of Software Development Life Cycle (SDLC), and understand the need for different kinds of testing strategies, general monitoring, and deployment concerns. This book is for those who want to learn efficient Spring Boot development techniques, integration, and extension capabilities in order to make the existing development process more efficient.

Conventions

In this book, you will find a number of styles of text that distinguish between the different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

public class StartupRunner implements CommandLineRunner {
  protected final Log logger = LogFactory.getLog(getClass());
  @Autowired
  private DataSourceds;
  @Override
  public void run(String... args) throws Exception {
    logger.info("DataSource: "+ds.toString());
  }
}

Any command-line input or output is written as follows:

spring.datasource.driver-class-name: com.mysql.jdbc.Driver
spring.datasource.url: jdbc:mysql://localhost:3306/springbootcookbook
spring.datasource.username: root
spring.datasource.password:

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: " Clicking on Generate Project will download a premade project skeleton for us to start with.".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.