Spring Boot Starters, Auto-Configuration, and Dependency Injection[Java Spring Boot Mastery Series – Part 2]
🎯 Objective
This part explores how Spring Boot simplifies setup using Starters, the magic behind Auto-Configuration, and how Dependency Injection helps manage object lifecycle.
🚀 Spring Boot Starters
Spring Boot starters are a set of convenient dependency descriptors you can include in your application.
Instead of manually specifying each dependency, you include a starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
🔍 Explanation
- Includes: Spring MVC, embedded Tomcat, Jackson, logging, etc.
- You don’t need to separately declare all the dependencies individually.
📦 Common Starters:
spring-boot-starter-web: RESTful web apps.spring-boot-starter-data-jpa: ORM and database access.spring-boot-starter-security: Security framework.spring-boot-starter-test: Testing with JUnit, Mockito, etc.spring-boot-starter-validation: Java Bean validation (Hibernate Validator).
🔄 Auto-Configuration
Spring Boot’s biggest power is Auto-Configuration:
- It automatically configures Spring Beans based on the dependencies on the classpath.
✅ Example
If spring-boot-starter-data-jpa and a DataSource (like MySQL) are in the classpath:
- Spring Boot auto-configures
EntityManagerFactory,DataSource,TransactionManager, etc.
Disable Auto-Configuration
You can exclude it if needed:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
🛠️ Custom Configuration Class
@Configuration
public class CustomConfig {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
💉 Dependency Injection (DI)
Dependency Injection is a design pattern where one object supplies the dependencies of another object.
Spring Boot supports 3 injection types:
1. Constructor Injection ✅ Recommended
@Service
public class MyService {
private final MyRepository repo;
public MyService(MyRepository repo) {
this.repo = repo;
}
}
2. Field Injection ❌ Not recommended (hard to test)
@Autowired
private MyRepository repo;
3. Setter Injection
private MyRepository repo;
@Autowired
public void setRepo(MyRepository repo) {
this.repo = repo;
}
🔍 DI Annotations:
@Autowired: Injects dependencies automatically.@Component: Marks a class as a Spring-managed component.@Service,@Repository,@Controller: Specializations of@Component
🧪 Simple Demo: DI + Auto-Configuration
➕ Step 1: Add a simple service
@Service
public class GreetingService {
public String greet(String name) {
return "Hello, " + name;
}
}
➕ Step 2: Inject into controller
@RestController
@RequestMapping("/api/greet")
public class GreetingController {
private final GreetingService greetingService;
public GreetingController(GreetingService greetingService) {
this.greetingService = greetingService;
}
@GetMapping("/{name}")
public String greet(@PathVariable String name) {
return greetingService.greet(name);
}
}
▶️ Access:
GET http://localhost:8080/api/greet/Ved
Response: Hello, Ved
✅ You now understand the core Spring Boot architecture:
- Starters simplify dependencies.
- Auto-configuration reduces boilerplate.
- Dependency Injection makes your code modular and testable.
➡️ Next Up: Part 3 – Building REST APIs with Spring MVC
