Spring Boot Starter Tutorial
This is a tutorial about Spring Boot Starter.
1. Introduction
Spring Boot is a Spring Framework module that provides Rapid Application Development or RAD for spring projects. In this article, we look at one of Spring Boot’s features called Spring Boot Starters
.
2. What is a Spring Boot Starter?
Without Spring Boot starters
, we need to add all the necessary dependencies and versions in the pom. With Spring Boot Starter
, we need to add just the starter name in the pom. Then all the related dependencies get added automatically to the project. Hence, Spring Boot starters
are called dependency descriptors.
Spring Boot provides many starters under the group org.springframework.boot. Their names all start with spring-boot-starter*
. The way we add a starter to our application is by using the pom.xml
The most commonly used starters are application starters
. They help to develop a specific type of application. For example, we can quickly build Spring applications with web services if we use the spring-boot-starter-web starter
. The way we add an application starter to our pom.xml
is as follows:
<dependency> <groupid > org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency>
There are 3 main types of Spring Boot starters. we take a look at each below.
3. Spring Boot Application Starters
When we include any starter in our pom.xml, Spring will download all the dependencies required for that functionality without having to explicitly add them to our pom.xml. For example, in the above example, Spring would download all the web application dependencies. There are a large number of application starters
that Spring supports. Given below is their list:
Starter Name | Description |
---|---|
spring-boot-starter-thymeleaf | MVC web applications with Thymeleaf for views. |
spring-boot-starter-data-couchbase | Couchbase document-oriented database and Spring Data Couchbase. |
spring-boot-starter-artemis | To use Apache Artemis as the JMS messaging protocol. |
spring-boot-starter-web-services | Spring Web Services application. |
spring-boot-starter-mail | Email sending applications. It also supports Javax mail API. |
spring-boot-starter-data-redis | Redis key-value data store, Spring Data Redis, and the Jedis client. |
spring-boot-starter-web | Build web and RESTful Spring applications. |
spring-boot-starter-data-gemfire | GemFire distributed data store with the Spring Data GemFire. |
spring-boot-starter-activemq | Apache ActiveMQ JMS messaging system. |
spring-boot-starter-data-elasticsearch | To enable Elasticsearch search and analytics engine with Spring Data Elasticsearch. |
spring-boot-starter-integration | Spring Integration. |
spring-boot-starter-test | Spring includes test libraries like JUnit, Hamcrest, and Mockito. |
spring-boot-starter-jdbc | Tomcat JDBC connection pool for JDBC applications. |
spring-boot-starter-mobile | Spring Mobile web applications. |
spring-boot-starter-validation | Hibernate Validator with Java Bean validator. |
spring-boot-starter-hateoas | Spring HATEOAS and MVC hypermedia-based RESTful web applications |
spring-boot-starter-jersey | This starter is an alternative starter to spring-boot-starter-web , which uses Tomcat by default. We can build RESTful applications using Jersey and JAX RS with this starter. |
spring-boot-starter-data-neo4j | Integrate the Neo4j graph database with Spring Data Neo4j. |
spring-boot-starter-data-ldap | use Spring Data LDAP. |
spring-boot-starter-websocket | Spring Framework’s WebSocket support-based WebSocket applications. |
spring-boot-starter-aop | Use Spring AOP and AspectJ for aspect-oriented programming. |
spring-boot-starter-amqp | use Spring AMQP and Rabbit MQ. |
spring-boot-starter-data-cassandra | Use Cassandra distributed database with Spring Data Cassandra. |
spring-boot-starter-social-facebook | Spring Social Facebook. |
spring-boot-starter-jta-atomikos | JTA transactions using Atomikos. |
spring-boot-starter-security | Spring Security. |
spring-boot-starter-mustache | MVC web applications with Views using Mustache. |
spring-boot-starter-data-jpa | Hibernate with Spring Data JPA. |
spring-boot-starter | The starter is the primary starter that contains the basic configuration details. |
spring-boot-starter-groovy-templates | MVC web applications with Views using Groovy. |
spring-boot-starter-freemarker | MVC web applications with Views using Freemarker. |
spring-boot-starter-batch | To enable batch jobs in spring. |
spring-boot-starter-social-linkedin | Spring Social LinkedIn. |
spring-boot-starter-cache | Spring Framework’s caching mechanism. |
spring-boot-starter-data-solr | To support apache Solr. |
spring-boot-starter- data-mongodb | To use MongoDB document-oriented database with Spring Data MongoDB. |
spring-boot-starter-jooq | To enable the use of jooq instead of jdbc for accessing SQL databases, we use this starter. |
spring-boot-starter-jta-narayana | To use the Spring Boot JTA Starter Narayana |
spring-boot-starter-cloud-connectors | To use Spring Cloud Connectors for cloud platforms like Cloud Foundry and Heroku. |
spring-boot-starter-jta-bitronix | To enable Bitronix JTA transactions |
spring-boot-starter-social-twitter | Spring Social Twitter. |
spring-boot-starter-data-rest | use Spring Data with Spring Data REST. |
4. Third-Party Starters
Along with Spring Boot starters
, we can also make custom starters
as well. These starters are known as Third-party starters and their names generally do not start with spring-boot-starter*
.
5. Spring Boot Production Starters and Technical Starters
Production Starters
provide additional features to help you monitor and manage your application in production. Production starters
are used for adding health metrics and monitoring to your application. The available ones are:
Starter name | Description |
---|---|
spring-boot-starter-actuator | Get production-ready features for monitoring and managing applications. |
Technical Starters are starters that provide functionality related to Web servers like Tomcat, Jetty, and logging.
Starter name | Description |
spring-boot-starter-undertow | To use Undertow as the embedded servlet container instead of the default Tomcat. |
spring-boot-starter-jetty | To use Jetty as the embedded servlet container instead of the default Tomcat. |
spring-boot-starter-logging | To enable Logging capability. |
spring-boot-starter-tomcat | To use Tomcat as the embedded servlet container. Tomcat is the default container used by the web starter. |
spring-boot-starter-log4j2 | Enable Log4j2 logging. |
6. Spring Boot Parent
Every application has a set of default properties that all starters
and dependencies use. The spring boot parent needs to be added for every Spring boot application. The way we add a spring boot parent starter
in our project is as follows:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> </parent>
The Spring boot starter parent does the job of dependency and plugin management. It provides the default Java version, Spring Boot versions, and Maven plugins.
7. Spring Boot Dependencies
Spring boot parent uses the spring dependencies. The spring-boot-dependencies contains a list of all dependencies it supports. The list of dependencies is available here.
Sometimes, in rare cases, we can skip adding the spring-boot-parent dependencies and do dependency management using the spring-boot-dependencies. The way we do this is by adding the following information in our pom.
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
8. Spring-Boot Starters Example
To understand how starters work, we have created a simple web-based “Hello World” Spring Boot project. The project has a Controller which displays a famous quote by Shakespeare. We have used Eclipse with Spring Tool Suite to create this project. The steps required to create this project are available here. In the pom.xml file, we have included the following starters
:
spring-boot-starter-parent
spring-boot-starter-web
spring-boot-starter-test
These starters
download all the required dependencies required to build a web-based Spring boot application. We can see the dependencies downloaded under the Maven dependencies in our project.
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.jcg.examples.springbootstarters</groupId> <artifactId>springbootstarters</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBootStartersDemo</name> <description>Demo project for Spring Boot starters</description> <properties> <java.version>16</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Since we have added the starters
and hence resolved all required Maven dependencies, we can use all the spring-boot-specific annotations such as @Controller
in our code. We do not have to worry about what jars to include, what versions to include, and so on. The starters
help us set up and maintain dependencies in an easy,hassle-free method.
9. Summary
This article looked at what Spring Boot starters
are and how we use them in our projects. They help make the tedious job of dependency and plugin management straightforward, hassle-free, and less error-prone.
10. More articles
11. Download the Source Code
The attached code is for the Simple Spring Boot application with starters
for dependency management.
You can download the full source code of this example here: Spring Boot Starter Tutorial