Introduction
The context is like a place in the memory of the Spring app in which all object instances are added and managed. Spring uses the instances in the context to connect the app to various functionalities.
By default, the Spring doesn’t know any of the instances defined in the app. And the Spring context is initially empty. These instances should be added to the context to enable Spring to see them.
These object instances are named “beans”.
In a real-world app, not every object will be added to the Spring context, but only the instances that we expect Spring to manage.
Add Beans to Spring Context
Create a Spring context
spring-context dependency should be added to the project to enable the context functionality.
|
|
Then we can create an instance of the Spring context:
|
|
We use the AnnotationConfigApplicationContext
class to create the Spring context instance, which is the implementation that uses annotations, the most used approach today.
Beans can be added into the context in three ways:
- Using the @Bean annotation
- Using stereotype annotations
- Programmatically
Using the @Bean
annotation
The steps that add a bean to the Spring context using the @Bean
annotation are:
- Define a configuration class for the project. A Spring configuration class is characterized by the fact that it is annotated with the
@Configuration
annotation.
|
|
- Add a method to the configuration class that returns the object instance we want to add to the context, and annotate the method with the
@Bean
annotation. This annotation instructs Spring to call this method when at context initialization and add the returned value to the context. The method’s name also becomes the bean’s name, which means it should be a noun rather than a verb by convention. Most often they have the same name as the class.
|
|
- Make Spring use the configuration class defined. When creating the Spring context instance, send the configuration class as a parameter to instruct Spring to use it.
|
|
We can verify the Cat
instance is indeed part of the context now by getBean
method. This method gets a reference of a bean from the context without need to do any explicit casting. If a bean does not exist, Spring will throw an exception.
|
|
Multiple beans of the same type can be added to the Spring context by using multiple methods annotated with @Bean
. Each instance will have a unique identifier, which is used to refer to them correctly.
|
|
In such case, getBean(Cat.class)
will not get the beans from the context because Spring cannot guess which instance is referred to. We need to refer precisely to one of the instances by the bean’s name. The first parameter of getBean()
is now the name of the instance.
|
|
By default, the bean’s name is the same as the method’s name, but we can also give another name to the bean:
|
|
We can also define one bean as primary when we have multiple beans of the same class in the Spring context:
|
|
Using stereotype annotations
Stereotype annotations can be added above the class for which we need to have an instance in the Spring context. The most basic one of steretype annotations is @Component
. When the app creates a Spring context, Spring creates instances of the classes that marked as a component and adds instances of them to the context.
The steps that add a bean to the Spring context using the stereotype annotations are:
- Mark the classes with
@Component
annotation for which we want Spring to add an instance to its context.
|
|
- Add
@ComponentScan
annotation over the configuration class. By default, Spring doesn’t search for classes annotated with stereotype annotations. With the@ComponentScan
annotation, we tell Spring where to look for these classes by enumerating the packages.
|
|
We can continue use getBean
to verify the instance of class Cat
has been added as a bean in the context. Notice in this case, cat’s name would be null
, because Spring just creates the instance of the class, but it’s our duty if we want to change the instance.
|
|
Let’s compare the advantages and disadvantages of the two ways to add beans the Spring context:
- With
@Bean
, We have full control over the instance annotation, which means we can create and config the instance and just hand it to the Spring. While with stereotype annotations, we only have control over the instance after it has been created by the Spring.
Sometimes we want to execute some instructions after the creation of the instance with stereotype annotations, we can add
@PostConstruct
to a method defined in the component class. This method would be called after the Spring executes the constructor and creates the instance.
With
@Bean
, multiple instances of the same class can be created and added to the context. While with stereotype annotations, only one instance of one class will be added to the context.@Bean
can be used to add any instances to the context, even the class of the instance are outside the app (like classes from external libraries or built-in libraries). While stereotype annotations can only be used to classes that the application owns.With
@Bean
, separate methods for beans should be written, while stereotype annotations do not add extra boilerplate code to the app.
In real-world scenarios, stereotype annotations are used much more than the @Bean
annotation due to the less code writing.
Programmatically
Sometimes, we want to implement a custom logic of adding beans to the context and the @Bean
or the stereotype annotations are not enough for our needs. registerBean()
method of the ApplicationContext
instance can be used to add a bean to the Spring context using a programmatic approach.
|
|
registerBean
has four parameters:
|
|
Here is an example using the registerBean()
to add a bean to the Spring context:
|
|
Summary
Adding object instance (beans) to the Spring context is the first step to learn Spring. Spring can only see the instances added to its context.
Beans can be added to the context in 3 ways: @Bean
annotation, stereotype annotations, and programmatically.
@Bean
annotation can enable us to create multiple instances of the same class, and added them through customized methods, which is more flexible, yet it requires extra code writing.- Stereotype annotations requires less code writing, but it only creates one bean for one internal class of the application.
registerBean()
can implement custom logic for adding beans.
Last modified on 2024-05-26