Skip to content

jandresqs/09-java-config-bean

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Custom Bean Configuration Using @Bean:


🏊 Spring Boot - Custom Bean Configuration Example

This project demonstrates how to manually configure beans in Spring using a configuration class with @Configuration and @Bean annotations, instead of using @Component and component scanning.


🎯 Learning Objective

By the end of this project, you will understand how to:

  • Define beans manually using a configuration class.
  • Use @Bean methods to control the creation and naming of beans.
  • Inject beans into other Spring-managed components using constructor injection.
  • Use @Qualifier to specify which bean should be injected when multiple implementations exist.

🧱 Project Structure

src/main/java/com/example/springcustomconfig/
│
├── config/
│   └── SportConfig.java        # Defines beans using @Bean methods
│
├── controller/
│   └── DemoController.java     # REST controller that injects the Coach bean
│
├── model/
│   ├── Coach.java              # Interface for different types of coaches
│   └── SwimCoach.java          # Implementation class (not annotated with @Component)
│
└── SpringCustomConfigApplication.java  # Main Spring Boot application


⚙️ Key Concepts

1. Manual Bean Definition (@Bean)

Instead of annotating SwimCoach with @Component, we define it in a configuration class:

@Configuration
public class SportConfig {

    @Bean
    public Coach swimCoach() {
        return new SwimCoach();
    }
}

✅ This tells Spring:

“When the application starts, create and manage a bean named swimCoach of type SwimCoach.”


2. Dependency Injection with @Qualifier

We use constructor injection to inject our custom bean into the controller:

@RestController
@RequestMapping("/")
public class DemoController {

    private Coach coach;

    @Autowired
    public DemoController(@Qualifier("swimCoach") Coach coach) {
        this.coach = coach;
    }

    @GetMapping("/dailyworkout")
    public String getDailyWorkout() {
        return coach.getDailyWorkout();
    }
}

The @Qualifier("swimCoach") ensures that the correct bean (the one defined in SportConfig) is injected.


3. The Bean Class (SwimCoach)

public class SwimCoach implements Coach {

    public SwimCoach() {
        System.out.println("In constructor: " + getClass().getSimpleName());
    }

    @Override
    public String getDailyWorkout() {
        return "Swim 1000 meters as a warm up";
    }
}

Notice that:

  • The class is not annotated with @Component.
  • It becomes a Spring bean only because it’s registered via SportConfig.

🚀 Running the Application

  1. Run the Spring Boot app:

    ./mvnw spring-boot:run
  2. Open your browser or use curl:

    http://localhost:8080/dailyworkout
    
  3. Expected Output:

    Swim 1000 meters as a warm up
    

In the console, you should also see:

In constructor: SwimCoach

🧠 Why Use Manual Bean Configuration?

Manual configuration gives you:

  • Fine-grained control over bean creation (e.g., calling constructors with arguments).
  • Flexibility to integrate external libraries or legacy classes that you can’t annotate.
  • The ability to customize bean names and behaviors.

In larger applications, combining @Component scanning and manual configuration is a common and powerful approach.


🧩 Related Concepts

  • @Component vs @Bean
  • @Configuration
  • @Autowired
  • @Qualifier
  • Dependency Injection in Spring

📚 Next Steps

To continue learning:

  1. Try adding another Coach implementation (e.g., TennisCoach).
  2. Define both beans in SportConfig.
  3. Use @Qualifier to switch between them in the controller.

Author: JAndresQS Topic: Spring Boot Fundamentals Concept: Manual Bean Configuration with @Bean Tools: Java 21+, Spring Boot 3.5.6, Maven


About

Custom bean configuration using a config class and @bean annotation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages