0
0
Spring Bootframework~5 mins

@Autowired for dependency injection in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the @Autowired annotation do in Spring Boot?

@Autowired tells Spring to automatically provide the needed object (dependency) to a class. It helps connect parts of your app without manual setup.

Click to reveal answer
beginner
Where can you use @Autowired in a Spring Boot class?

You can use @Autowired on fields, constructors, or setter methods to inject dependencies automatically.

Click to reveal answer
intermediate
Why is constructor injection with @Autowired preferred over field injection?

Constructor injection makes dependencies clear and helps with testing. It also ensures the object is fully ready when created.

Click to reveal answer
intermediate
What happens if Spring finds multiple beans of the same type when using @Autowired?

Spring throws an error because it doesn't know which bean to inject. You can fix this by using @Qualifier to specify the bean.

Click to reveal answer
intermediate
How can you make a dependency optional with @Autowired?

You can set required = false in @Autowired(required = false) so Spring won't fail if the bean is missing.

Click to reveal answer
What is the main purpose of @Autowired in Spring Boot?
ATo create new beans manually
BTo automatically inject dependencies
CTo configure database connections
DTo handle HTTP requests
Which of these is NOT a valid place to use @Autowired?
AOn a constructor
BOn a setter method
COn a local variable inside a method
DOn a field
If multiple beans of the same type exist, how can you specify which one to inject?
AUse <code>@Qualifier</code>
BUse <code>@Component</code>
CUse <code>@Service</code>
DUse <code>@Repository</code>
What does setting @Autowired(required = false) do?
ADisables dependency injection
BForces Spring to always inject the bean
CCreates a new bean instance
DMakes the dependency optional
Why is constructor injection recommended over field injection?
AIt makes dependencies explicit and helps testing
BIt requires less code
CIt allows private fields to be injected
DIt disables dependency injection
Explain how @Autowired works for dependency injection in Spring Boot and where you can use it.
Think about how Spring connects parts of your app without manual setup.
You got /3 concepts.
    Describe what happens if Spring finds multiple beans of the same type when using @Autowired and how to resolve it.
    Imagine Spring is confused about which object to give you.
    You got /3 concepts.