Skip to content

Spring Beans

Vishnu Garg edited this page Aug 4, 2018 · 2 revisions

Spring Beans

What is Bean

The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container.

Configuration metadata, which is needed for the container to know the following

  • How to create a bean
  • Bean's lifecycle details
  • Bean's dependencies

All the above configuration metadata translates into a set of the following properties that make up each bean definition.

Meta Data 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 XMLbased 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 methodA callback to be called just after all necessary properties on the bean have been set by the container.

9.destruction method A callback to be used when the container containing the bean is destroyed.

Spring Configuration Metadata

  • XML based configuration file. By creating Spring Configuration XML file to configure the beans. If you are using Spring MVC framework, the xml based configuration can be loaded automatically by writing some boiler plate code in web.xml file.
  • Annotation-based configuration : By using @Service or @Component annotations. Scope details can be provided with @Scope annotation.
  • Java-based configuration Starting from Spring 3.0, we can configure Spring beans using java programs. Some important annotations used for java based configuration are @Configuration, @ComponentScan and @Bean.

Bean Scopes

  • singleton – Only one instance of the bean will be created for each container. This is the default scope for the spring beans. While using this scope, make sure bean doesn’t have shared instance variables otherwise it might lead to data inconsistency issues.
  • prototype – A new instance will be created every time the bean is requested.
  • request – This is same as prototype scope, however it’s meant to be used for web applications. A new instance of the bean will be created for each HTTP request.
  • session – A new bean will be created for each HTTP session by the container.
  • global-session – This is used to create global session beans for Portlet applications.

The default scope is always singleton.

Clone this wiki locally