chapter two

2 The Spring context: Defining beans

 

This chapter covers

  • Understanding the need for Spring context
  • Adding new object instances to the Spring context in a declarative way
  • Adding new object instances to the Spring context programmatically

Let’s start learning how to work with a crucial Spring framework element: the context (also known as the application context in a Spring app). Imagine the context as a place in the memory of your app in which we add all the object instances that we want the framework to manage. By default, Spring doesn’t know any of the objects you define in your application. To enable Spring to see your app’s objects, you need to add them to the context.

Learning what Spring context is and how it works is the first step in learning to use Spring, because without knowing how to manage the Spring context, almost nothing else you’ll learn to do with it will be possible. The context is a complex mechanism that enables Spring to control instances you define. This way, it allows you to use the capabilities the framework offers.

2.1 Using the @Bean annotation to create beans

2.2 Using stereotype annotations to create beans

2.3 Programmatically creating beans

2.4 Questions

2.5 Summary