org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 30 in XML document from class path resource [spring.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 30; columnNumber: 67; Attribute value "squarePointA" of type ID must be unique within the document.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.saravanansivaji.spring.SpringDI.main(SpringDI.java:13)
Caused by: org.xml.sax.SAXParseException; lineNumber: 30; columnNumber: 67; Attribute value "squarePointA" of type ID must be unique within the document.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.validateDTDattribute(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
... 14 more

Recently I faced this exception in spring framework. The following is my code

Square.java – This is simple pojo class having reference to another class “Point”.


package com.saravanansivaji.spring;

public class Square {

private Point pointA;
private Point pointB;
private Point pointC;
private Point pointD;

public Point getPointA() {
return pointA;
}

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

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;
}

public Point getPointD() {
return pointD;
}

public void setPointD(Point pointD) {
this.pointD = pointD;
}

public void draw() {
System.out.println("Square Drawn with the following points:");
System.out.println("("+getPointA().getX()+","+getPointA().getY()+")");
System.out.println("("+getPointB().getX()+","+getPointB().getY()+")");
System.out.println("("+getPointC().getX()+","+getPointC().getY()+")");
System.out.println("("+getPointD().getX()+","+getPointD().getY()+")");
}
}

Point.java – This class represent a point with X and Y coordinates

package com.saravanansivaji.spring;

public class Point {
private int X;
private int Y;

public int getX() {
return X;
}
public void setX(int x) {
X = x;
}
public int getY() {
return Y;
}
public void setY(int y) {
Y = y;
}
}

My application class “SpringDI.java” – just invokes the draw() method of Square instance


public class SpringDI {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Square square = (Square) context.getBean("square");
square.draw();
}
}

Finally, my spring Configuration file “spring.xml

<beans>
<bean id="square" class="com.saravanansivaji.spring.Square">
<property name="pointA" ref="squarePointA"></property>
<property name="pointB" ref="squarePointB"></property>
<property name="pointC" ref="squarePointC"></property>
<property name="pointD" ref="squarePointD"></property>
</bean>

<bean id="squarePointA" class="com.saravanansivaji.spring.Point">
<property name="X" value="10"></property>
<property name="Y" value="10"></property>
</bean>
<bean id="squarePointA" class="com.saravanansivaji.spring.Point">
<property name="X" value="10"></property>
<property name="Y" value="40"></property>
</bean>
<bean id="squarePointA" class="com.saravanansivaji.spring.Point">
<property name="X" value="20"></property>
<property name="Y" value="10"></property>
</bean>
<bean id="squarePointA" class="com.saravanansivaji.spring.Point">
<property name="X" value="20"></property>
<property name="Y" value="40"></property>
</bean>
</beans>

XmlBeanDefinitionStoreException is called as “XML-specific BeanDefinitionStoreException”. This exception extends the org.springframework.beans.factory.BeanDefinitionStoreException.

The BeanDefinitionStoreException is thrown when a BeanFactory encounters an invalid bean definition. For e.g. in case of incomplete or contradictory bean metadata, this exception occurs.

With respect to my code, the exception description says that “Attribute value “squarePointA” of type ID must be unique within the document.”

Now, we need to analyze the configuration file for any invalid data. Seeing my configuration file above, I found the bean “square” has reference to “pointA, pointB, pointC and pointD”. But I defined the duplicate bean declaration only for “pointA” which points to “squarePointA”. I defined “squarePointA” bean declaration four times.

After correction, my configuration file looks like below,

<beans>
<bean id="square" class="com.saravanansivaji.spring.Square">
<property name="pointA" ref="squarePointA"></property>
<property name="pointB" ref="squarePointB"></property>
<property name="pointC" ref="squarePointC"></property>
<property name="pointD" ref="squarePointD"></property>
</bean>

<bean id="squarePointA" class="com.saravanansivaji.spring.Point">
<property name="X" value="10"></property>
<property name="Y" value="10"></property>
</bean>
<bean id="squarePointB" class="com.saravanansivaji.spring.Point">
<property name="X" value="10"></property>
<property name="Y" value="40"></property>
</bean>
<bean id="squarePointC" class="com.saravanansivaji.spring.Point">
<property name="X" value="20"></property>
<property name="Y" value="10"></property>
</bean>
<bean id="squarePointD" class="com.saravanansivaji.spring.Point">
<property name="X" value="20"></property>
<property name="Y" value="40"></property>
</bean>
</beans>

After code fix, execution gives the following Output

Square Drawn with the following points:
(10,10)
(10,40)
(20,10)
(20,40)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: