Spring Boot – Starters
Spring Boot Application Starters
Spring boot application starters are a set of required dependency description added for execution of project. It’s a kind of predefined template or one-stop shop for all the Spring and related technologies dependency description.
For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa
dependency in your project.
The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed dependencies.
How spring boot application starters are named?
All official starters follow a similar naming pattern; spring-boot-starter-*
, where *
is a particular type of application. This naming structure is intended to help when you need to find a starter.
List of starters can be found here.
Locating the Main Application Class @SpringBootApplication
annotation
It is recommend to locate your main application class in a root package above other classes. The @SpringBootApplication
annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items.
For example, if you are writing a JPA application, the package of the @SpringBootApplication
annotated class is used to search for @Entity
items. Using a root package also allows component scan to apply only on your project.
Example Main Application Class:
package com.talksinfo.myapplication; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan @ConfigurationPropertiesScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Using the @SpringBootApplication Annotation
A single @SpringBootApplication
annotation can be used to enable three features, that is:
@EnableAutoConfiguration
: Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added.@ComponentScan
: enable@Component
scan on the package where the application is located (see the best practices)@ConfigurationPropertiesScan
: enable@ConfigurationProperties
scan on the package where the application is located (see the best practices)@Configuration
: allow to register extra beans in the context or import additional configuration classes
Understanding the components behind the @SpringBootApplication
annotation
Following code will list out the components while loading the @SpringBootApplication
annotation
package com.talksinfo.myapplication; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan @ConfigurationPropertiesScan public class Application { public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(App.class, args); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } } }
Above code will list all the componenets while loading @SpringBootApplication
annotation as below for spring-boot-starter-web
applicationTaskExecutor basicErrorController beanNameHandlerMapping beanNameViewResolver characterEncodingFilter conventionErrorViewResolver defaultServletHandlerMapping defaultValidator defaultViewResolver dispatcherServlet dispatcherServletRegistration employeeReportController employeeReportService error errorAttributes errorPageCustomizer errorPageRegistrarBeanPostProcessor faviconHandlerMapping faviconRequestHandler formContentFilter generateReport handlerExceptionResolver handlerFunctionAdapter helloWorld helloworldApplication hiddenHttpMethodFilter httpRequestHandlerAdapter jacksonObjectMapper jacksonObjectMapperBuilder jsonComponentModule localeCharsetMappingsCustomizer mappingJackson2HttpMessageConverter messageConverters methodValidationPostProcessor multipartConfigElement multipartResolver mvcContentNegotiationManager mvcConversionService mvcHandlerMappingIntrospector mvcPathMatcher mvcResourceUrlProvider mvcUriComponentsContributor mvcUrlPathHelper mvcValidator mvcViewResolver org.springframework.boot.autoconfigure.AutoConfigurationPackages org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration org.springframework.boot.autoconfigure.condition.BeanTypeRegistry org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$ParameterNamesModuleConfiguration org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration$TomcatWebServerFactoryCustomizerConfiguration org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletConfiguration org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter$FaviconConfiguration org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration$TomcatWebSocketConfiguration org.springframework.boot.context.internalConfigurationPropertiesBinder org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.event.internalEventListenerFactory org.springframework.context.event.internalEventListenerProcessor parameterNamesModule preserveErrorControllerTargetClassPostProcessor propertySourcesPlaceholderConfigurer requestContextFilter requestMappingHandlerAdapter requestMappingHandlerMapping resourceHandlerMapping restTemplateBuilder routerFunctionMapping server-org.springframework.boot.autoconfigure.web.ServerProperties servletWebServerFactoryCustomizer simpleControllerHandlerAdapter spring.http-org.springframework.boot.autoconfigure.http.HttpProperties spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties spring.jackson-org.springframework.boot.autoconfigure.jackson.JacksonProperties spring.mvc-org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties spring.security.oauth2.resourceserver-org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties spring.servlet.multipart-org.springframework.boot.autoconfigure.web.servlet.MultipartProperties spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties spring.task.scheduling-org.springframework.boot.autoconfigure.task.TaskSchedulingProperties springApplicationAdminRegistrar standardJacksonObjectMapperBuilderCustomizer stringHttpMessageConverter taskExecutorBuilder taskSchedulerBuilder tomcatServletWebServerFactory tomcatServletWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer viewControllerHandlerMapping viewResolver webServerFactoryCustomizerBeanPostProcessor websocketServletWebServerCustomizer welcomePageHandlerMapping
You could see how many beans got registered automatically. Everything is managed by @SpringBootApplication
.
Conclusion
In this article we have given an overview of Starters, explained why we need them and provided examples on how to use them in your projects.
Let’s recap the benefits of using Spring Boot starters:
- increase pom manageability
- production-ready, tested & supported dependency configurations
- decrease the overall configuration time for the project