liveBook · Manning

Chapter 2: The Spring context: Defining beans

What you will learn in chapter 2 of Spring Start Here, Second Edition

Introduces the Spring application context as the place where object instances must be added for Spring to manage them.

After this chapter you will be able to

Section by section

Introduction

Introduces the Spring application context as the place where object instances must be added for Spring to manage them.

Key terms: Spring context, application context, object instances

2.1 Using the @Bean annotation to create beans

This section shows how to add objects to the Spring context with @Bean methods, retrieve them, and resolve lookups when multiple beans share a type.

Key terms: bean, Spring context, spring-context, AnnotationConfigApplicationContext, Maven, @Configuration, @Bean, configuration class, bean name, bean identifier

2.2 Using stereotype annotations to create beans

This section shows how to create Spring beans with stereotype annotations, initialize them after creation, and decide when to use this approach instead of @Bean.

Key terms: stereotype annotation, @Component, @ComponentScan, configuration class, Spring context, stereotype annotations, @Bean, library, @PostConstruct, component

2.3 Programmatically creating beans

This section explains when to add beans programmatically and how to use ApplicationContext.registerBean() to register and configure them.

Key terms: programmatically adding beans, Spring context, @Bean, stereotype annotations, ApplicationContext, registerBean(), beanName, Supplier, BeanDefinitionCustomizer, varargs

2.4 Questions

This section provides questions for reviewing the chapter’s concepts, with answers in Appendix A.

2.5 Summary

Summarizes what Spring beans are, how they are added to the context, and two considerations when managing them.

Where this is used later

Concepts explained in this chapter

← Chapter 1: Spring in the real world Chapter 3: The Spring context: Wiring beans →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.