Skip to content
Related Articles
Open in App
Not now

Related Articles

Spring Boot – Create a Custom Auto-Configuration

Improve Article
Save Article
Like Article
  • Last Updated : 08 Jul, 2022
Improve Article
Save Article
Like Article

Usually, we might be taking a maven project or grade project for Spring Boot related projects. We will be adding the dependencies in pom.xml (in case of a maven project). In a spring application, Spring Boot auto-configuration helps to automatically configure by checking with the jar dependencies that we have added. For example, though no beans are specifically given for beans related to the database because of the existence of database jar/database dependency in pom.xml, Spring Boot’s auto-configuration feature automatically configures it in the project.

@SpringBootApplication = @ComponentScan + @EnableAutoConfiguration + @Configuration

Hence the code that has @SpringBootApplication will take care of doing the auto-configuration automatically. No need to worry about using the annotation @EnableAutoConfiguration. 

spring-boot-starter-web dependency

If this dependency is provided, it looks for the Spring MVC (Model View Controller) in the classpath. It also auto-configures DispatcherServlet and additionally web jars that are required to do MVC functionally and a default error is all set.

spring-boot-starter-data-jpa dependency

Automatically it configures a data source and an Entity Manager. Let us see Spring Boot Auto Configuration via an example project

Example Project

Project Structure: 

Project Structure


This is the maven-driven project



<?xml version="1.0" encoding="UTF-8"?>
      <relativePath />
   <description>Demo project for Spring Boot</description>
      <!-- Need to specify the dependencies for spring boot project -->

Let us see the important files in the project


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// @SpringBootApplication=@ComponentScan+@EnableAutoConfiguration+@Configuration
public class IllustrationOfSpringBootAutoconfigurationExampleApplication {
    public static void main(String[] args) {, args);

Let us see the controller file


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
public class SampleControllerDemo {
    public String home() {
        return "homePage.jsp";


import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
// This table will be created and we can view the data
// as we are inserting the data in the sql
@Table(name = "geekuserdata")
public class GeekUser {
    @Id private int id;
    private String username;
    private String noOfPosts;
    private String proficiencies;
    public String getUsername() { return username; }
    public void setUsername(String username)
        this.username = username;
    public String getNoOfPosts() { return noOfPosts; }
    public void setNoOfPosts(String noOfPosts)
        this.noOfPosts = noOfPosts;
    public String getProficiencies()
        return proficiencies;
    public void setProficiencies(String proficiencies)
        this.proficiencies = proficiencies;
    public int getId() { return id; }
    public void setId(int id) { = id; }
    public String getUname() { return username; }
    public void setUname(String username)
        this.username = username;
    @Override public String toString()
        return "GeekUser [id=" + id + ", uname=" + username
            + ", noOfPosts = " + noOfPosts
            + ", proficiencies= " + proficiencies + "]";


insert into geekuserdata values(1,'Rachel','100','Java');
insert into geekuserdata values(2,'Monica','75','Python');
insert into geekuserdata values(3,'Phoebe','70','PHP');


server.port=8085 // That means the server port
spring.datasource.plateform=h2 //h2 is used
spring.datasource.url=jdbc:h2:mem:gfg // jdbc:h2:mem:gfg is going to get used DEBUG // With this we can see in output all the potential matches

Let us create a web page called homePage.jsp 



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html>
      <meta charset="ISO-8859-1">
      <title>Demo for Spring Boot Autoconfiguration</title>
      <div align="center">
         <h1>Add Geek Users</h1>
         <form action="<%=request.getContextPath()%>/addUser" method="post">
            <table style="with: 100%">
                  <td><input type="text" name="id" /></td>
                  <td>User Name</td>
                  <td><input type="text" name="userName" /></td>
            <input type="submit" value="Submit" />

Run the file ‘IllustrationOfSpringBootAutoconfigurationExampleApplication’ now. In the console, we can see this output

Output Console


In the, if we have enabled DEBUG

we can see these additional outputs in the console

Output Console


Output Console


On hitting http://localhost:8085/, we can see the below output, which is nothing but the given JSP page (homePage.jsp)



Similarly on hitting http://localhost:8085/h2-console/





In the console, if we carefully observe we can see that TransactionManagement, EntityManagerFactory, and DataSource are automatically configured, as shown in the following figure.

Output Console


So once the dependencies are given, Spring boot automatically configures everything.

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!