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 this chapter, you 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 objects, you need to add them to the context. You have to add to the context any object instance that you expect Spring will use in specific ways. Later in this book, we discuss using different capabilities provided by Spring in apps. You’ll learn that plugging in such features is done through the context. You need to add object instances to the context and establish relationships among them to make Spring aware of them. Spring uses the instances in the context to connect your app to various functionality it provides. You’ll learn the basics of the most important such features (like transactions, security configurations, testing, and so on) throughout the book.

2.1    Creating a Maven project

2.2    Adding new beans to the Spring context

2.2.1   Using the @Bean annotation to add beans into the Spring context

2.2.2   Using stereotype annotations to add beans into the Spring context

2.2.3   Programmatically adding beans to the Spring context

2.3    Summary