liveBook · Manning

Spring bean

As explained in Spring Start Here, Second Edition

An object instance created, configured, and managed by the Spring IoC container.

A Spring bean is an object instance added to the Spring context, also called the IoC container. Spring manages the instance and can connect it to capabilities such as dependency injection, transactions, and security. Not every object in an application needs to be a bean; add the objects Spring needs to manage. section 2.1 section 2.5

There are three main ways to add a bean to the context:

  • Use @Bean on a method in a configuration class. The method creates and configures the object; Spring adds the returned instance to the context. This approach also works for classes from the JDK or other dependencies. section 2.1 section 3.1
  • Mark an application class with a stereotype annotation such as @Component, and use @ComponentScan to tell Spring where to find it. Spring creates and adds the instance, reducing configuration code. section 2.2
  • Call registerBean() on the context for custom registration. A supplier provides the instance, and optional customizers can set bean characteristics. section 2.3

Create and retrieve a bean

In a configuration class, an @Bean method can create a Parrot, set its name, and return it. Create an AnnotationConfigApplicationContext using that configuration, then call context.getBean(Parrot.class) to retrieve the managed instance. section 2.1

Listing 2.6 Defining the @Bean method
@Configuration
public class ProjectConfig {
 
  @Bean   #A
  Parrot parrot() {
    var p = new Parrot();
    p.setName("Koko");   #B
    return p;   #C
  }
}

See how a configuration method creates and returns the Parrot bean. section 2.1

A context can contain several beans of the same type. In that case, requesting one by type alone is ambiguous. Retrieve a bean by its identifier, mark one @Primary as the default, or use @Qualifier to select a named bean for injection. section 2.1 section 4.5

Connect two beans

A Person bean can hold a reference to a Parrot bean so the person can use it. In a @Configuration class, the person() method can call the parrot() bean method and set the result. Spring controls the call: in this example, it returns the existing Parrot bean if available rather than creating a second one. section 3.1 section 3.1.1

Beans can also receive initialization logic after Spring creates them through a lifecycle callback such as @PostConstruct. section 2.5

How Spring Start Here, Second Edition covers it

How deep it goes
Explains registration, lookup, selection, and wiring step by step, including managed method calls while deferring interception details.
Examples
Uses Parrot and Person objects to demonstrate creation, retrieval, conditional registration, and wiring.
What it assumes
Introduces the Spring context from an initially framework-free Java project and progressively explains bean registration.
What you can do after
Apply it via Spring to register and retrieve beans, select among matching instances, and connect managed objects.
How it connects
Connects beans to the IoC container, dependency injection, lifecycle callbacks, and interfaces.

Also explained in

Compare how 5 books explain Spring bean →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.