Spring Boot
Spring itself is a very popular Java-based framework for building web and enterprise applications. It's not only the Spring Core, which focuses on dependency injection. Spring Framework provides a lot of features that can make a developer's life easier out of the box and allows you to deliver needed features faster. The list is long; here are just a few examples:
- Spring data: Simplifies data access from relational and NoSQL data stores
- Spring batch: Provides a powerful batch processing framework
- Spring security: Provides numerous ways to secure applications
- Spring social: Supports integration with social networking sites such as Twitter, Facebook, GitHub, and so on
- Spring integration: An implementation of enterprise integration patterns to facilitate integration with other enterprise applications using lightweight messaging and declarative adapters
But why did Spring become so popular? There are several reasons for that:
- It uses the dependency injection approach, which encourages writing testable, loosely coupled code
- It's easy to include database transaction management capabilities
- The integration with other popular Java frameworks such as JPA/Hibernate, for example
- It includes a state of the art MVC framework for building web applications faster, separating the view from the business logic
Configuring beans in the Spring framework can be done in multiple ways such as the XML definition file, Java annotations, and code configuration. This can be a tedious process. Also, we often do a lot of boilerplate configuration all the time, for different applications. Spring Boot was born to address the complexity of configuration. We can use Spring Boot for our own purposes, and develop small, independent services that can just be run. It can be a single runnable fat JAR file, with all the Java dependencies needed to run your application. There's no need for an application server or the complicated deployment descriptor configuration. In fact, behind the scenes, Spring Boot will boot up an embedded server for you. Of course, you are not forced to use the embedded application server. You can always build a WAR file to deploy it on your own Tomcat or Wildfly, for example. It's worth knowing, that even though most things will happen automatically when running a Spring Boot application, it's not a code generation framework.
Does all of this remind you about the simplicity and portability of Docker containers? Sure it does, but on the application level. As we discussed in Chapter 3, Working with Microservices, we are moving towards architectures with smaller, independently deployable microservices. This means we will need to be able to quickly get off the ground and get running with new components. We get a lot of features out of the box when using Spring Boot. These features are delivered in the form of Maven artifacts, which you can just include in your Maven pom.xml file.
The following table shows some of the important starter projects provided by Spring Boot we will be using:
Project
Description
spring-boot-starter
Base starter for Spring Boot applications. Provides support for auto-configuration and logging.
spring-boot-starter-web
Starter project for building Spring MVC based web applications or RESTful applications. This uses Tomcat as the default embedded servlet container.
spring-boot-starter-data-jpa
Provides support for Spring Data JPA. Default implementation is Hibernate.
spring-boot-starter-validation
Provides support for Java Bean Validation API. Default implementation is Hibernate Validator.
spring-boot-starter-test
Provides support for various unit testing frameworks, such as JUnit, Mockito, and Hamcrest matchers
There are a lot more projects, which can be useful for you. We are not going to use them, but let's look at what else is available:
spring-boot-starter-web-services
Starter project for developing XML based web services
spring-boot-starter-activemq
Supports message based communication using JMS on ActiveMQ
spring-boot-starter-integration
Supports Spring Integration, framework that provides implementations for Enterprise Integration Patterns
spring-boot-starter-jdbc
Provides support for using Spring JDBC. Configures a Tomcat JDBC connection pool by default.
spring-boot-starter-hateoas
HATEOAS stands for Hypermedia as the Engine of Application State. RESTful services that use HATEOAS return links to additional resources that are related to the current context in addition to data.
spring-boot-starter-jersey
JAX-RS is the Java EE standard for developing REST APIs. Jersey is the default implementation. This starter project provides support for building JAX-RS based REST APIs.
spring-boot-starter-websocket
HTTP is stateless. Web sockets allow maintaining connection between server and browser. This starter project provides support for Spring WebSockets.
spring-boot-starter-aop
Provides support for Aspect oriented programming. Also provides support for AspectJ for advanced Aspect oriented programming.
spring-boot-starter-amqp
With default as RabbitMQ, this starter project provides message passing with AMQP.
spring-boot-starter-security
This starter project enables auto-configuration for Spring Security.
spring-boot-starter-batch
Provides support for developing batch applications using Spring Batch.
spring-boot-starter-cache
Basic support for caching using Spring Framework.
spring-boot-starter-data-rest
Support for exposing REST services using Spring Data REST.
Let's use some of these goodies to code our own Spring Boot microservice.