What is a Spring Beans?
In Spring, the objects that form your application’s backbone and are managed by the Spring IoC container are called beans. A bean is an object instantiated, assembled, and managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.
Bean definition contains the configuration metadata information, which is needed for the container to know:
- How to create a bean
- lifecycle details
- dependencies
The above configuration metadata translates into the following set of properties.
1 | class |
This attribute is mandatory and specifies the bean class to be used to create the bean. | |
2 | name |
This attribute specifies the bean identifier uniquely. In XML-based configuration metadata, you use the id and/or name attributes to specify the bean identifier(s). | |
3 | scope |
This attribute specifies the scope of the objects created from a particular bean definition | |
4 | constructor-arg |
This is used to inject the dependencies. | |
5 | properties |
This is used to inject the dependencies. | |
6 | autowiring mode |
This is used to inject the dependencies. | |
7 | lazy-initialization mode |
A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at the startup. | |
8 | initialization method |
A callback is to be called just after all necessary properties on the bean have been set by the container. | |
9 | destruction method |
A callback is to be used when the container containing the bean is destroyed. |
Bean configuration
We can do bean configuration using the following three ways:
- Creating Bean Inside an XML Configuration File (beans.xml)
- Using @Component Annotation
- Using @Bean Annotation
We will see each of these in detail below.
- XML Based bean configuration:
Let’s consider we have a Greeting class as shown below.
For the above class, we can create a bean as follows:
- Using @Component Annotation
- @Component annotation is used which allows Spring to automatically detect the custom beans.
- Following is the same Greeting class which we can annotate with @Component annotation.
- In the above case, a bean will be created with the bean ID as “greeting”. We can also override the ID by giving some other name inside the @Component annotation
@Component("greetingBean")
- Using @Bean Annotation
- One of the most important annotations in Spring is the @Bean annotation which is applied to a method to specify that it returns a bean to be managed by Spring context.
- Spring Bean annotation is usually declared in Configuration classes methods.
- Let’s create a bean for the above Greeting class. We will create a configuration class as shown below.
- Here bean with bean ID as “greeting” will be created, this is the same as the method name. We can also change the name by mentioning different parameters in bean annotation as shown below.
@Bean(“greetingBean”)