Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Spring Boot Cookbook
Spring Boot Cookbook

Spring Boot Cookbook: Over 35 recipes to help you build, test, and run Spring applications using Spring Boot

eBook
€17.99 €20.99
Paperback
€26.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Spring Boot Cookbook

Chapter 1. Getting Started with Spring Boot

Spring Boot has a lot of starters that are already a part of the Spring Boot family. This chapter will provide you with an overview of http://start.spring.io/, available components provided by Spring Boot, and will also show you how to make a project Bootiful, as Josh Long likes to call it.

In this chapter, we will learn about the following topics:

  • Using a Spring Boot template and starter
  • Creating a simple application
  • Launching an application using Gradle
  • Using the command-line runners
  • Setting up a database connection
  • Setting up a data repository service
  • Scheduling executors

Introduction

In a fast-paced world of today's software development, the speed of an application creation and the need for rapid prototyping are becoming more and more important. If you are developing a software using a JVM language, Spring Boot is exactly the kind of framework that will give you the power combined with the flexibility that will enable you to produce high-quality software at a rapid pace. So, let's take a look at how Spring Boot can help you to make your application Bootiful.

Using a Spring Boot template and starters

Spring Boot comes with over 40 different starter modules, which provide ready-to-use integration libraries for many different frameworks, such as database connections that are both relational and NoSQL, web services, social network integration, monitoring libraries, logging, template rendering, and the list just keeps going. While it is not practically feasible to cover every single one of these components, we will go over the important and popular ones in order to get an idea of the realm of possibilities and the ease of application development that Spring Boot provides us with.

How to do it…

We will start with creating a basic simple project skeleton and Spring Boot will help us in this:

  1. Let's head over to http://start.spring.io.
  2. Fill out a simple form with the details about our project.
  3. Clicking on Generate Project will download a premade project skeleton for us to start with.

How it works…

You will see the Project Dependencies section, where we can choose the kind of functionalities that our application will perform: will it connect to a database, will it have a web interface, do we plan to integrate with any of the social networks, provide runtime operational support capabilities, and so on. By selecting the desired technologies, the appropriate starter libraries will be added automatically to the dependency list of our pregenerated project template.

Before we proceed with the generation of our project, let's go over exactly what a Spring Boot starter is and the benefits it provides us with.

Spring Boot aims to simplify the process of getting started with an application creation. Spring Boot starters are bootstrap libraries that contain a collection of all the relevant transitive dependencies that are needed to start a particular functionality. Each starter has a special file, which contains the list of all the provided dependencies—spring.provides. Let's take a look at the following link for a spring-boot-starter-test definition as an example:

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-starters/spring-boot-starter-test/src/main/resources/META-INF/spring.provides

Here we will see the following:

provides: spring-test, spring-boot, junit, mockito, hamcrest-library

This tells us that by including spring-boot-starter-test in our build as a dependency, we will automatically get spring-test, spring-boot, junit, mockito, and hamcrest-library. These libraries will provide us with all the necessary things in order to start writing application tests for the software that we will develop, without needing to manually add these dependencies to the build file individually.

With more than 40 starters provided and with the ongoing community additions increasing the list, it is very likely that in case we find ourselves with the need to integrate with a fairly common or popular framework, there is already a starter out there that we can use.

The following table shows you the most notable ones so as to give you an idea of what is available:

Starter

Description

spring-boot-starter

This is the core Spring Boot starter that provides you with all the foundational functionalities. It is depended upon by all other starters, so there is no need to declare it explicitly.

spring-boot-starter-actuator

This starter provides you with a functionality to monitor, manage an application, and audit it.

spring-boot-starter-jdbc

This starter provides you with a support to connect and use JDBC databases, connection pools, and so on.

spring-boot-starter-data-jpa

The JPA starter provides you with needed libraries in order to use Java Persistence API such as Hibernate, and others.

spring-boot-starter-data-*

Collection of data-* family starter components providing support for a number of data stores such as MongoDB, Data-Rest, or Solr.

spring-boot-starter-security

This brings in all the needed dependencies for spring-security.

spring-boot-starter-social-*

This provides you with integration with Facebook, Twitter, and LinkedIn.

spring-boot-starter-test

This is a starter that contains the dependencies for spring-test and assorted testing frameworks such as JUnit and Mockito among others.

spring-boot-starter-web

This gives you all the needed dependencies for web application development. It can be complimented with spring-boot-starter-hateoas, spring-boot-starter-websocket, spring-boot-starter-mobile, or spring-boot-starter-ws, and assorted template rendering starters such as sping-boot-starter-thymeleaf or spring-boot-starter-mustache.

Creating a simple application

Now that we have a basic idea of the starters that are available to us, let's go ahead and create our application template at http://start.spring.io.

How to do it…

The application that we are going to create is a book catalog management system. It will keep a record of books that were published, who were the authors, the reviewers, publishing houses, and so forth. We will name our project BookPub, and apply the following steps:

  1. Use the default proposed Group name: org.test.
  2. Enter bookpub for an Artifact field.
  3. Provide BookPub as a Name for the application.
  4. Specify org.test.bookpub as our Package Name.
  5. Choose Gradle Project.
  6. Select Jar as Packaging.
  7. Use Java Version as 1.8.
  8. Use Spring Boot Version as 1.2.5.
  9. Select the H2, JDBC, and JPA starters from the Project Dependencies selection so that we can get the needed artifacts in our build file in order to connect to an H2 database
  10. Click Generate Project to download the project archive.

How it works…

Clicking on the Generate Project button will download the bookpub.zip archive, which we will extract in our working directory. In the newly created bookpub directory, we will see a build.gradle file that defines our build. It already comes preconfigured with the right version of a Spring Boot plugin and libraries and even includes the extra starters, which we have chosen.

The following is the code of the build.gradle file:

dependencies {
  compile("org.springframework.boot:spring-boot-starter-data-jpa")
  compile("org.springframework.boot:spring-boot-starter-jdbc")
  runtime("com.h2database:h2")
  testCompile("org.springframework.boot:spring-boot-starter-test") 
}

We have selected the following starters:

  • org.springframework.boot:spring-boot-starter-data-jpa pulls in the JPA dependency
  • org.springframework.boot:spring-boot-starter-jdbc pulls in the JDBC supporting libraries
  • com.h2database:h2 is a particular type of database implementation, namely H2

As you can see, the runtime("com.h2database:h2") dependency is a runtime one. This is because we don't really need, and probably don't even want, to know the exact kind of a database to which we will connect at the compile time. Spring Boot will autoconfigure the needed settings and create appropriate beans once it detects the presence of the org.h2.Driver class in the classpath when the application is launched. We will look into the inner workings of how and where this happens later in this chapter.

The data-jpa and jdbc are Spring Boot starter artifacts. If we look inside these dependency jars once they are downloaded locally by Gradle, or using Maven Central online file repository, we will find that they don't contain any actual classes, only the various metadata. The two containing files that are of particular interest to us are pom.xml and spring.provides. Let's first look at the spring.provides file in the spring-boot-starter-jdbc.jar artifact, with the following content:

provides: spring-jdbc,spring-tx,tomcat-jdbc

This tells us that by having this starter as our dependency, we will transitively get the spring-jdbc, spring-tx, and tomcat-jdbc dependency libraries in our build. The pom.xml file contains the proper dependency declarations that will be used by Gradle or Maven to resolve the needed dependencies during the build time. This also applies to our second starter: spring-boot-starter-data-jpa. This starter will transitively provide us with the spring-orm, hibernate-entity-manager, and spring-data-jpa libraries.

At this point, we have enough libraries/classes in our application classpath so as to give Spring Boot an idea of what kind of application we are trying to run and what are the kind of facilities and frameworks that need to be configured automatically by Spring Boot in order to stitch things together.


Earlier, we mentioned that the presence of the org.h2.Driver class in the classpath will trigger Spring Boot to automatically configure the H2 database connection for our application. To see exactly how this will happen, let's start by looking at our newly created application template, specifically at BookPubApplication.java located in the src/main/java/org/test/bookpub directory in the root of the project, as follows:

package org.test.bookpub;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BookPubApplication {

  public static void main(String[] args) {
    SpringApplication.run(BookPubApplication.class, args);
  }
}

This is effectively our entire and fully runnable application. There's not a whole lot of code here and definitely no mention about configuration or databases anywhere. The key to making magic is the @SpringBootApplication meta-annotation. In order to understand what actually happens, we can take a look inside the code definition for this annotation, where we will find the real annotations that will direct Spring Boot to set things up automatically:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {…}

Let's go through the following list of annotations:

  • @Configuration tells Spring (and not just Spring Boot, as it is a Spring Framework core annotation) that the annotated class contains Spring configuration definitions such as the @Bean, @Component, and @Service declarations, and others.
  • @ComponentScan tells Spring that we want to scan our application packages—starting from the package of our annotated class as a default package root—for the other classes that might be annotated with @Configuration, @Controller, and other applicable annotations, which Spring will automatically include as part of the context configuration.
  • @EnableAutoConfiguration is a part of the Spring Boot annotation, which is a meta-annotation on its own (you will find that Spring libraries rely very heavily on the meta-annotations in order to group and compose configurations together). It imports the EnableAutoConfigurationImportSelector and AutoConfigurationPackages.Registrar classes that effectively instruct Spring to automatically configure the conditional beans depending on the classes available in the classpath. (We will cover the inner workings of autoconfiguration in detail in Chapter 4, Writing Custom Spring Boot Starters).

The SpringApplication.run(BookPubApplication.class, args); line in the main method basically creates a Spring application context that reads the annotations in BookPubApplication.class and instantiates a context, which is similar to how it would have been done if instead of using Spring Boot we would have stuck with the regular Spring Framework.

Launching an application using Gradle

Typically, the very first step of creating any application is to have a basic skeleton, which can be immediately launched as is. As the Spring Boot starter has created the application template for us already, all we have to do is extract the code, build, and execute it. Now let's go to the console and launch the app with Gradle.

How to do it…

  1. Change in the directory where the bookpub.zip archive was extracted from and execute the following command from the command line:
    $ ./gradlew clean bootRun
    

Tip

If you don't have gradlew in the directory, then download a version of Gradle from https://gradle.org/downloads or install it via homebrew by executing brew install gradle. After Gradle is installed, run gradle wrapper to get the Gradle wrapper files generated. Another way is to invoke gradle clean bootRun in order to achieve the same results.

The output of the preceding command will be as follows:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v1.2.3.BUILD-SNAPSHOT)

2015-03-09 23:18:53.721 : Starting BookPubApplication on mbp with PID 43850 
2015-03-09 23:18:53.781 : Refreshing org.springframework.context.annotation.Annotatio
2015-03-09 23:18:55.544 : Building JPA container EntityManagerFactory for persistence 
2015-03-09 23:18:55.565 : HHH000204: Processing PersistenceUnitInfo [name: default	
2015-03-09 23:18:55.624 : Hibernate Core {4.3.8.Final}
2015-03-09 23:18:55.625 : HHH000206: hibernate.properties not found
2015-03-09 23:18:55.627 : HHH000021: Bytecode provider name : javassist
2015-03-09 23:18:55.774 : HCANN000001: Hibernate Commons Annotations {4.0.5.Final
2015-03-09 23:18:55.850 : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2015-03-09 23:18:55.902 : HHH000397: Using ASTQueryTranslatorFactory
2015-03-09 23:18:56.094 : HHH000227: Running hbm2ddl schema export
2015-03-09 23:18:56.096 : HHH000230: Schema export complete
2015-03-09 23:18:56.337 : Registering beans for JMX exposure on startup
2015-03-09 23:18:56.345 : Started BookPubApplication in 3.024 seconds (JVM running...
2015-03-09 23:18:56.346 : Closing org.springframework.context.annotation.AnnotationC..
2015-03-09 23:18:56.347 : Unregistering JMX-exposed beans on shutdown
2015-03-09 23:18:56.349 : Closing JPA EntityManagerFactory for persistence unit 'def…
2015-03-09 23:18:56.349 : HHH000227: Running hbm2ddl schema export
2015-03-09 23:18:56.350 : HHH000230: Schema export complete
BUILD SUCCESSFUL
Total time: 52.323 secs

How it works…

As we can see, the application started just fine, but as we didn't add any functionality or configure any services, it terminated right away. From the startup log, however, we do see that the autoconfiguration did take place. Let's take a look at the following lines:

Building JPA container EntityManagerFactory for persistence unit 'default'
HHH000412: Hibernate Core {4.3.8.Final}
HHH000400: Using dialect: org.hibernate.dialect.H2Dialect

This information tells us that because we added the jdbc and data-jpa starters, the JPA container was created and will use Hibernate 4.3.8.Final to manage the persistence using H2Dialect. This was possible because we had the right classes in the classpath.

Tip

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.

Using the command-line runners

With our basic application skeleton ready, let's add some meat to the bones by making our application do something.

Let's start by first creating a class named StartupRunner. This will implement the CommandLineRunner interface, which basically provides just one method—public void run(String… args)—that will get called by Spring Boot only once after the application has started.

How to do it…

  1. Create the file named StartupRunner.java under the src/main/java/org/test/bookpub/ directory from the root of our project with the following content:
    package org.test.bookpub;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.boot.CommandLineRunner;
    public class StartupRunner implements CommandLineRunner {
      protected final Log logger = LogFactory.getLog(getClass());
      @Override
      public void run(String... args) throws Exception {
        logger.info("Hello");
      }
    }
  2. After we have defined the class, let's proceed by defining it as @Bean in the BookPubApplication.java application configuration, which is located in the same folder as our newly created StartupRunner.java, shown as follows:
    @Bean
    public StartupRunner schedulerRunner() {
      return new StartupRunner();
    }

How it works…

If we run our application again by executing $ ./gradlew clean bootRun, we will get an output that is similar to our previous application startup. However, we will see our Hello message in the logs as well, which will look as follows:

2015-03-10 21:57:51.048  INFO --- org.test.bookpub.StartupRunner         : Hello

Even though the program will get terminated on execution, at least we made it do something!

Command line runners are a useful functionality to execute the various types of code that only have to be run once, right after application startup. Some may also use this as a place to start various executor threads but Spring Boot provides a better solution for this task, which will be discussed at the end of this chapter. The CommandLineRunner interface is used by Spring Boot to scan all of its implementations and invoke each instance's run method with the startup arguments. We can also use an @Order annotation or implement an Ordered interface so as to define the exact order in which we want Spring Boot to execute them. For example, Spring Batch relies on the runners in order to trigger the execution of the jobs.

As command-line runners are instantiated and executed after the application has started, we can use the dependency injection to our advantage in order to wire in whatever dependencies that we need, such as data sources, services, and other components. These can be utilized later while implementing run(String... args) method..

Note

It is important to note that if any exceptions are thrown inside the run(String… args) method, this will cause the context to close and an application to shut down. Wrapping the risky code blocks with try/catch is recommended in order to prevent this from happening.

Setting up a database connection

In every application, there is a need to access some data and conduct some operations on it. Most frequently, this source of data is a data store of some kind, namely a database. Spring Boot makes it very easy to get started in order to connect to the database and start consuming the data via the Java Persistence API among others.

Getting ready

In our previous example, we created the basic application that will execute a command-line runner by printing a message in the logs. Let's enhance this application by adding a connection to a database.

Earlier, we already added the necessary jdbc and data-jpa starters as well as an H2 database dependency to our build file. Now, we will configure an in-memory instance of the H2 database.

Tip

In the case of an embedded database such as H2, HSQL, or Derby, no actual configuration is required besides including the dependency on one of these in the build file. When one of these databases is detected in the classpath and a DataSource bean dependency is declared in the code, Spring Boot will automatically create one for you.

To demonstrate the fact that just by including the H2 dependency in the classpath, we will automatically get a default database, let's modify our StartupRunner.java to look 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());
  }
}

Now, if we proceed with the running of our application, we will see the name of the DataSource printed in the log, as follows:

2015-03-11 21:46:22.067 org.test.bookpub.StartupRunner :DataSource: org.apache.tomcat.jdbc.pool.DataSource@4…{…driverClassName=org.h2.Driver; … }

So, under the hood, Spring Boot recognized that we've autowired a DataSource dependency and automatically created one initializing the in-memory H2 datastore. It is all good and well, but probably not all too useful beyond an early prototyping phase or for the purpose of testing. Who would want a database that goes away with all the data as soon as your application shuts down and you have to start with a clean slate every time you restart the app?

How to do it…

Let's change the defaults in order to create an embedded H2 database that will not store data in-memory, but rather use a file to persist the data in between application restarts.

  1. Open the file named application.properties under the src/main/resources directory from the root of our project and add the following content:
    spring.datasource.url = jdbc:h2:~/test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    spring.datasource.username = sa
    spring.datasource.password =
  2. Start the application by executing ./gradlew clean bootRun from the command line.
  3. Check your home directory and you should see the following file in there: test.mv.db.

    Note

    The user home directory is located under /home/<username> on Linux and under /Users/<username> on OS X.

How it works…

Even though, by default, Spring Boot makes certain assumptions about the database configuration by examining the classpath for the presence of supported database drivers, it provides you with easy configuration options in order to tweak the database access via a set of exposed properties grouped under spring.datasource.

The things that we can configure are the url, username, password, driver-class-name, and so on. If you want to consume the datasource from a JNDI location, where an actual instance of a DataSource is being created outside the application, for example by a container, like JBoss or Tomcat, and shared via JNDI, you can configure this using the spring.datasource.jndi-name property. The complete set of possible properties is fairly large, so we will not go into all of them. However, we will cover more options in Chapter 5, Application Testing, where we will talk about mocking data for application tests using a database.

Note

By looking at various blogs and examples, you might notice that some places use dashes in property names such as driver-class-name while others use camel-cased variants such as driverClassName. In Spring Boot, these are actually two equally supported ways of naming the same property and they get translated into the same thing internally.

If you want to connect to a regular (non-embedded) database, besides just having the appropriate driver library in the classpath, we need to specify the driver of our choice in the configuration. The following snippet is what the configuration to connect to MySQL would resemble:

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

If we wanted Hibernate to create the schema automatically, based on our entity classes, we will need to add the following line to the configuration:

spring.jpa.hibernate.ddl-auto=create-drop

Note

Don't do this in the production environment, otherwise on startup, all the table schemas and data will be deleted! Use the update or validate values instead, where needed.

One can go even further in the abstraction layer and instead of auto-wiring a DataSource object, you could go straight for a JdbcTemplate. This would instruct Spring Boot to automatically create a DataSource and then create a JdbcTemplate wrapping the DataSource, thus providing you with a more convenient way of interacting with a database in a safe way. The code for JdbcTemplate is as follows:

@Autowired
private JdbcTemplate jdbcTemplate;

For the extra curious minds, one can look in the spring-boot-autoconfigure source at an org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration class so as to see the code behind the DataSource creation magic.

Setting up a data repository service

Connecting to a database and then executing good old SQL—while simplistic and straightforward—is not the most convenient way to operate on the data, map it in a set of domain objects, and manipulate the relational content. This is why multiple frameworks emerged in order to aid you with mapping the data from tables into objects, better known as Object Relational Mapping. The most notable example of such a framework is Hibernate.

In the previous example, we covered how to set up a connection to a database and configure the settings for the username, password, which driver to use, and so on. In this recipe, we will enhance our application by adding a few entity objects that define the structure of the data in the database and a CrudRepository interface to access the data.

As our application is a book tracking catalogue, the obvious domain objects would be the Book, Author, Reviewers, and Publisher.

How to do it…

  1. Create a new package folder named entity under the src/main/java/org/test/bookpub directory from the root of our project.
  2. In this newly created package, create a new class named Book with the following content:
    @Entity
    public class Book {
      @Id
      @GeneratedValue
      private Long id;
      private String isbn;
      private String title;
      private String description;
    
      @ManyToOne
      private Author author;
      @ManyToOne
      private Publisher publisher;
    
      @ManyToMany
      private List<Reviewers> reviewers;
    
      protected Book() {}
    
      public Book(String isbn, String title, Author author, Publisher publisher) {
        this.isbn= isbn;
        this.title = title;
        this.author= author;
        this.publisher= publisher;
      }
      //Skipping getters and setters to save space, but we do need them
    }
  3. As any book should have an author and a publisher, and ideally some reviewers, we need to create these entity objects as well. Let's start by creating an Author entity class under the same directory as our Book on, as follows:
    @Entity
    public class Author {
      @Id
      @GeneratedValue
      private Long id;
      private String firstName;
      private String lastName;
      @OneToMany(mappedBy = "author")
      private List<Book> books;
    
      protected Author() {}
    
      public Author(String firstName, String lastName) {...}
        //Skipping implementation to save space, but we do need it all
    }
  4. Similarly, we will create the Publisher and Reviewer classes, as shown in the following code:
    @Entity
    public class Publisher {
      @Id
      @GeneratedValue
      private Long id;
      private String name;
      @OneToMany(mappedBy = "publisher")
      private List<Book> books;
    
      protected Publisher() {}
    
      public Publisher(String name) {...}
      }
    
      @Entity
      public class Reviewer {
        @Id
        @GeneratedValue
        private Long id;
        private String firstName;
        private String lastName;
    
        protected Reviewer() {}
    
        public Reviewer(String firstName, String lastName) {
          //Skipping implementation to save space
        }
    }
  5. Now, we will create our BookRepository interface by extending Spring's CrudRepository under the src/main/java/org/test/bookpub/repository package, as follows:
    @Repository
    public interface BookRepository extends CrudRepository<Book, Long> {
      public Book findBookByIsbn(String isbn);
    }
  6. Finally, let's modify our StartupRunner in order to print the number of books in our collection instead of some random DataSource string by auto-wiring a newly created BookRepository and printing the result of a .count() call to the log, as follows:
    public class StartupRunner implements CommandLineRunner {
      @Autowired
      private BookRepository bookRepository;
    
      public void run(String... args) throws Exception {
        logger.info("Number of books: " + bookRepository.count());
      }
    }

How it works…

As you have probably noticed, we didn't write a single line of SQL or even mentioned anything about database connections, building queries, or things like that. The only hint that we are dealing with the database-backed data that we have in our code are the class and field annotations: @Entity, @Repository, @Id, @GeneratedValue, and @ManyToOne along with @ManyToMany and @OneToMany. These annotations, which are a part of the Java Persistance API, along with the extension of the CrudRepository interface are our ways of communicating with Spring about the need to map our objects to the appropriate tables and fields in the database and provide us with the programmatic ability to interact with this data.

Let's go through the following annotations:

  • @Entity indicates that the annotated class should be mapped to a database table. The name of the table will be derived from the name of the class but it can be configured, if needed. It is important to note that every entity class should have a default protected constructor, which is needed for automated instantiation and Hibernate interactions.
  • @Repository indicates that the interface is intended to provide you with the access and manipulation of data for a database. It also serves as an indication to Spring during the component scan that this instance should be created as a bean that will be available for use and injection into other beans in the application.
  • The CrudRepository interface defines the basic common methods to read, create, update, and delete data from a data repository. The extra methods that we will define in our BookRepository extension, public Book findBookByIsbn(String isbn), indicate that Spring JPA should automatically translate the call to this method to a SQL finder query selecting a Book by its ISBN field. This is a convention-named mapping that translates the method name into a SQL query. It can be a very powerful ally, allowing you to build queries such as findByNameIgnoringCase(String name) and others.
  • The @Id and @GeneratedValue annotations provide you with an indication that an annotated field should be mapped to a primary key column in the database and the value for this field should be generated, instead of being explicitly entered.
  • The @ManyToOne and @ManyToMany annotations define the relational field associations that refer to the data stored in the other tables. In our case, multiple Books belong to one Author and many Reviewers review multiple Books. The mappedBy attribute in @OneToMany annotation declaration defines a reverse association mapping. It indicates to Hibernate that the mapping source of truth is defined in the Book class, in the author or publisher fields. The Books references from within Author and Publisher classes are merely reverse associations.

    Tip

    For more information about all the vast capabilities of Spring Data, visit http://docs.spring.io/spring-data/data-commons/docs/current/reference/html/.

Scheduling executors

Earlier in this chapter, we discussed how the command-line runners can be used as a place to start the scheduled executor thread pools in order to run the worker threads in intervals. While that is certainly a possibility, Spring provides you with a more concise configuration to achieve the same goal: @EnableScheduling.

Getting ready

We will enhance our application so that it will print a count of books in our repository every 10 seconds. To achieve this, we will make the necessary modifications to the BookPubApplication and StartupRunner classes.

How to do it…

  1. Let's add an @EnableScheduling annotation to the BookPubApplication class, as follows:
    @SpringBootApplication
    @EnableScheduling
    public class BookPubApplication {…}
  2. As an @Scheduled annotation can be placed only on methods without arguments, let's add a new run() method to the StartupRunner class and annotate it with the @Scheduled annotation, as shown in the following line:
    @Scheduled(initialDelay = 1000, fixedRate = 10000)
    public void run() {
      logger.info("Number of books: " + bookRepository.count());
    }
  3. Start the application by executing ./gradlew clean bootRun from the command line so as to observe the Number of books: 0 message that shows in the logs every 10 seconds.

How it works…

Like some other annotations that we discussed in this chapter and will further discuss in the book, @EnableScheduling is not a Spring Boot annotation, but instead is a Spring Context module annotation. Similar to the @SpringBootApplication and @EnableAutoConfiguration annotations, this is a meta-annotation and internally imports the SchedulingConfiguration via the @Import(SchedulingConfiguration.class) instruction, which can be seen if looked found inside the code for the @EnableScheduling annotation class.

ScheduledAnnotationBeanPostProcessor that will be created by the imported configuration will scan the declared Spring Beans for the presence of the @Scheduled annotations. For every annotated method without arguments, the appropriate executor thread pool will be created. It will manage the scheduled invocation of the annotated method.

Left arrow icon Right arrow icon

Description

Spring Boot is Spring's convention-over-configuration solution. This feature makes it easy to create Spring applications and services with absolute minimum fuss. Spring Boot has the great ability to be customized and enhanced, and is specifically designed to simplify development of a new Spring application. This book will provide many detailed insights about the inner workings of Spring Boot, as well as tips and recipes to integrate the third-party frameworks and components needed to build complex enterprise-scale applications. The book starts with an overview of the important and useful Spring Boot starters that are included in the framework, and teaches you to create and add custom Servlet Filters, Interceptors, Converters, Formatters, and PropertyEditors to a Spring Boot web application. Next it will cover configuring custom routing rules and patterns, adding additional static asset paths, and adding and modifying servlet container connectors and other properties such as enabling SSL. Moving on, the book will teach you how to create custom Spring Boot Starters, and explore different techniques to test Spring Boot applications. Next, the book will show you examples of configuring your build to produce Docker images and self-executing binary files for Linux/OSX environments. Finally, the book will teach you how to create custom health indicators, and access monitoring data via HTTP and JMX.

Who is this book for?

If you are a Spring Developer who has good knowledge level and understanding of Spring Boot and application development and now want to learn efficient Spring Boot development techniques in order to make the existing development process more efficient, then this book is for you.

What you will learn

  • Create Spring Boot applications from scratch
  • Configure and tune web applications and containers
  • Create custom Spring Boot autoconfigurations and starters
  • Use Spring Boot Test framework with JUnit, Cucumber, and Spock
  • Configure and tune web applications and containers
  • Deploy Spring Boot as selfstarting executables and Docker containers
  • Monitor data using DropWizard, Graphite, and Dashing

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 28, 2015
Length: 206 pages
Edition : 1st
Language : English
ISBN-13 : 9781785289118
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Sep 28, 2015
Length: 206 pages
Edition : 1st
Language : English
ISBN-13 : 9781785289118
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 51.98
Spring Boot Cookbook
€26.99
Building a RESTful Web Service with Spring
€24.99
Total 51.98 Stars icon

Table of Contents

8 Chapters
1. Getting Started with Spring Boot Chevron down icon Chevron up icon
2. Configuring Web Applications Chevron down icon Chevron up icon
3. Web Framework Behavior Tuning Chevron down icon Chevron up icon
4. Writing Custom Spring Boot Starters Chevron down icon Chevron up icon
5. Application Testing Chevron down icon Chevron up icon
6. Application Packaging and Deployment Chevron down icon Chevron up icon
7. Health Monitoring and Data Visualization Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.6
(5 Ratings)
5 star 60%
4 star 40%
3 star 0%
2 star 0%
1 star 0%
Kalaiselvan Dec 25, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Hands on development book. No much details about concept
Amazon Verified review Amazon
Shubham Aggarwal May 22, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Content this book covers is very practical and concise. 99% of the code in the book is 'run as is' which is excellent.The way this book teaches each concept with Spring Boot is excellent and it has nice presentation on how each goal should be achieved with its step by step layout.The sections 'How to do it' and 'what happened' were smartly designed to separate concepts from procedure to achieve a target. Covers very good area regarding Spring Boot, Docker section is very informative as well.
Amazon Verified review Amazon
Manu Bhasin Jun 05, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Good
Amazon Verified review Amazon
Monkey Toed Freak Oct 22, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I recently starting learning Spring Boot and it's been very interesting. Developers will know though that writing the code and having it running locally isn't even half the job. Customisation for environments, deployment tasks and adding monitoring etc. can add just as much time.That's why I was very pleased with this book, I was half expecting it to be a series of small tutorials about coding with Spring Boot but it takes a very unusual approach that covers the edge cases of development and has a strong focus on what Spring Boot brings to the table. The Application Testing chapter is very good and cover using Mockito, Cucumber and Spock.The deployment chapters cover building an executable jar, accessing environment specific variables and most interesting for me, creating Docker images. It also covers application monitoring, another aspect Spring Boot gives you for free.I'm still reading through the book but it's definitely one that's full of information that you won't normally get in development books. It's well written and easy to dive into because the chapters are all self contained.If you're interested in Spring Boot, I strongly recommend this in addition to one about developing in Spring Boot.
Amazon Verified review Amazon
Amazon Customer Jun 28, 2017
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Good book. It is very helpful if u run the boot application first and than start reading.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.