
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
- Go to https://start.spring.io/.
- Fill in the details:
- Project: Maven
- Language: Java
- Spring Boot: 3.x.x
- Dependencies: Spring Web, Spring Data JPA, H2 Database
- 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:
- Set up a Spring Boot project.
- Explored auto-configuration with examples like REST APIs and DataSource.
- Learned how to customize auto-configuration by excluding classes or using profiles.