Skip to content

Spring Framework demo showcasing automated bean detection using @componentscan and @component. Features dependency injection via @Autowired, handling bean priority with @primary, and explicit selection with @qualifier.

License

Notifications You must be signed in to change notification settings

harman-04/spring-dependency-injection-automated-component-scanning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Automated Component Scanning

This project demonstrates the Annotation-Based Configuration method using Spring's Component Scanning capabilities. This is the most modern and common way to handle Dependency Injection in Spring Boot.

Key Features

  • Auto-Detection: Using @ComponentScan to find beans automatically.
  • Stereotype Annotations: Marking classes with @Component to register them in the IoC container.
  • Auto-wiring: Using @Autowired to inject dependencies without manual setters or constructors.
  • Ambiguity Handling:
    • @Primary: Defines a default bean implementation.
    • @Qualifier: Allows specific selection between multiple implementations (Jio vs Airtel).

Project Evolution

This repo represents the 3rd stage of Spring learning:

  1. XML-Based (Manual)
  2. Java-Based (Semi-Manual)
  3. Component Scanning (Fully Automated) - Now are here

How it Works

Spring scans the package org.spring.dependencyinjection.automatedcomponent.scanmethod.

  1. It finds Jio and Airtel and creates beans named jioBean and airtelBean.
  2. It finds the Mobile class and sees it is also a @Component.
  3. It "wires" the Sim dependency into Mobile using the ID provided in @Qualifier.

Sample Output

Jio Calling
Jio Data

Comparison of @Primary vs @Qualifier

Here is why your output will specifically show "Jio":

Feature What it does Your Code
@Primary The "Default" choice. Set on Airtel.
@Qualifier The "Specific" choice. Set on Mobile (points to Jio).
Winner Qualifier always wins. Jio is printed.

About

Spring Framework demo showcasing automated bean detection using @componentscan and @component. Features dependency injection via @Autowired, handling bean priority with @primary, and explicit selection with @qualifier.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages