Mastering Spring @Autowired: 9 Powerful Injection Techniques

This article explains how Spring's @Autowired annotation works across constructors, methods, fields, arrays, collections, maps, optional dependencies, and special beans, providing practical code examples and important notes on required flags, ordering, and usage restrictions.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering Spring @Autowired: 9 Powerful Injection Techniques

Feature 1

The @Autowired annotation first tries injection by type; if multiple candidates exist it falls back to by‑name, and if no matching name is found the container throws an error. Combine it with @Qualifier to narrow the bean selection.

Since Spring Framework 4.3, if a bean defines only a single constructor, @Autowired on that constructor is optional. If multiple constructors are present and no primary/default constructor exists, at least one constructor must be annotated with @Autowired to tell the container which one to use.
public class SimpleMovieLister {
  private MovieFinder movieFinder;

  @Autowired
  public void setMovieFinder(MovieFinder movieFinder) {
    this.movieFinder = movieFinder;
  }
}

Feature 2

@Autowired can be applied to a method with any name and multiple parameters.

public class MovieRecommender {
  private MovieCatalog movieCatalog;
  private CustomerPreferenceDao customerPreferenceDao;

  @Autowired
  public void prepare(MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
    this.movieCatalog = movieCatalog;
    this.customerPreferenceDao = customerPreferenceDao;
  }
}

Feature 3

@Autowired may be used on fields and can be mixed with constructor injection.

public class MovieRecommender {
  private final CustomerPreferenceDao customerPreferenceDao;

  @Autowired
  private MovieCatalog movieCatalog;

  @Autowired
  public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
    this.customerPreferenceDao = customerPreferenceDao;
  }
}

Feature 4

You can inject an array of beans by annotating a field or method with @Autowired.

public class MovieRecommender {
  @Autowired
  private MovieCatalog[] movieCatalogs;
}

Feature 5

You can inject a collection (e.g., Set) of beans using @Autowired on a field or setter method.

public class MovieRecommender {
  private Set<MovieCatalog> movieCatalogs;

  @Autowired
  public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
    this.movieCatalogs = movieCatalogs;
  }
}

Note: To preserve order when injecting a List or array, implement the Ordered interface, use @Order, or the standard @Priority annotation. A Set does not guarantee order.

Feature 6

@Autowired can also inject a Map where the key is the bean name.

public class MovieRecommender {
  private Map<String, MovieCatalog> movieCatalogs;

  @Autowired
  public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
    this.movieCatalogs = movieCatalogs;
  }
}

Note: For declared arrays, collections, or maps, at least one matching bean must exist, otherwise an error occurs.

By default, @Autowired marks the dependency as required; the container will fail if the bean is missing. You can change this behavior:

public class SimpleMovieLister {
  private MovieFinder movieFinder;

  @Autowired(required = false)
  public void setMovieFinder(MovieFinder movieFinder) {
    this.movieFinder = movieFinder;
  }
}

If no suitable MovieFinder bean is present, the setter will not be invoked. When using constructor injection, setting required = false does not prevent a failure if the bean is missing.

Feature 7

Java 8's java.util.Optional can represent a non‑mandatory dependency.

public class SimpleMovieLister {
  @Autowired
  public void setMovieFinder(Optional<MovieFinder> movieFinder) {
    // optional handling
  }
}

Feature 8

The @Nullable annotation can be used to declare an optional dependency.

@Autowired
public void setMovieFinder(@Nullable MovieFinder movieFinder) {
  System.out.println("-----movie");
  this.movieFinder = movieFinder;
}

Feature 9

@Autowired can inject several special Spring infrastructure beans such as BeanFactory, ApplicationContext, Environment, ResourceLoader, ApplicationEventPublisher, and MessageSource.

@Autowired
private ApplicationContext context;
The @Autowired, @Inject, @Value, and @Resource annotations are processed by Spring's BeanPostProcessor implementations. They cannot be used inside custom BeanPostProcessor or BeanFactoryPostProcessor classes; those must obtain beans via XML configuration or @Bean methods.

End of article.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend Developmentspringdependency-injectionCode ExamplesAutowiredSpring Framework
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.