← Back to Blog

What is a Spring @Bean?

December 22, 2025

In the Spring Framework, a Bean is an object that is instantiated, assembled, and managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application.

Why do we need Beans?

Beans allow Spring to manage the lifecycle of your objects and handle dependency injection. This promotes loose coupling and makes your application easier to test and maintain.

Defining a Bean

The most common way to define a bean in modern Spring Boot applications is using the @Bean annotation within a @Configuration class.

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}

When the application starts, Spring detects the @Bean annotation and executes the method. The returned object is then registered in the application context.

Bean Scopes

Spring beans can have different scopes. The default scope is Singleton, meaning only one instance of the bean is created per Spring container.

  • Singleton: (Default) Single object instance per Spring IoC container.
  • Prototype: A new instance is created each time the bean is requested.
  • Request: A single instance per HTTP request (Web apps).