Spring Autowiring: Spring Fills the Constructor, and When @Autowired Is Needed
PostService asks for an EmailNotifier in its constructor and Spring plugs in the matching bean. With one constructor that's all. Add a second, empty constructor and Spring picks it, the notifier stays null, and only @Autowired fixes the choice.
What you will learn
- Explain autowiring as Spring matching a constructor parameter to a bean by type
- Predict the order Spring creates beans in
- Show what goes wrong when a class has two constructors and no hint
- Use @Autowired to pick the constructor Spring should call
Before you start
- Spring Component Scanning
- Inversion of Control and Dependency Injection in Spring
PostService needs an EmailNotifier to send the email. It asks for one in its constructor, and Spring finds an EmailNotifier bean and passes it in. Asking for a part in the constructor and letting Spring fill it is called autowiring.
A plug fits its own socket
Think of the constructor parameter as a socket with a shape: the shape of an EmailNotifier. Spring has a plug of exactly that shape, the EmailNotifier bean, so it plugs it in. You don't pick the plug; Spring matches it by type.
One constructor: no annotation needed
package org.studyeasy.blog;
import org.springframework.stereotype.Component;
// Tells the readers about a new post.
@Component
public class EmailNotifier {
public EmailNotifier() {
System.out.println("EmailNotifier created");
}
public void send(String title) {
System.out.println("Email sent: " + title);
}
}
package org.studyeasy.blog;
import org.springframework.stereotype.Component;
// Publishes a post, then asks the notifier to tell the readers.
@Component
public class PostService {
private final EmailNotifier notifier;
// One constructor: Spring calls it and passes in the EmailNotifier bean.
// (@Autowired would mean the same thing here, so we leave it out.)
public PostService(EmailNotifier notifier) {
this.notifier = notifier;
System.out.println("PostService created");
}
public void publish(String title) {
System.out.println("Post published: " + title);
notifier.send(title);
}
}
BlogConfig is the same @Configuration + @ComponentScan class as in component scanning, and main starts Spring, gets the PostService and calls publish. Run it:
EmailNotifier created
PostService created
Post published: Hello, Spring
Email sent: Hello, Spring
Watch the first two lines. EmailNotifier's constructor needs nothing, so Spring can make it straight away. Then it makes PostService and passes in that notifier. PostService never says new. With only one constructor, Spring calls it anyway, so @Autowired would mean the same thing and is left out.
Now PostService has two constructors
Imagine adding a second, empty constructor, maybe for a quick test where no emails should go out. Now Spring has a question: which one do I call? Both can make a PostService. The rule: with no hint, Spring calls the empty constructor.
Without @Autowired on either constructor, the run goes like this:
EmailNotifier created
PostService created without a notifier
Post published: Hello, Spring
NullPointerException
Spring called the empty constructor, which sets notifier to null. The constructor with EmailNotifier was never called. Spring did make the EmailNotifier bean; it just never passed it in. So the post is published, then notifier.send crashes.
@Autowired picks the constructor
package org.studyeasy.blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
// Publishes a post, then asks the notifier to tell the readers.
@Component
public class PostService {
private final EmailNotifier notifier;
// A second constructor: no notifier, for a quick test without emails.
public PostService() {
this.notifier = null;
System.out.println("PostService created without a notifier");
}
// Two constructors: @Autowired tells Spring which one to call.
@Autowired
public PostService(EmailNotifier notifier) {
this.notifier = notifier;
System.out.println("PostService created with a notifier");
}
public void publish(String title) {
System.out.println("Post published: " + title);
notifier.send(title);
}
}
EmailNotifier created
PostService created with a notifier
Post published: Hello, Spring
Email sent: Hello, Spring
One line, @Autowired (from org.springframework.beans.factory.annotation), above the second constructor tells Spring to call that one. The empty constructor is still there for a quick test; Spring simply skips it now.
When do I write @Autowired?
| Constructors in the class | Spring calls | Write @Autowired? |
|---|---|---|
| one | that one | not needed |
| two or more | the empty one, unless told otherwise | yes, on the one Spring should use |
Common mistakes
- Adding a no-argument constructor to a component "just for tests" and forgetting
@Autowiredon the real one. The dependency stays null until the first call crashes. - Reading "Spring made the bean" as "Spring injected the bean". Creating a bean and passing it into a constructor are two separate steps.
- Sprinkling
@Autowiredon a single constructor and thinking it's required. It's harmless, but it isn't needed.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Why Spring in 2026: What the Container Does and What Spring Framework 7 Changed
Spring in one line: a container that creates your classes and hands each one what it needs. Plus the Spring Framework 7 baseline (JDK 17 to 25, Jakarta EE 11, Tomcat 11, JUnit 6), what got better than Spring 6, and a two-line version check.
A Plain Spring Project, No Boot: Your First Bean With spring-context
One dependency, one recipe, one getBean: a Maven project with spring-context 7.0.9, a Blog record, an AppConfig with @Bean, and a main that asks the container for the Blog. Then delete @Bean and meet NoSuchBeanDefinitionException.
Inversion of Control and Dependency Injection in Spring: One Program, Built Three Ways
A PostService needs a PostRepository. Build it three ways: the service calls new itself (tight coupling), main hands it in through the constructor (DI by hand), and the Spring container does the wiring from a @Configuration recipe.
Spring Component Scanning: @Component and @ComponentScan Instead of @Bean Methods
Instead of one @Bean method per class, put @Component on EmailNotifier and PostService and @ComponentScan on an empty BlogConfig. Spring walks the package, finds the stickers and builds the beans; main doesn't change.
