0

Just for curiosity, I left the id of the bean inside bean.xml empty. I was expecting the exception something like "bean id is not defined" or "bean id can't be null" But actually, I got the below exception -

cvc-datatype-valid.1.2.1: '' is not a valid value for 'NCName'.

What does it indicate?

Full exception is -

06:26:18.262 [main] DEBUG org.springframework.beans.factory.xml.PluggableSchemaResolver - Found XML schema [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-3.0.xsd
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 13 in XML document from class path resource [beans.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 94; cvc-datatype-valid.1.2.1: '' is not a valid value for 'NCName'.
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:399)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)

The content of bean.xml is

<?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 id = "employee" class = "com.test.springboot.spring_boot_example.Employee" >
      <property name = "name" value = "Arvind"/>
       <property name = "address" ref = "address"/>
   </bean>

   <bean id = "" class = "com.test.springboot.spring_boot_example.Address" scope="prototype">
      <property name = "houseNumber" value = "64"/>

   </bean>



</beans>
Arvind Kumar
  • 459
  • 5
  • 21

1 Answers1

2

It's because the xml will not validate against the DTD

Since the id property is expected to be an NCName the xml parser will throw the exception. An NCName is not allowed to be empty.

There is also an excellent q/a here to read up more about how it's used

jontro
  • 10,241
  • 6
  • 46
  • 71