Book Image

Learning Spring Boot 2.0 - Second Edition

By : Greg L. Turnquist, Greg L. Turnquist
Book Image

Learning Spring Boot 2.0 - Second Edition

By: Greg L. Turnquist, Greg L. Turnquist

Overview of this book

Spring Boot provides a variety of features that address today's business needs along with today's scalable requirements. In this book, you will learn how to leverage powerful databases and Spring Boot's state-of-the-art WebFlux framework. This practical guide will help you get up and running with all the latest features of Spring Boot, especially the new Reactor-based toolkit. The book starts off by helping you build a simple app, then shows you how to bundle and deploy it to the cloud. From here, we take you through reactive programming, showing you how to interact with controllers and templates and handle data access. Once you're done, you can start writing unit tests, slice tests, embedded container tests, and even autoconfiguration tests. We go into detail about developer tools, AMQP messaging, WebSockets, security, and deployment. You will learn how to secure your application using both routes and method-based rules. By the end of the book, you'll have built a social media platform from which to apply the lessons you have learned to any problem. If you want a good understanding of building scalable applications using the core functionality of Spring Boot, this is the book for you.
Table of Contents (11 chapters)

Delving into Spring Boot's property support

We just got things off the ground with an operational application, but that isn't the only killer feature of Spring Boot.

Spring Boot comes with a fistful of prebuilt properties. In fact, just about every autoconfigured component has some property setting ( allowing you to override just the parts you like.

Many of these autoconfigured beans will back off if Boot spots us creating our own. For example, when Spring Boot spots reactive MongoDB drivers on the classpath, it automatically creates a reactive MongoClient. However, if we define our own MongoClient bean, then Spring Boot will back off and accept ours.

This can lead to other components switching off. But sometimes, we don't need to swap out an entire bean. Instead, we may wish to merely tweak a single property of one of these autoconfigured beans.

Let's try to make some adjustments to src/main/resources/ as follows:

    # Override the port Tomcat listens on 
    # Customize log levels 

This preceding changes will cause Spring Boot to launch Netty on port 9000, as shown here:

2017-08-02 15:40:02.489: Netty started on port(s): 9000  

It will also bump up the log level for package com.greglturnquist to DEBUG.

Many modern IDEs include code completion to find various properties.

While it's handy to externalize configuration settings into property files, it wouldn't be a big advantage if they were only embeddable inside our app's JAR file.

That's why, Spring Boot comes with property override support. The following list shows all the locations from which we can override properties, the first being the highest priority:

  • The @TestPropertySource annotation on test classes
  • Command-line arguments
  • The properties found inside SPRING_APPLICATION_JSON (inline JSON embedded in an env variable or system property)
  • The ServletConfig init parameters
  • The ServletContext init parameters
  • The JNDI attributes from java:comp/env
  • The Java System properties (System.getProperties())
  • The OS environment variables
  • A RandomValuePropertySource that only has properties in random.*
  • Profile-specific properties outside the packaged JAR file (application-{profile}.properties and YAML variants)
  • Profile-specific properties inside the packaged JAR file (application-{profile}.properties and YAML variants)
  • Application properties outside the package JAR file ( and YAML variants)
  • Application properties inside the packaged JAR file ( and YAML variants)
  • The @PropertySource annotation on any @Configuration classes
  • Default properties (specified using SpringApplication.setDefaultProperties)

For an example of the same overrides in YAML format as our file, we could put the following in application.yml in src/main/resources:

      port: 9000
          greglturnquist: DEBUG  

This would do the exact same thing that we already saw with The only difference is the formatting.

What are the benefits of YAML over properties? If we need to override lots of settings, it avoids duplication of various keys.

Spring properties can also reference other properties, as shown in this fragment:
    app.description=${} is a Spring Boot application  

In this preceding example, the second property, app.description, references the first property,

This isn't the end of options with property overrides. It's just the beginning. Throughout this book, we'll expand on the options provided by Spring Boot's property support.

For now, let's focus on getting our app to production!