Asad Asad - 7 months ago 41
Java Question

getting NullPointerException using BeanPostProcessor

I am making an simple program to test the bean post processore but getting the NullPointerException. Here is the code i have write

Person Class:

public class Person {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void init(){
System.out.println("Bean Initialize");
}

public void destroy(){
System.out.println("Bean Destroy");
}
}


PostProcessor Class:

public class Post implements BeanPostProcessor {

@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("Before Initialization : " + beanName);
return null;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("After Initialization : " + beanName);
return null;
}
}


Controller CLass:

public class Controller {

public static void main(String[] args){
@SuppressWarnings("resource")
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Person p = (Person)context.getBean("person");
System.out.println(p.getName());
context.registerShutdownHook();
}
}


Spring.xml :

<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 id="person" class="com.data.Person" init-method="init" destroy-method="destroy">
<property name="name" value="Spring"></property>
</bean>

<bean class="com.postprocessor.Post"/>



Stack Trakce:

After Initialization : person
Apr 27, 2016 10:29:08 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'person' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'person' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.controller.Controller.main(Controller.java:12)
Caused by: java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1660)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1645)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 12 more


This is all the code i have written can any body tell me what is the mistake i am doing here. I shall be thankful.

Answer

Solution

@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
        throws BeansException {
    System.out.println("Before Initialization : " + beanName);
    return bean;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
        throws BeansException {
    System.out.println("After Initialization : " + beanName);
    return bean;
}

Explanation

BeanPostProcessor is a factory hook that allows for custom modification of new bean instances, e.g. checking for marker interfaces or wrapping them with proxies.

So purpose of this interface is to define callback methods that you can implement to provide your own instantiation logic, dependency-resolution logic, modify the new beans instances etc.

As an example you can wrap up your bean in a proxy bean at the time of bean instantiation.

If defined PostProcessor methods are called by AbstractAutowireCapableBeanFactory to get the wrapped bean and you are returning null from your PostProcessor method which essentially creating the null pointer exception.

FYI Corresponding codes from grepcode