What Is Bean in Spring? What is the Scope and Lifecycle of Bean?


What Is Bean in Spring? What is the Scope and Lifecycle of Bean?

To learn about Java Spring, it is necessary to understand what a bean is. Similarly, learn about the scope and lifecycle of a bean.

What Is a Bean in Spring

In Spring, the Spring IoC container manages objects which powers the entire application. These objects are known as beans. All objects which Spring IoC instantiates assembles, and supervises fall into the category of a bean.

You can create a bean by providing the configuration metadata to your container. A bean has the following properties.

  • class: It is a mandatory attribute and defines the bean class which will be required for the generation of the bean.
  • name: It defines a unique name for the bean identifier. If you are working with configuration metadata which is powered by XML, you can utilize the name or id to define the bean identifier.
  • Scope: It defines the scope for all those objects which are generated through a single bean definition.
  • constructor-arg: It is used for the dependency injection.
  • properties: It is used for the dependency injection.
  • autowiring mode: It is used for the dependency injection.
  • lazy-initialization mode: By using a lazy-initiated bean, you can generate an instance of bean whenever the first request comes as opposed to the bean creation at the startup.
  • initialization method: It is used as a callback when the IoC container defines the mandatory properties of the bean.
  • destruction method: It is a callback which is used when the container which holds the bean is eliminated.

Scope Of Bean

While specifying a bean, you can also define its scope. For instance, you can set a “prototype” attribute for a bean’s scope; this means that the Spring has to generate a new instance of bean whenever it is needed. On a similar note, you can use the “singleton” attribute if you need to return bean instances in accordance with your requirements. In total, there are five bean scopes.

  • singleton: It defines a single bean instance for each Spring IoC container.
  • prototype: It defines a single bean instance with multiple instances for an object.
  • request: It defines an HTTP request for a bean.
  • session: It defines an HTTP session for a bean.
  • global-session: It defines a global HTTP session for a bean.

Singleton

When a bean’s scope is defined as a singleton, then precisely one object instance is generated by the Spring IoC container. The instance is then saved in a cache which is used to store all the beans associated with the singleton scope. Afterward, any reference or request which comes for that bean sends back the cached object. By default, the scope of a bean is always set to a singleton. In case, you only require a single bean instance, configure your scope property and change it to single. For instance, consider the following format.

<!—Using the singleton scope to define a bean –>

<bean id = “…” class = “…” scope = “singleton”>

<!–write the configuration and collaborators of the bean here –>

</bean>

Prototype

When the scope is specified as a prototype, a new bean instance is generated by the Spring IoC container whenever a request is generated for that bean. Therefore, utilize the prototype scope when your beans are stateful while using the singleton when they are stateless. To specify a prototype scope, you can use the following format.

<!—Use the prototype scope to define a bean –>

<bean id = “…” class = “…” scope = “prototype”>

<!– write the configuration and collaborators of the bean here –>

</bean>

Bean Life Cycle

After the instantiation of a bean, it can execute initialization so it changes into a usable state. After the need for a bean is completed and subsequently, it is deleted, there has to be some sort of cleanup. In the post, we would discuss 2 of the most crucial lifecycle callback methods for beans.

To begin with, you can write <bean> and use with destroy-method or init-method parameters. The latter is used to define a method which is called first after the bean’s instantiation. The destroy method is used to define a method which is revoked after the deletion of a bean from the container.

Initialization Callbacks

In the org.springframework.beans.factory.InitializingBean interface, you can use the following method.

void afterPropertiesSet() throws Exception;

Therefore, you can easily use this interface and initialize by using the following format.

public class BeanExampleOne implements InitializingBean {

public void afterPropertiesSet() {

// write the code for initialization

}

}

If you are using configuration data which uses XML, then you utilize the attribute, “init-method”, to define a method name having a signature without any argument.

<bean id = “beanExampleOne” class = “examples.BeanExampleOne” init-method = “init”/>

Consider the following definition for your class.

public class BeanExampleOne {

public void init() {

// write the code for initialization

}

}

Destruction Callbacks

In the org.springframework.beans.factory.DisposableBean interface, you can use the following method.

void destroy() throws Exception;

Therefore, you can easily use this interface and initialize by using the following format.

public class BeanExampleTwo implements DisposableBean {

public void destroy() {

// write the code for destruction

}

}

If you are using configuration data which uses XML, then you utilize the attribute, “destroy-method”, to define a method name having a signature without any argument.

<bean id = “beanExampleTwo” class = “examples.BeanExampleTwo” destroy-method = “destroy”/>

Consider the following definition for your class.

public class BeanExampleTwo {

public void destroy() {

// write code for destruction

}

}

When you are working in a non-website application setup with a Spring IoT container, for instance, in a desktop environment, then you can configure a shutdown for the Java Virtual Machine. This helps to create an efficient shutdown and revokes only the appropriate destroy methods.

It must be noted that the use of Disposable Bean or Initializing Bean callbacks is not recommended as XML configuration allows a greater degree of flexibility for the XML configuration.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s