How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml


If you are using the SpringBoot framework with the Spring Config file, then the XML bean definition of the file should have the correct namespaces and schema locations. If they are not correct you will likely see an XmlBeanDefinitionStoreException as shown below.

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
 Line 4 in XML document from class path resource [applicationConfig.xml] is invalid

	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:411)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:347)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:319)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:184)
	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:130)
	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:95)
	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
	at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:713)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:595)
	at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:85)
	at org.code2care.demo.DemoApplication.main(DemoApplication.java:14)

Caused by: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 133; cvc-elt.1.a: 

Cannot find the declaration of element 'beans'.

As you can see in the above stack trace the beans declaration is not found.

To fix this issue, make sure that the template for the Spring Config file is the correct one as below.

Valid Spring Config file template:
<?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.xsd">

    <!-- Spring Config template -->

</beans>
Spring Config Template to fix XmlBeanDefinitionStoreException

Note: If you make use of IDE such as IntelliJ, you can get this template by navigating to New -> XML Configuration File -> Spring Config from the resources folder.

IntelliJ - Spring Config File

This is not an AI-generated article but is demonstrated by a human on an M1 Mac with IntelliJ and Spring Boot.

Please support independent contributors like Code2care by donating a coffee.

Buy me a coffee!

Buy Code2care a Coffee!

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap