Topic 2: – 5 steps of Auto-Configuration in Spring Boot

Play Store Application link – Spring Boot in 13 steps – App on Google Play


Github Project link – https://github.com/kuldeep101990/AutoConfiguration_in_Spring_Boot

Step 1: Setting Up a Spring Boot Project

Using Spring Initializr

  1. Go to https://start.spring.io/.
  2. Fill in the details:
    • Project: Maven
    • Language: Java
    • Spring Boot: 3.x.x
    • Dependencies: Spring Web, Spring Data JPA, H2 Database
  3. Click Generate, and extract the downloaded ZIP file.

Import into IDE

  • Open your IDE (e.g., IntelliJ or Eclipse).
  • Import the extracted project as a Maven project.

Step 2: Writing a Simple REST API with Auto-Configured Components

Main Application Class

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
@RestController
class HelloController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, Auto-Configured World!";
    }
}

Run the application and access http://localhost:8080/hello to see the message.


Step 3: Auto-Configured DataSource Example

Spring Boot automatically configures a DataSource if spring-boot-starter-data-jpa and a database dependency (like H2) are present.

Configure application.properties

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update

Create an Entity and Repository

package com.example.demo;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Entity
class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    // Getters and Setters
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}
interface UserRepository extends JpaRepository<User, Long> {}
@RestController
class UserController {
    private final UserRepository userRepository;
    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
    @GetMapping("/users")
    public Iterable<User> getUsers() {
        return userRepository.findAll();
    }
}

Start the application, and you can access the H2 console at http://localhost:8080/h2-console. Use the credentials defined in application.properties.


Step 4: Excluding Auto-Configuration

You can exclude unnecessary auto-configuration classes using the exclude attribute in @SpringBootApplication.

Example: Exclude Security Auto-Configuration

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

This disables the default Spring Security configuration.


Step 5: Conditional Beans with Profiles

Define Profile-Specific Beans

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class AppConfig {
    @Bean
    @Profile("dev")
    public String devBean() {
        return "Development Bean";
    }
    @Bean
    @Profile("prod")
    public String prodBean() {
        return "Production Bean";
    }
}

Activate Profiles

Set the active profile in application.properties:

spring.profiles.active=dev

Restart the application, and the devBean will be loaded.


Conclusion

In this guide, we:

  1. Set up a Spring Boot project.
  2. Explored auto-configuration with examples like REST APIs and DataSource.
  3. Learned how to customize auto-configuration by excluding classes or using profiles.

8 comments

  1. I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

  2. Tôi đã giới thiệu trang này cho vài người bạn của mình vì trải nghiệm khá tốt, đăng ký nhanh và không gặp vấn đề gì khi sử dụng netmirror online TONY10-13

  3. **mitolyn reviews**

    Mitolyn is a carefully developed, plant-based formula created to help support metabolic efficiency and encourage healthy, lasting weight management.

  4. I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

Leave a Reply

Your email address will not be published. Required fields are marked *