jav spring boot mastery

🎯 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

Similar Posts