liveBook · Manning

@Bean

As explained in Spring Start Here, Second Edition

A Spring annotation that marks a method whose returned object is registered as a bean.

@Bean marks a method whose returned object becomes a bean in the Spring context. This makes the object available for Spring to manage and connect to other beans. Not every object in an application needs to be a bean. section 2.1

To create a bean explicitly, add a method to a class annotated with @Configuration, mark the method with @Bean, and return the configured object. Create the context using the configuration class; you can then retrieve the bean from it. 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
  }
}

The method creates a Parrot, sets its name, and returns it for Spring to manage. section 2.1

By default, the method name identifies the bean. Multiple @Bean methods can create beans of the same type, but requesting that type alone is then ambiguous. Retrieve a bean by name, or mark one @Primary to select it when requesting by type. section 2.1 section 2.5

Use @Bean when you want control over creating and configuring an object, particularly when its class comes from the JDK or another dependency. For many application classes, a stereotype such as @Component reduces boilerplate; @ComponentScan tells Spring where to find those classes. The approaches can be combined. section 2.5 section 2.2

Beans can also depend on one another. In a @Configuration class, one @Bean method can call another to establish a relationship. In the illustrated configuration, Spring returns the existing bean if it is already in the context, or creates it if needed, rather than necessarily creating a second instance. section 3.1.1

Listing 3.3 Making a link between the beans with a direct method call
@Configuration
public class ProjectConfig {
 
  @Bean
  public Parrot parrot() {
    Parrot p = new Parrot();
    p.setName("Koko");
    return p;
  }
 
  @Bean
  public Person person() {
    Person p = new Person();
    p.setName("Ella");
    p.setParrot(parrot());   #A
    return p;
  }
}

The person() method calls parrot() and assigns the resulting bean to the person. section 3.1.1

Alternatively, declare dependencies as parameters of a @Bean method. Spring supplies the dependencies, including beans referenced through abstraction types, and the method uses them to construct its result. section 4.2.1

How Spring Start Here, Second Edition covers it

How deep it goes
Explains creation, selection, and wiring with worked examples, while deferring further details of method interception.
Examples
Uses Parrot and Person beans, multiple same-type beans, and a comment service with repository and notification dependencies.
What it assumes
Starts with ordinary Java object creation and an empty Spring context before introducing configuration methods.
What you can do after
Apply it via Spring to configure, retrieve, select, and wire beans.
How it connects
Connects configuration classes, the Spring context, dependency injection, component scanning, and primary bean selection.

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.