5 min read

What are spring boot starters?

January 14, 2018

Let’s say, you must create a web application that speaks to a database and gives the content of it over an HTTP call. To achieve this in an old-school spring project, the developers are supposed to write and maintain configurations manually.

In a traditional web application, The developers will have to,

  • Write a spring-context.xml or similar, to define and instantiate the beans required.
  • Write a definition for your database configuration with connection pooling, probably an hibernate-config.xml.
  • Write a definition for MVC configuration and their mappings/filters/etc
  • And writing a web.xml to define the web application context.
  • Add whatever dependencies needed to make the above things work in your application and maintaining the appropriate versions for compatibilities.

Here comes the help in terms of starters. Starters are a set of dependency descriptors that can be included in your application to enable a component, say database or a web module. The are one-stop-shop bundles that comes up with all the dependencies needed for that specific starter based module to work. For example, If you want your project to work as a web application, just include the dependency spring-boot-starter-web, This will add tomcat runtime dependencies to the project and it will also setup some useful defaults for the web application. And you did not even write a single XML descriptor or manual dependency definition in your build file. This is called auto-configuration.

How the starters work?

It’s simple. They are maven dependency bundles. The starters contain a bunch of the dependencies which are needed to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.

For example, If we want a project to work with database but you don’t want to do the heavy lifting in configuring the connection settings, Just add the following to your maven dependencies.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <scope>runtime</scope>
</dependency>

Note that spring boot supports some of the famous in memory databases like H2,HSQLdb and Derby out of the box. The connection properties will be initialized with vendor specific defaults.

Lets write a code block to retrieve and check the connection initialized by spring boot.

package com.springhow.starterexample;

import java.sql.SQLException;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class StarterExampleApplication {

    private static Logger logger = LoggerFactory.getLogger(StarterExampleApplication.class);

    @Autowired
    DataSource dataSource;

    @Bean
    String checkDatabase() throws SQLException {
        String s = dataSource.getConnection().toString();
        logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        logger.info("Connection name is {}", s);
        logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        return s;
    }

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

Running the above code will yield you the following result.

$ mvn clean install
$ java -jar target/starter-example-0.0.1.jar
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

2017-12-22 21:36:14.272  INFO 12432 --- [           main] c.s.s.StarterExampleApplication          : Starting StarterExampleApplication on machine with PID 12432 (C:\repo\starter-example\target\classes started by user in C:\repo\starter-example)
2017-12-22 21:36:14.276  INFO 12432 --- [           main] c.s.s.StarterExampleApplication          : No active profile set, falling back to default profiles: default
2017-12-22 21:36:14.339  INFO 12432 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@146044d7: startup date [Fri Dec 22 21:36:14 IST 2017]; root of context hierarchy
2017-12-22 21:36:15.196  INFO 12432 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-12-22 21:36:15.215  INFO 12432 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
	name: default
	...]
2017-12-22 21:36:15.263  INFO 12432 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2017-12-22 21:36:15.264  INFO 12432 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2017-12-22 21:36:15.264  INFO 12432 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2017-12-22 21:36:15.346  INFO 12432 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-12-22 21:36:15.425  INFO 12432 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2017-12-22 21:36:15.564  INFO 12432 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
2017-12-22 21:36:15.567  INFO 12432 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
2017-12-22 21:36:15.579  INFO 12432 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-12-22 21:36:15.595  INFO 12432 --- [           main] c.s.s.StarterExampleApplication          : ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2017-12-22 21:36:15.595  INFO 12432 --- [           main] c.s.s.StarterExampleApplication          : Connection name is ProxyConnection[PooledConnection[conn9: url=jdbc:h2:mem:testdb user=SA]]
2017-12-22 21:36:15.595  INFO 12432 --- [           main] c.s.s.StarterExampleApplication          : ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2017-12-22 21:36:15.762  INFO 12432 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-12-22 21:36:15.774  INFO 12432 --- [           main] c.s.s.StarterExampleApplication          : Started StarterExampleApplication in 1.848 seconds (JVM running for 2.694)
2017-12-22 21:36:15.775  INFO 12432 --- [       Thread-7] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@146044d7: startup date [Fri Dec 22 21:36:14 IST 2017]; root of context hierarchy
2017-12-22 21:36:15.776  INFO 12432 --- [       Thread-7] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2017-12-22 21:36:15.777  INFO 12432 --- [       Thread-7] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2017-12-22 21:36:15.777  INFO 12432 --- [       Thread-7] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
2017-12-22 21:36:15.777  INFO 12432 --- [       Thread-7] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete

Observe the logger entries for

Connection name is ProxyConnection[PooledConnection[conn9:url=jdbc:h2:mem:testdb user=SA]]

This tells us that an h2 in memory database is initialized with default user SA. And all of these are done by spring-boot-starter-jpa without us writing a single line of configuration.

To conclude,

Spring boot starters,

  • Fasten the development by providing plug and play configurable modules.
  • Help the developers from the burden of maintaining all the jar files needed for spring and other dependencies.
  • provides auto configuration for various starters like tomcat, mysql, rabbitmq etc.
Raja Anbazhagan

About the author

Raja is a Software Engineer with over 7 years of experience in working with Enterprise Java applications. Lately, He is focused in cloud-based Java applications and serverless technologies. He spends his spare time in stackoverflow.

Browse Categories