TechnologyBeginner

Spring Boot: Bean, @Component, and Component Scanning!

TechnologySpring Boot: Bean, @Component, and Component Scanning!Beginner · 3 min read

**Spring Boot:Bean, @Component, and Component Scanning **

If you're learning Spring Boot, you've probably come across these terms:

Spring Bean
@Component
Component Scanning
Dependency Injection

But have you ever wondered how they actually work together?

Let's understand them with a simple real-world example. ?

1 What is a Spring Bean?

A Bean is simply a Java object that is created and managed by the Spring IoC (Inversion of Control) container.

In traditional Java, we create objects manually:

Java1 line
EmailService emailService = new EmailService();

But in Spring Boot, we can let Spring create and manage the object for us.

2 What is @Component?

@Component tells Spring that a class should be discovered and registered as a Bean.

Java7 lines
@Component
public class EmailService {

    public void sendEmail() {
        System.out.println("Email sent!");
    }
}

When Spring scans this class, it registers an EmailService Bean in its application context.

No need to manually create the object!

3 What is Component Scanning?

Component scanning is how Spring automatically discovers classes annotated with:

@Component — General-purpose components
@Service — Business logic
@Repository — Data access layer
@Controller — Web MVC controllers
@RestController — REST API controllers

Consider this project structure:

Code9 lines
com.example.demo

DemoApplication.java
controller
UserController.java
service
UserService.java
repository
UserRepository.java

Our main application class:

Java9 lines
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(
            DemoApplication.class, args
        );
    }
}

Here's the interesting part! ?

@SpringBootApplication includes @ComponentScan.

By default, Spring scans the package containing the main application class and all its subpackages.

It discovers eligible components and registers them as Beans.

4 How Does Dependency Injection Work?

Let's say our UserService needs EmailService.

Instead of creating the dependency manually, we use constructor injection.

Java14 lines
@Service
public class UserService {

    private final EmailService emailService;

    public UserService(EmailService emailService) {
        this.emailService = emailService;
    }

    public void registerUser() {
        System.out.println("User registered!");
        emailService.sendEmail();
    }
}

What happens behind the scenes?

  1. Spring scans the application packages.
  2. It discovers EmailService and UserService.
  3. It registers them as Spring Beans.
  4. It identifies that UserService requires EmailService.
  5. It injects the dependency automatically.

That's the power of Dependency Injection!

5 @Bean vs @Component — What's the Difference?

Both are used to register Spring Beans, but they serve different purposes.

@Component

Applied directly to a class and discovered through component scanning.

Java3 lines
@Component
public class EmailService {
}

@Bean

Applied to a method, usually inside a @Configuration class, to explicitly define how an object is created.

Java8 lines
@Configuration
public class AppConfig {

    @Bean
    public EmailService emailService() {
        return new EmailService();
    }
}

When should you use each?

  • Use @Component (or @Service, @Repository) for application classes managed automatically by Spring.
  • Use @Bean when you need explicit control over object creation or want to configure a third-party class.

Key Takeaways

Bean = An object managed by Spring.
@Component = Marks a class for automatic discovery.
Component Scanning = Finds and registers eligible classes.
Dependency Injection = Spring provides objects to classes that depend on them.
IoC Container = Manages Bean creation, dependencies, and lifecycle.

The big idea:

Instead of your application managing every object and its dependencies, Spring handles that responsibility for you.

This makes applications easier to maintain, test, and scale.

Question for Java developers:

Do you prefer constructor injection or field injection in your Spring Boot projects? Why?

Let's discuss in the comments! ?

#Java #SpringBoot #SpringFramework #DependencyInjection #SpringBeans #ComponentScanning #BackendDevelopment #JavaDeveloper #Microservices #SoftwareEngineering #Programming #LearnJava

S

Written by Simon Temesgen

Sharing practical knowledge with the MelkaTech community.

Join the discussion

Ask a question or share what you learned. Comments are reviewed before appearing.