Announcement Announcement Module
Collapse
No announcement yet.
Nullpointer exception while using BeanPostProcessor Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Nullpointer exception while using BeanPostProcessor

    I am able to run the application fine if it is not configured with BeanPostProcessor in the container. As and when it is configured to run with BeanPostProcessor raising null pointer exception.


    Mar 24, 2013 9:19:26 PM org.springframework.context.support.AbstractApplic ationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlAp plicationContext@766e3d60: startup date [Sun Mar 24 21:19:26 IST 2013]; root of context hierarchy
    Mar 24, 2013 9:19:26 PM org.springframework.beans.factory.xml.XmlBeanDefin itionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [BeanConfig.xml]
    Mar 24, 2013 9:19:26 PM org.springframework.beans.factory.support.DefaultL istableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@6542bece: defining beans [com.satya.spring.CustomBeanPostProcessor#0,employe e,customer]; root of factory hierarchy
    EMP NAME SET
    BEAN NAMEemployee
    postProcessBeforeInitializationcom.satya.spring.Em ployeeBean@7b5a6029
    Mar 24, 2013 9:19:26 PM org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry destroySingletons
    INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@6542bece: defining beans [com.satya.spring.CustomBeanPostProcessor#0,employe e,customer]; root of factory hierarchy
    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'employee' defined in class path resource [BeanConfig.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1420)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 91)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:288 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:425)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:139)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:83)
    at com.satya.spring.Client.main(Client.java:11)
    Caused by: java.lang.NullPointerException
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeCustomInitMethod( AbstractAutowireCapableBeanFactory.java:1500)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:1485)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1417)
    ... 12 more


    EmployeeBean.java


    package com.satya.spring;

    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean ;

    public class EmployeeBean implements InitializingBean,DisposableBean,BeanNameAware{
    private String empName;


    public String getEmpName() {
    System.out.println("GETTING EMP NAME"+empName);
    return empName;
    }

    public void setEmpName(String empName) {
    this.empName = empName;
    System.out.println("EMP NAME SET");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
    // TODO Auto-generated method stub
    System.out.println("AFTER PROPERTIES SET METHOD HAS BEEN CALLED");
    }

    @Override
    public void destroy() throws Exception {
    System.out.println("DESTROY METHOD HAS BEEN CALLED");
    // TODO Auto-generated method stub
    }
    public void myInit(){
    System.out.println("MY INIT METHOD EXECUTION");

    }
    public void myDestroy(){
    System.out.println("MY DESTROY METHOD EXECUTION");
    }

    @Override
    public void setBeanName(String arg0) {
    // TODO Auto-generated method stub
    System.out.println("BEAN NAME"+arg0);
    }


    }

    CustomerBeanPostProcessor

    package com.satya.spring;

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostP rocessor;

    public class CustomBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object arg0, String arg1)
    throws BeansException {
    // TODO Auto-generated method stub
    System.out.println("postProcessAfterInitialization ");
    return null;

    }

    @Override
    public Object postProcessBeforeInitialization(Object arg0, String arg1)
    throws BeansException {
    // TODO Auto-generated method stub
    System.out.println("postProcessBeforeInitializatio n"+arg0.toString());
    return null;
    }

    }


    BeanConfig.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-3.0.xsd">
    <bean class="com.satya.spring.CustomBeanPostProcessor"/>

    <bean id="employee" class="com.satya.spring.EmployeeBean" init-method="myInit" destroy-method="myDestroy">
    <property name="empName" value="satya"></property>
    </bean>
    </beans>

    Client.java

    package com.satya.spring;

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFacto ry;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlAp plicationContext;
    import org.springframework.core.io.ClassPathResource;

    public class Client {
    public static void main(String[]args){
    ApplicationContext ctx = new ClassPathXmlApplicationContext("BeanConfig.xml");
    //BeanFactory factory = new XmlBeanFactory(new ClassPathResource("BeanConfig.xml"));
    //ctx.getBean("employee",EmployeeBean.class);
    }
    }


    Please help me how to resolve this error.

  • #2
    Please use [ code][/code ] tags when posting code/xml/stacktraces, that way your code remains readable, currently it is hard to decipher...

    Your BeanPostProcessor is destroying the beans, you are returning null all over the place meaning you are overriding beans with null... I suggest you start to read the documentation on what a Bean(Factory)PostProcessor does and how it works.

    Comment

    Working...
    X