codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

Spring Boot

9 / 62 topics
9Components and Beans in Spring Boot10Dependency Injection with Autowiring
Tutorials/Spring Boot/Components and Beans in Spring Boot
🍃Spring Boot

Components and Beans in Spring Boot

Updated 2026-05-15
10 min read

Components and Beans in Spring Boot

Introduction

Spring Boot is a powerful framework that simplifies the development of Java applications. At its core, Spring Boot relies heavily on the concepts of components and beans to manage application objects and their dependencies. Understanding these concepts is crucial for building robust and maintainable applications with Spring Boot.

In this tutorial, we will delve into what components and beans are in Spring Boot, how they interact, and how you can use them effectively in your projects.

Concept

What is a Bean?

A bean in the context of Spring Boot is an object that is managed by the Spring IoC (Inversion of Control) container. The IoC container is responsible for creating, configuring, and managing these beans throughout the application's lifecycle. Beans can be any Java object, but they are typically used to represent services, controllers, repositories, or other components in a Spring Boot application.

What is a Component?

A component is a special type of bean that is annotated with @Component, or one of its specialized annotations like @Service, @Repository, or @Controller. These annotations tell the Spring container to treat the class as a component and manage it as a bean. By using these annotations, you can easily define beans without having to manually register them in configuration classes.

The Relationship Between Components and Beans

All components are beans, but not all beans are components. When you annotate a class with @Component or its specialized variants, Spring automatically registers the class as a bean. This means that the Spring container will create an instance of this class when needed and manage it throughout the application's lifecycle.

Examples

Let's explore these concepts through some practical examples.

Creating a Simple Bean

First, let's create a simple Java class and annotate it with @Component to make it a bean.

Java
1import org.springframework.stereotype.Component;
2
3@Component
4public class MyBean {
5 public void sayHello() {
6 System.out.println("Hello from MyBean!");
7 }
8}

Using the Bean

To use this bean, you need to create a Spring Boot application and autowire the bean into another component.

Java
1import org.springframework.beans.factory.annotation.Autowired;
2import org.springframework.boot.SpringApplication;
3import org.springframework.boot.autoconfigure.SpringBootApplication;
4
5@SpringBootApplication
6public class MySpringBootApplication {
7
8 @Autowired
9 private MyBean myBean;
10
11 public static void main(String[] args) {
12 SpringApplication.run(MySpringBootApplication.class, args);
13 }
14
15 public void run() {
16 myBean.sayHello();
17 }
18}

Running the Application

To run the application, you can use the following command:

Terminal
$ ./mvnw spring-boot:run

You should see the output:

Output
Hello from MyBean!

What's Next?

In this tutorial, we covered the basics of components and beans in Spring Boot. Understanding these concepts is essential for building applications with Spring Boot.

Next, you can explore Dependency Injection with Autowiring to learn how to manage dependencies between beans more effectively. This will help you build more modular and maintainable applications.

Stay tuned for more tutorials on Spring Boot!


PreviousSpring ProfilesNext Dependency Injection with Autowiring

Recommended Gear

Spring ProfilesDependency Injection with Autowiring