简体   繁体   English

在Spring的bean.xml中将bean的id设置为空时发生奇怪的异常

[英]Strange exception when id of the bean is set empty in spring's bean.xml

Just for curiosity, I left the id of the bean inside bean.xml empty. 出于好奇,我将bean.xml中的bean的ID保留为空。 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 - 我一直在期待类似“未定义bean id”或“ bean id不能为null”之类的异常,但是实际上,我得到了以下异常-

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 bean.xml的内容是

<?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>

It's because the xml will not validate against the DTD 这是因为xml无法针对DTD进行验证

Since the id property is expected to be an NCName the xml parser will throw the exception. 由于id属性应该是NCName,因此XML解析器将引发异常。 An NCName is not allowed to be empty. NCName不允许为空。

There is also an excellent q/a here to read up more about how it's used 这里还有一个出色的问答以阅读更多有关其用法的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在春季使用bean.xml时发生Classdefnotfound异常 - Classdefnotfound exception while using bean.xml in spring 在 spring bean.xml 中指定 xsd 的正确方法是什么 - What is the proper way to specify a xsd within a spring bean.xml CDI-正确的bean.xml格式是什么? - CDI - what is the correct bean.xml format? 加载应用程序上下文文件Bean.xml - Loading Application Context File Bean.xml 如何从bean.xml获取bean值 - How to I get bean value from bean.xml 添加了新的bean.xml并获取具有根本原因的类路径资源[Spring-Mail.xml]无法打开,因为它不存在] - Added new bean.xml and get class path resource [Spring-Mail.xml] cannot be opened because it does not exist] with root cause 线程“主”org.springframework.beans.factory.BeanDefinitionStoreException 中的异常:IOException java.io.FileNotFoundException:bean.Z0F635D0E0F3874FFF8B7E8C - Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: IOException java.io.FileNotFoundException: bean.xml 如何在 Spring 中指定 bean 的“id”属性? - How to specify bean`s "id" property in Spring? 为什么不能在春天将bean的id设置为属性 - why can not set id of a bean as property in spring Bean中发生冲突时,Spring Bean创建异常 - Spring Bean Creation Exception when Conflicts in Beans
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM