Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • BeanPostProcessor in Spring

    • 0
    • 3
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 353
    Comment on it

    BeanPostProcessor :- It is an interface that defines callback methods by which you can provide your own instantiation logic and your own custom logic before and after the bean creation. along with this you can perform some additional logic or your own task before and after initializing the bean.


    BeanPostProcessor Example

    Bean_Post_Process.java

    package com.manish;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    
    public class Bean_Post_Process implements BeanPostProcessor {
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String name)
                throws BeansException {
            // TODO Auto-generated method stub
            System.out.println("After Intializing the Bean:="+name);
            return bean;
        }
    
        @Override
        public Object postProcessBeforeInitialization(Object bean, String name)
                throws BeansException {
            // TODO Auto-generated method stub
            System.out.println("Before Intializing the Bean:="+name);
            return bean;
        }
    
    
    
    
    }
    

    Student.java

    package com.manish;
    
    public class Student 
    {
        private int id;
        private String name;
    
    
        Student()
        {
            System.out.println("Student Bean");
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    
        public void init()
        {
            System.out.println("Initializing Bean Student");
        }
    
        public void destroy()
        {
            System.out.println("Destroy Bean Student");
        }
    
    
    
    }
    

    app-context.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"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
     <bean id="student" class="com.manish.Student" init-method="init" destroy-method="destroy">
        <property name="id" value="1216"></property>
        <property name="name" value="manish"></property>
    
    </bean> 
    
    
    
    
    </beans>
    

    App.java

    package com.app;
    
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.AbstractApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        import com.manish.Course;
        import com.manish.Student;
    
        public class App {
    
            /**
             * @param args
             */
            public static void main(String[] args) {
                // TODO Auto-generated method stub
    
                AbstractApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"app-context.xml"});
    
    
                Student stu=(Student)ctx.getBean("student");
    
    
                System.out.println("Rollno:="+stu.getId());
                System.out.println("Name:="+stu.getName());
                ctx.registerShutdownHook();
    
    
    
            }
    
        }
    

    Output:-

          Student Bean
    Initializing Bean Student
    Rollno:=1216
    Name:=manish
    Destroy Bean Student
    


    The above example show that how the BeanPostProcessor interface can be used and to override it's callback methods postProcessAfterInitialization and postProcessBeforeInitialization.

    These callbacks methods can be called after creating and before deletion of an bean

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: