Spring Boot Auto Configuration

Spring Boot Auto Configuration

One of the powerful features of Spring Boot is its ability to automatically configure our application based on the jar dependencies we are adding to our classpath. In this post, we will cover Spring Boot Auto Configuration features with an understanding of how this can help in the development life-cycle.

 

Introduction

We can start by raising very simple questions, Why do we need Spring Boot Auto Configuration? What are the benefits of using Spring Boot for our application? To get better answers to these questions, let’s look at the configurations needed to create a simple Spring MVC application without Spring Boot. (These configurations are from Shopizer which we are migrating to Spring Boot)

<beans:beans>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
 	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>

	
	<beans:bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <beans:property name="defaultLocale" value="en"></beans:property>
    </beans:bean>
<!-- other configuration -->
</beans:beans>

 

We also need to configure dispatcher servlet in Web.xml file as part of the configuration. For Database applications, we need additional configurations for Hibernate / JPA along with data-source and other configurations. With enterprise Spring applications, these configurations can become complex and we might end up configuring lots of things to start our application.

 

1. What is Auto Configuration

What if the system can provide some default setup based on the jars in the classpath along with options to override it based on our requirements

  • Default servlet container for our Spring MVC application?
  • What about is system will configure default dispatcher servlet for us

 

Spring Boot Auto Configuration comes with these features. Auto-Configuration will attempt to automatically try to set up our application with default behavior based on the jars in the classpath. For example, if Spring Boot finds HSQLDB in out classpath, it will automatically configure an in-memory database for us. Think of the auto-Configuration as an intelligent system which can provide ready to use the application to us based on the configured jars in our classpath.

 

2. Auto Configuration in Action

To get an understanding of how Spring Boot Auto Configuration works internally, we can create Spring MVC application using Spring Boot. Follow    Creating a Web Application with Spring Boot to create an application using Spring Boot.Run your MVC application and look at the console. You might find a similar output in the console.


2017-12-21 20:53:34.429 INFO 51206 --- [ main] org.apache.catalina.core.StandardEngine : 
Starting Servlet Engine: Apache Tomcat/8.5.23 
2017-12-21 20:53:34.523 INFO 51206 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 
2017-12-21 20:53:34.524 INFO 51206 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1496 ms 
2017-12-21 20:53:34.749 INFO 51206 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 
2017-12-21 20:53:34.754 INFO 51206 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 
2017-12-21 20:53:34.755 INFO 51206 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 
2017-12-21 20:53:34.756 INFO 51206 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 
2017-12-21 20:53:34.756 INFO 51206 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]

 

In our sample application, we defined no dispatcher servlet or configured tomcat for the deployment, however, we can still find  Mapping servlet: 'dispatcherServlet' to [/] in the console, this is happening because we added spring-boot-starter-web in our application using POM.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

 

This single entry shows power and feature of auto-configuration. On adding this starter in our application, Spring Boot auto configuration understands that we are building an MVC application, and it added all required dependencies in our classpath for a Spring MVC web application.

This went one step ahead and automatically configured DispactherServlet, CharacterEncodingFilter, RequestContextFilter and even error page for us (We did no configuration for these). Spring Boot will add similar configurations based on the Spring Boot Starters added in the POM file.

 

3. Auto Configuration Details

Let’s check how auto configurations work internally, check what is added to the classpath using IDE (Eclipse or IntelliJ etc).

Here is the one from our sample application.

Spring Boot Auto Configuration
Spring Boot Auto Configuration

As seen in this above screenshot, Spring Boot automatically added dependencies for us to ensure we have ready-to-use application.

 

3.1 Spring.Factories

To enable auto-configuration for our application, we can either use @AutoConfiguration or @SpringBootApplication annotation in our application. Check spring.factories property file under /META-INF directory under spring-boot-autoconfigure.jar.

 

Spring-boot-autoconfigure
Spring-boot-autoconfigure

This file contains a list of the auto-configuration classes which will be automatically enabled by Spring Boot. Here is a sample output from spring.factories file.

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\

Here is how auto-configuration class look like

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(DispatcherServlet.class)
@AutoConfigureAfter(EmbeddedServletContainerAutoConfiguration.class)
public class DispatcherServletAutoConfiguration { 
//configuration code
}

We will be covering these annotations and other details in a separate post.

 

Summary

In this post, we covered the Spring Boot Auto Configuration feature. We covered some interesting features of Configurationation along with how this works internally. The interesting features of Spring Boot Auto Configuration are its ability of noninvasive. Define your own configuration to replace default auto-configuration.

If you are starting with Spring Boot, read our other articles on Spring Boot

Comments are closed.

Scroll to Top