In the Spring Framework, a Bean is the foundational building block of an application. At its most basic level, a Bean is an object that is instantiated, assembled, and otherwise managed by a Spring Inversion of Control (IoC) container. Unlike standard Java objects that you manually create using the new keyword, Beans are lifecycle-managed entities that exist within the context of the framework, allowing for automated dependency management and simplified configuration.

When you develop a Spring-based application, you define the objects (Beans) that make up your business logic, data access, and web layers. The Spring container then takes these definitions and creates the actual object instances, injecting any required dependencies along the way. This process transforms a collection of separate classes into a cohesive, functional system.

The Core Relationship Between Beans and the IoC Container

To understand Beans, it is essential to understand the environment in which they live: the Inversion of Control (IoC) container. In traditional Java programming, a class is responsible for creating its own dependencies. For example, a UserService might create a UserRepository instance within its constructor.

In the Spring ecosystem, this responsibility is inverted. The UserService no longer creates the UserRepository; instead, the Spring container creates both and "injects" the repository into the service. The objects that the container manages in this fashion are called Beans.

BeanFactory vs ApplicationContext

The Spring Framework provides two main types of IoC containers:

  1. BeanFactory: This is the root interface for accessing the Spring bean container. It provides the basic configuration mechanism and is suitable for environments where memory consumption is critical, such as mobile devices or small applets. It uses lazy loading, meaning Beans are only instantiated when requested.
  2. ApplicationContext: A sub-interface of BeanFactory, this is the container used in almost all enterprise applications. It adds more sophisticated features like internationalization support, event publication, and automatic registration of BeanPostProcessors. In modern development, ApplicationContext is the standard, as it provides a more robust set of tools for managing the Bean lifecycle.

How to Define Beans in Spring Framework

There are three primary ways to inform the Spring container about the Beans it needs to manage. Over the years, the industry has shifted from heavy XML configurations to more concise, code-centric approaches.

1. Annotation-Based Configuration

This is the modern standard for Spring development. By placing specific annotations on your classes, you signal to Spring's component scanning mechanism that these classes should be treated as Beans.

  • @Component: The generic stereotype for any Spring-managed component.
  • @Service: Indicates that the class holds business logic.
  • @Repository: Used for the data access layer; it also enables automatic persistence exception translation.
  • @Controller: Marks a class as a web controller in Spring MVC.

For example, a simple service bean looks like this: