Mastering @Autowired in Spring Boot: Dependency Injection Made Simple

Anil R
2 min readDec 6, 2024

--

@Autowired is a key annotation in Spring Framework, specifically in Spring Boot, used for dependency injection. Dependency injection allows the Spring framework to automatically resolve and inject the required dependencies into your classes, eliminating the need for explicit instantiation.

Key Features of @Autowired:

  • Automatic Dependency Injection:
    It tells Spring to look for a bean of the required type and inject it where it’s applied.
  • ByType Injection:
    Spring resolves the dependency based on the type of the bean. If there are multiple beans of the same type, you may need to use @Qualifier to specify which bean to inject.
  • Placement:
    You can use @Autowired on:
  • Fields (property injection)
  • Constructors (constructor injection)
  • Setters (setter injection)
  • Optional Dependencies:
    By default, Spring will throw an error if a dependency cannot be found. You can use the required attribute to make it optional:
@Autowired(required = false)
private SomeService someService;

Examples of Usage:

Field Injection:

@Component
public class UserService {
@Autowired
private ProfileService profileService;
}

Setter Injection:

@Component
public class UserService {
private ProfileService profileService;

@Autowired
public void setProfileService(ProfileService profileService) {
this.profileService = profileService;
}
}

Constructor Injection (recommended for mandatory dependencies):

@Component
public class UserService {
private final ProfileService profileService;

@Autowired
public UserService(ProfileService profileService) {
this.profileService = profileService;
}
}

Best Practices:

  1. Constructor Injection is preferred for mandatory dependencies as it ensures the dependencies are available when the object is created.
  2. For multiple beans of the same type, use @Qualifier to specify the exact bean:
@Autowired
@Qualifier("profileService")
private ProfileService profileService;

3. Avoid field injection in production code as it is harder to test and mock.

Common Errors:

  1. No qualifying bean of type found:
  • Ensure the required bean is annotated with @Component, @Service, @Repository, or a similar stereotype annotation, or is defined in a @Bean method within a @Configuration class.
  • Ensure component scanning is enabled.

2. Multiple beans of the same type:

  • Use @Qualifier to resolve ambiguity.

3. Circular dependency error

  • Refactor the design to eliminate the circular dependency.
  • Use @Lazy to delay the injection of a dependency until it’s needed

4. Bean not eligible for autowiring (due to scope or context issues)

  • Ensure the bean is in the same Spring context (e.g., @Component or manually registered in the correct @Configuration class).
  • If using multiple contexts, ensure proper context configuration.

5. Using @Autowired with final fields (Field Injection)

  • Use constructor injection for final fields.

6. Optional dependency injection fails

  • Use @Autowired(required = false) for optional dependencies

--

--

Anil R
Anil R

Written by Anil R

Full Stack Developer with 15 years experience.

No responses yet