Spring_ What is @Autowired_
Spring_ What is @Autowired_
com/in/sahaidachnyi
TARAS SAHAIDACHNYI
WHAT IS @AUTOWIRED?
S P R I N G I N T E R I V E W Q U E S T I O N S
linkedin.com/in/sahaidachnyi
TARAS SAHAIDACHNYI
ENABLING
@AUTOWIRED IN SPRING
EXAMPLE:
CONSTRUCTOR INJECTION
Pros:
Ensures immutability since dependencies
are final and cannot be changed after the
object is created.
Guarantees that dependencies are not null
during object creation.
Best practice for dependency injection.
Cons:
Requires a constructor for every
dependency.
linkedin.com/in/sahaidachnyi
TARAS SAHAIDACHNYI
CONSTRUCTOR INJECTION
Example:
linkedin.com/in/sahaidachnyi
TARAS SAHAIDACHNYI
SETTER INJECTION
Pros:
Easier to test by providing mock
dependencies via setter methods.
Supports optional dependencies.
Cons:
Dependencies can be modified after the
object is constructed, which might lead to
bugs.
linkedin.com/in/sahaidachnyi
TARAS SAHAIDACHNYI
SETTER INJECTION
Example:
linkedin.com/in/sahaidachnyi
TARAS SAHAIDACHNYI
FIELD INJECTION
Pros:
Quick and easy to implement.
Cons:
Harder to test because the dependency is
private.
Does not support immutability since the
dependency is directly injected into the field.
linkedin.com/in/sahaidachnyi
TARAS SAHAIDACHNYI
FIELD INJECTION
Example:
linkedin.com/in/sahaidachnyi
TARAS SAHAIDACHNYI
Example:
linkedin.com/in/sahaidachnyi
TARAS SAHAIDACHNYI
OPTIONAL DEPENDENCIES
@AUTOWIRED
AND CIRCULAR DEPENDENCIES
@AUTOWIRED
AND CIRCULAR DEPENDENCIES
Solutions:
BEST PRACTICES
3. Null dependency
Cause: The bean is not being scanned or is not
part of the Spring context.
Solution: Ensure proper component scanning
and configuration.
linkedin.com/in/sahaidachnyi
TARAS SAHAIDACHNYI
Thank you
for your attention!
linkedin.com/in/sahaidachnyi
TARAS SAHAIDACHNYI