π― 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