1
                package com.bpp.beans;

                public class EmpBean {
                    String name;
                    String id;
                    public String getName() {
                        return name;
                    }
                    public void setName(String name) {
                        this.name = name;
                    }
                    public String getId() {
                        return id;
                    }
                    public void setId(String id) {
                        this.id = id;
                    }

                    public void initempbean()
                    {
                        System.out.println("inside initempbean");
                    }

                    public void destroyempbean()
                    {
                        System.out.println("inside destroyempbean");
                    }
                }


            package com.bpp.beans;

            public class Person {
                String name;
                String id;
                EmpBean empBean;
                public String getName() {
                    return name;
                }
                public void setName(String name) {
                    this.name = name;
                }
                public String getId() {
                    return id;
                }
                public void setId(String id) {
                    this.id = id;
                }
                public EmpBean getEmpBean() {
                    return empBean;
                }
                public void setEmpBean(EmpBean empBean) {
                    this.empBean = empBean;
                }

                public void initperson()
                {
                    System.out.println("inside init personnnnnnn");
                }

                public void destroyperson()
                {
                    System.out.println("inside destroy personnnn");
                }

            }

        package com.bpp.beans;

        import org.springframework.beans.BeansException;
        import org.springframework.beans.factory.config.BeanPostProcessor;

        public class EmpPostProcessor implements BeanPostProcessor {

            @Override
            public Object postProcessAfterInitialization(Object bean, String arg1)
                    throws BeansException {

                System.out.println("inside bean pp after initialization");

                return null;
            }

            @Override
            public Object postProcessBeforeInitialization(Object bean, String arg1)
                    throws BeansException {
                System.out.println("inside bean pp before initialization");

                return null;
            }

        }
    <?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:aop="http://www.springframework.org/schema/aop"
            xmlns:c="http://www.springframework.org/schema/c"
            xmlns:p="http://www.springframework.org/schema/p"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

            <bean class="com.bpp.beans.EmpPostProcessor"/>

            <bean id="emp" class="com.bpp.beans.EmpBean" init-method="initempbean" destroy-method="destroyempbean">
                <property name="name" value="sanjay"/>
                <property name="id" value="44"/>
            </bean>

            <bean id="person" class="com.bpp.beans.Person" init-method="initperson" destroy-method="destroyperson">
                <property name="name" value="sanju"/>
                <property name="id" value="77"/>
                <property name="empBean" ref="emp"/>
            </bean>



    </beans>                


package com.bpp.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bpp.beans.Person;

public class BppMain {

    public static void main(String[] args) {

        ApplicationContext context=new ClassPathXmlApplicationContext("com/bpp/commons/application-context.xml");
         Person obj=context.getBean("person",Person.class);
         System.out.println("the value of name is:"+obj.getName());
    }

}

The exception is null pointer exception,while creating emp bean

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emp' defined in class path resource [com/bpp/commons/application-context.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
    at com.bpp.main.BppMain.main(BppMain.java:12)
Caused by: java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1522)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 12 more
2
  • 1
    Why are you retuning null within your BPP? Commented Feb 25, 2015 at 18:30
  • thanq,i did not notice it . . Commented Feb 25, 2015 at 18:34

1 Answer 1

0

In the Spring doc is written for postProcessAfterInitialization and postProcessBeforeInitialization :

Returns: the bean instance to use, either the original or a wrapped one

Since you do nothing in these methods you should return the Object passed as a parameter, which is the instance of the bean.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.