0
0
Spring Bootframework~10 mins

@Autowired for dependency injection in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @Autowired for dependency injection
Start Application
Spring scans for @Autowired
Find dependency to inject
Create or find bean instance
Inject bean into target class
Use injected bean in code
Application runs with dependencies ready
Spring scans for @Autowired annotations, finds the needed beans, creates or retrieves them, and injects them into the target class before running the application.
Execution Sample
Spring Boot
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class Service {
  @Autowired
  private Repository repo;

  public void doWork() {
    repo.save();
  }
}
This code shows Spring injecting a Repository bean into Service using @Autowired, so Service can call repo.save() without manual setup.
Execution Table
StepActionEvaluationResult
1Spring starts and scans classesFind @Autowired on 'repo' fieldMarks 'repo' for injection
2Spring looks for Repository beanRepository bean found or createdRepository instance ready
3Spring injects Repository instance into Service.repoField 'repo' set with Repository instanceService.repo now references Repository bean
4Service.doWork() calledCalls repo.save()Repository.save() executes
5Application runs with dependencies injectedNo manual new Repository() neededDependency injection successful
💡 All dependencies injected, application runs with ready-to-use beans
Variable Tracker
VariableStartAfter InjectionAfter doWork() call
reponullRepository instanceRepository instance (used)
Key Moments - 3 Insights
Why is 'repo' null before injection but not after?
Before injection, Spring has not assigned the Repository instance, so 'repo' is null. After step 3 in execution_table, Spring injects the Repository bean, so 'repo' references the instance.
What happens if Spring cannot find a Repository bean to inject?
Spring will throw an error during startup because it cannot fulfill the @Autowired dependency, stopping the application from running.
Does @Autowired create a new instance every time?
No, by default Spring injects a singleton bean instance, so the same Repository object is reused unless configured otherwise.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'repo' after step 2?
Anull
BRepository instance
CNew Service instance
DUndefined
💡 Hint
Check variable_tracker column 'After Injection' and execution_table step 2 where bean is found but not yet injected
At which step does Spring inject the Repository bean into Service?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table step 3 where injection is described
If the Repository bean is missing, what will happen according to the key moments?
ASpring creates a new Repository automatically
BSpring throws an error and stops startup
CService runs with repo as null
DApplication runs but repo.save() does nothing
💡 Hint
Refer to key_moments about missing bean behavior
Concept Snapshot
@Autowired injects dependencies automatically.
Spring scans for @Autowired fields.
Finds or creates matching beans.
Injects bean instance into target.
No manual new needed.
Injection happens before use.
Full Transcript
When a Spring Boot application starts, it scans classes for the @Autowired annotation. When it finds a field marked with @Autowired, like 'repo' in the Service class, it looks for a matching bean to inject. If a Repository bean exists, Spring creates or retrieves it and injects it into the Service's 'repo' field. Before injection, 'repo' is null. After injection, 'repo' references the Repository instance. When Service calls doWork(), it uses the injected Repository to perform actions like save(). If Spring cannot find a required bean, it throws an error and stops the application from starting. This automatic wiring saves you from manually creating and passing dependencies.