Spring InitializingBean Example

By Arvind Rai, October 26, 2021
On this page we will provide Spring InitializingBean example. The InitializingBean is an interface containing afterPropertiesSet() method. A bean can use it to perform a task required after the bean properties are set. The BeanFactory invokes afterPropertiesSet() method once the bean properties are initialized. The InitializingBean can be used to validate our properties value or to initialize any task.

Note: The alternative of InitializingBean is @PostConstruct Annotation.

Maven File

Find the Maven file used in our example.
pom.xml
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.5.5</version>
	<relativePath />
</parent>
<properties>
	<java.version>16</java.version>
</properties>
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
</dependencies> 

Implement InitializingBean

The InitializingBean has following method.
void afterPropertiesSet() throws Exception 
The BeanFactory invokes the above method once BeanFactory has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.
Find the code to use InitializingBean. We are creating a class that is implementing InitializingBean and defining its afterPropertiesSet() method.
Book.java
package com.concretepage;
import org.springframework.beans.factory.InitializingBean;
public class Book implements InitializingBean {
  private String bookName;

  @Override
  public void afterPropertiesSet() {
	System.out.println("---Do initialization task---");
	if (bookName != null) {
	  System.out.println("Book Name:" + bookName);
	}
  }

  public String getBookName() {
	return bookName;
  }

  public void setBookName(String bookName) {
	this.bookName = bookName;
  }
} 
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="book" class="com.concretepage.Book">
      <property name="bookName" value="Mahabharat"/>
    </bean>
</beans> 

Run Application

To test the application, run the following main method.
SpringDemo.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo {
	public static void main(String[] args) {
		AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
	}
} 
Find the output.
09:25:02.670 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'book'
---Do initialization task---
Book Name:Mahabharat
09:25:02.670 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'book' 

Reference

Interface InitializingBean

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us