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
null
within your BPP? – Sotirios Delimanolis