Chapter 2: The Spring context: Defining beans
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
- Purpose of the Spring context. Explain what the Spring context is and why an application's objects must be added to it for Spring to manage them.
- Beans in the Spring context. Explain what makes an object a bean and why an application adds selected objects to the Spring context.
- Bean configuration sequence. Trace how a @Configuration class and a @Bean method put a returned object into the Spring context.
- Bean names and lookup ambiguity. Distinguish lookup by type from lookup by bean name when the context contains multiple objects of the same type.
- Primary bean default choice. Explain how @Primary changes a lookup when several beans have the same type and no name is specified.
- Component creation and scanning. Explain how @Component and @ComponentScan work together to add a bean to the Spring context. Describe where each annotation goes and why @Component alone is insufficient in this example.
- Post creation bean initialization. Explain why a component's fields are not automatically customized and how @PostConstruct can run initialization instructions after Spring creates the bean.
- Reasons for programmatic registration. Explain why an application might add beans directly to the Spring context rather than rely on @Bean or stereotype annotations, using the green-parrot example.
- RegisterBean call and inputs. Describe calling registerBean() on an ApplicationContext and what its name, class, and Supplier inputs contribute to the registered bean.
Section by section
Introduction
Introduces the Spring application context as the place where object instances must be added for Spring to manage them.
- Purpose of the Spring context: Explain what the Spring context is and why an application's objects must be added to it for Spring to manage them.
Why it matters: This explains why creating a Java object alone does not make Spring manage it.
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.
- Beans in the Spring context: Explain what makes an object a bean and why an application adds selected objects to the Spring context.
Why it matters: This distinction explains why creating an ordinary Java object is not the same as making it available for Spring to manage. - Minimal Spring context setup: Describe the dependency and context implementation used to start the example, and explain why the example adds only that dependency.
Why it matters: These are the prerequisites for trying the section’s @Bean example without adding unrelated Spring modules. - Bean configuration sequence: Trace how a @Configuration class and a @Bean method put a returned object into the Spring context.
Why it matters: This is the section’s central method for making an object available for Spring to manage. - Bean names and lookup ambiguity: Distinguish lookup by type from lookup by bean name when the context contains multiple objects of the same type.
Why it matters: Bean identifiers let you retrieve the intended instance rather than getting an ambiguity error. - Primary bean default choice: Explain how @Primary changes a lookup when several beans have the same type and no name is specified.
Why it matters: A primary bean offers another way to resolve the same-type ambiguity without naming a bean at the lookup point.
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.
- Component creation and scanning: Explain how @Component and @ComponentScan work together to add a bean to the Spring context. Describe where each annotation goes and why @Component alone is insufficient in this example.
Why it matters: Both annotations are needed for Spring to find and create the example bean. - Stereotype and Bean tradeoff: Distinguish when this section favors stereotype annotations and when it favors @Bean.
Why it matters: The shorter approach is not suitable for every class you need to add to the context. - Post creation bean initialization: Explain why a component's fields are not automatically customized and how @PostConstruct can run initialization instructions after Spring creates the bean.
Why it matters: A component bean can exist in the context without yet having the values your application wants it to hold. - PreDestroy cleanup caution: Describe when an @PreDestroy method is called and why the author cautions against relying on it for sensitive cleanup.
Why it matters: Critical cleanup should not depend on a callback that might not run.
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.
- Reasons for programmatic registration: Explain why an application might add beans directly to the Spring context rather than rely on @Bean or stereotype annotations, using the green-parrot example.
Why it matters: This explains when direct registration is useful instead of annotation-based bean definitions. - RegisterBean call and inputs: Describe calling registerBean() on an ApplicationContext and what its name, class, and Supplier inputs contribute to the registered bean.
Why it matters: These inputs connect the bean’s identity and type with the actual object added to the context. - Optional bean characteristic customization: Explain the optional final registerBean() argument and how it can make a registered bean primary.
Why it matters: Registration can specify not just which instance enters the context, but a characteristic that affects Spring’s default selection.
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
- Deciding which objects should be part of the Spring context (chapter 4) · Deciding which objects should be part of the Spring context relies on this distinction.
- (chapter intro) (chapter 3) · Introduces methods used to create beans.
- Implementing relationships among beans defined in a configuration file (chapter 3) · Assumes that @Bean methods can add objects to the Spring context.
- Choosing from multiple beans in the Spring context (chapter 3) · Choosing among multiple beans relies on understanding the distinction between type and bean identity.
- Focusing on object responsibilities with stereotype annotations (chapter 4) · Uses stereotype annotations when defining objects by their responsibilities.
- Using @Autowired to inject the values through the class fields (chapter 3) · Uses stereotype annotations to create beans.
- (chapter intro) (chapter 5) · Introduces further discussion of bean scopes and life cycle.
Concepts explained in this chapter
- Spring beanAn object instance created, configured, and managed by the Spring IoC container.
- @BeanA Spring annotation that marks a method whose returned object is registered as a bean.
- configuration classA configuration class is a Spring class annotated with @Configuration that declares instructions for configuring the Spring context.
- primary beanA primary bean is a Spring bean designated as the default candidate when multiple beans of the same type are available.
- spring-contextThe spring-context module is a Spring Framework module that provides the application context and related infrastructure.
- primary annotationThe @Primary annotation marks a Spring bean as the preferred candidate when multiple beans match a dependency.
- stereotype annotationA stereotype annotation is a Spring annotation that declares an application class's role so Spring can detect and manage it as a component.
- @ComponentScan annotationA Spring annotation that configures package scanning for classes marked with component stereotype annotations.
- @PostConstructAn annotation that marks a method to be invoked after dependency injection is complete and before the managed object is put into service.
- component annotationThe component annotation is a Spring annotation that marks a class for automatic detection and registration as a bean.
- dependency injection containerAn IoC container creates and connects object instances according to configuration.
- Spring bean nameA bean name is an identifier assigned to a Spring-managed bean for selecting or referring to that bean.
- plain old java objectA Java object that does not require framework-specific inheritance or interfaces.
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.
