Home  >  Spring Core

Example of InitializingBean in Spring

By Arvind Rai, April 17, 2013
On this page we will provide example of InitializingBean in spring. InitializingBean is an interface which has afterPropertiesSet() method. Our bean can use it to perform any task required after the bean properties are set. BeanFactory invokes afterPropertiesSet() method once the bean properties are initialized. We can use InitializingBean to validate our properties value or to initializing any task. The alternative of InitializingBean is @PostConstruct Annotation.

Gradle File

Find the gradle file used in our example.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'SpringDemo'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.springframework.boot:spring-boot-starter:1.2.7.RELEASE'
}  



Implement InitializingBean

We are implementing InitializingBean and defining its method afterPropertiesSet().
Book.java
package com.concretepage;
import org.springframework.beans.factory.InitializingBean;
public class Book implements  InitializingBean {
	private String bookName;
	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 Main Method

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.623 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'book' to allow for resolving potential circular references
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'
09:25:02.670 [main] DEBUG o.s.c.s.ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@1a8a8f7c] 
Find the link that will describe about DisposableBean.

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us