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.
What you will learn
- Mark classes with @Component so Spring creates them
- Turn on scanning with @ComponentScan on an empty @Configuration class
- Follow what Spring does at startup when it scans a package
- Choose between listing beans with @Bean and letting Spring find them
Before you start
- Inversion of Control and Dependency Injection in Spring
- A Plain Spring Project, No Boot
With @Bean methods, the configuration class lists every class: one method for EmailNotifier, one for PostService. Fine for two classes, tedious for fifty. Component scanning flips it around. You put a small sticker, @Component, on each class, and the configuration just says "go and look".
Moving day: a sticker on the box
Picture an old house full of boxes. Some have a green "move" sticker: books, plates, toys. The old junk has none. The movers walk through the house and take only the boxes with a sticker. Nobody writes a list. The sticker is @Component, the house is the package, and the movers are Spring.
Inside BlogConfig
package org.studyeasy.blog;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
// No @Bean methods: Spring finds the @Component classes itself.
@Configuration
@ComponentScan
public class BlogConfig {
}
@Configuration still marks the setup class. @ComponentScan is new: with no arguments it tells Spring to look in this class's package and find every class with @Component. The class body is empty. No @Bean methods at all.
The two stickers
package org.studyeasy.blog;
import org.springframework.stereotype.Component;
// Tells the readers about a new post.
@Component
public class EmailNotifier {
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;
public PostService(EmailNotifier notifier) {
this.notifier = notifier;
}
public void publish(String title) {
System.out.println("Post published: " + title);
notifier.send(title);
}
}
The only new line in each class is @Component: "Spring, please make one of these." PostService's constructor still needs an EmailNotifier, and Spring hands it the one it found. Exactly how it matches that is the subject of autowiring.
main is the same as before
package org.studyeasy.blog;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class BlogApp {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(BlogConfig.class);
PostService service = context.getBean(PostService.class);
service.publish("Hello, Spring");
context.close();
}
}
Post published: Hello, Spring
Email sent: Hello, Spring
Same app, same output. The change is only in the other classes.
@Bean or @Component?
| @Bean methods | @Component + @ComponentScan | |
|---|---|---|
| Where the list lives | in the configuration class | on each class |
| Adding a class | add a method | add the sticker |
| Configuration class | one method per bean | empty |
Common mistakes
- Forgetting
@Componenton a class. Spring doesn't make it, and asking for it fails withNoSuchBeanDefinitionException. - Putting a component in a package outside the scanned one. A bare
@ComponentScanlooks in the configuration class's own package and its sub-packages. - Forgetting
@ComponentScanitself. The stickers are there, but nobody walks through the house.
Quick recap
@ComponentScan says go and look, @Component says make me, and getBean says give it to me.
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 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.
