Overview

2 The Spring context: Defining beans

In this chapter, the Spring context is presented as the central place where Spring keeps the object instances it is meant to manage. Those managed instances are called beans, and the key idea is that not every object in an application must be placed in the context, only the ones Spring should know about so it can apply its features to them. The chapter builds this idea gradually with simple examples, showing that once an object is added to the context, Spring can create, manage, and provide it when needed.

The chapter first explains the declarative approach for defining beans with a configuration class and the @Bean annotation. A configuration class marked with @Configuration tells Spring how to set up the context, and each @Bean method returns an object that Spring registers under the method’s name. This approach gives full control over object creation and allows multiple beans of the same type, with bean names used to distinguish them when needed. The chapter also notes that a bean can be marked as primary so Spring uses it by default when several candidates of the same type exist.

Next, the chapter introduces stereotype annotations, especially @Component, as a shorter way to create beans for classes owned by the application. Instead of writing factory methods, Spring scans specified packages with @ComponentScan, creates the annotated classes automatically, and adds them to the context. This approach is simpler and less verbose, but it offers less direct control over instantiation and is generally limited to classes you can annotate yourself. Finally, the chapter presents programmatic bean registration with registerBean(), which is useful when bean creation must depend on runtime conditions or custom logic, and it closes by comparing the three approaches and emphasizing that each has its place depending on the scenario.

To start, we create an object instance and the empty Spring context.
You created the Spring context instance and a Parrot instance. Now, you want to create a Parrot instance inside the Spring context to make Spring aware of this instance.
Steps for adding the bean to the context using the @Bean annotation. By creating the instance in the Spring context, you make the framework aware of the object, enabling it to manage the instance.
You can add any object to the Spring context to make Spring aware of it.
You can add more beans of the same type to the Spring context by using multiple methods annotated with @Bean. Each instance will have a unique identifier. To refer to them afterward, you’ll need to use the beans’ identifiers.
When using stereotype annotations, consider two steps. First, use the stereotype annotation (@Component) to annotate the class for which you want Spring to add a bean to its context. Second, use the @ComponentScan annotation to tell Spring where to look for classes annotated with stereotype annotations.
Using the registerBean() method to add specific object instances to the Spring context
Calling the registerBean() method to add a bean to the Spring context programmatically

Summary

  • The Spring context (IoC container) manages the objects integrated with Spring features such as dependency injection, transactions, security, and testing. These managed objects are called beans.
  • Beans can be added to the Spring context in three main ways: using @Bean, using stereotype annotations such as @Component, or programmatically with registerBean().
  • The @Bean approach offers full control over object creation and is useful for third-party classes or advanced configurations. Stereotype annotations are preferred for most application classes because they reduce boilerplate code.
  • When multiple beans of the same type exist, Spring requires additional information, such as a bean name or the @Primary annotation, to determine which bean to use.
  • Bean lifecycle callbacks such as @PostConstruct allow you to customize initialization logic after Spring creates the object.
  • Defining and managing beans correctly is fundamental because higher-level Spring features rely on the Spring context and its managed objects.

FAQ

What is the Spring context?The Spring context, also called the application context, is the part of the application where Spring stores and manages object instances that it knows about.
What is a bean?A bean is an object instance that has been added to the Spring context and is managed by Spring.
Do all objects in an application need to be beans?No. Only the objects that Spring needs to manage should be added to the context.
Why do we add objects to the Spring context?We add objects to the Spring context so Spring can manage them and connect framework capabilities to the application logic.
What are the three main ways to add beans to the Spring context?You can add beans using the @Bean annotation, using stereotype annotations such as @Component, or programmatically with registerBean().
What is a configuration class?A configuration class is a special Spring class, usually annotated with @Configuration, used to define Spring-related settings such as bean creation.
How do you create a bean using the @Bean annotation?Define a method inside a @Configuration class, annotate that method with @Bean, and return the object instance you want Spring to add to the context.
What is the default name of a bean created with @Bean?By default, the bean name is the same as the method name annotated with @Bean.
Can you add more than one bean of the same type?Yes. You can define multiple @Bean methods that return the same type, and each bean gets its own name.
What happens if Spring finds multiple beans of the same type and you request one by type only?Spring throws a NoUniqueBeanDefinitionException because it cannot decide which bean you mean.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Spring Start Here, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Spring Start Here, Second Edition ebook for free