troubleshooting Question

spring inheritance of bean definition

Avatar of gudii9
gudii9Flag for United States of America asked on
JavaJSPJava EE
1 Comment1 Solution687 ViewsLast Modified:
Hi,

I am running below example on "spring inheritance of bean definition"


client class looks as below
package org.gp.gpr;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

public class DrawingAppObj {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//	Triangle triangle=new Triangle();
		//	triangle.draw();


//		BeanFactory factory=new XmlBeanFactory(new FileSystemResource("spring.xml"));
		ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
		Trianglebj trianglebj	=(Trianglebj) context.getBean("triangle1");
	//	Trianglebj trianglebj	=(Trianglebj) context.getBean("triangle-alias");
		trianglebj.draw();
	}

}

















Point class looks as below
package org.gp.gpr;

public class Point {
private int x;
public int getX() {
	return x;
}
public void setX(int x) {
	this.x = x;
}
public int getY() {
	return y;
}
public void setY(int y) {
	this.y = y;
}
private int y;

}


Trianglebj class looks as below

package org.gp.gpr;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Trianglebj implements ApplicationContextAware,BeanNameAware {


private Point pointA;
private Point pointB;
private Point pointC;
private ApplicationContext context=null;

	public void draw(){


	//	System.out.println(getType()+"triangle drawed"+getHeight());
	System.out.println("PointA=("+getPointA().getX()+","+getPointA().getY()+")");
	System.out.println("PointB=("+getPointB().getX()+","+getPointB().getY()+")");
	System.out.println("PointC=("+getPointC().getX()+","+getPointC().getY()+")");
		
		//for(Point point : points){
	//		
//			System.out.println(getType()+"triangle drawed"+getHeight());
//		System.out.println("Point=("+point.getX()+","+point.getY()+")");
//			System.out.println("PointB=("+getPointB().getX()+","+getPointB().getY()+")");
//		System.out.println("PointC=("+getPointC().getX()+","+getPointC().getY()+")");
	//	}
	}

	public Point getPointA() {
		return pointA;
	}

	public void setPointA(Point pointA) {
		this.pointA = pointA;
	}

	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		// TODO Auto-generated method stub
		this.context=context;
		
	}

	public Point getPointB() {
		return pointB;
	}

	public void setPointB(Point pointB) {
		this.pointB = pointB;
	}

	public Point getPointC() {
		return pointC;
	}

	public void setPointC(Point pointC) {
		this.pointC = pointC;
	}

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

	

	
}


spring.xml looks as  below

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/portlet/context/WEB-INF/contextInclude.xml">
]>

<beans>

<bean id="parenttriangle" class="org.gp.gpr.Trianglebj">
<property name="pointA" ref="pointA"/>
</bean>

<bean id="triangle1" class="org.gp.gpr.Trianglebj" parent="parenttriangle">
<property name="pointB" ref="pointB"/>
 <property name="pointC" ref="pointC"/> 
</bean>

<bean id="triangle3" class="org.gp.gpr.Trianglebj" parent="parenttriangle">
<property name="pointB" ref="pointB"/>

</bean>
<bean id="pointA" class="org.gp.gpr.Point">
<property name="x" value="0"/>
<property name="y" value="0"/>
</bean>


<bean id="pointB" class="org.gp.gpr.Point">
<property name="x" value="20"/>
<property name="y" value="0"/>
</bean>

<bean id="pointC" class="org.gp.gpr.Point">
<property name="x" value="-200"/>
<property name="y" value="0"/>
</bean>

</beans>

When i run client file with 'triangle1', I got output as below

Mar 27, 2013 2:19:04 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4def8cf3: startup date [Wed Mar 27 14:19:04 CDT 2013]; root of context hierarchy
Mar 27, 2013 2:19:04 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Mar 27, 2013 2:19:04 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2b76e552: defining beans [parenttriangle,triangle1,triangle3,pointA,pointB,pointC]; root of factory hierarchy
bean nameparenttriangle
bean nametriangle1
bean nametriangle3
PointA=(0,0)
PointB=(20,0)
PointC=(-200,0)

when i ran client file with 'triangle3' getting null pointer exception as below

Mar 27, 2013 2:23:03 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@31d520c4: startup date [Wed Mar 27 14:23:03 CDT 2013]; root of context hierarchy
Mar 27, 2013 2:23:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Mar 27, 2013 2:23:03 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@ac980c9: defining beans [parenttriangle,triangle1,triangle3,pointA,pointB,pointC]; root of factory hierarchy
bean nameparenttriangle
bean nametriangle1
bean nametriangle3
PointA=(0,0)
PointB=(20,0)
Exception in thread "main" java.lang.NullPointerException
      at org.gp.gpr.Trianglebj.draw(Trianglebj.java:24)
      at org.gp.gpr.DrawingAppObj.main(DrawingAppObj.java:24)




my question is why i am getting NPE
Also what are, uses, purpose of 'spring inheritance of bean definition' .

Is it is same as bean inheritance.
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
DefnInheritance1.jpg
DefnInheritance2.jpg
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros