Failed to import bean definitions from URL location [classpath:applicationContext-core.xml]

51,773

As you've not specified you web application structure. I assume you've a simple web application at hand with the following structures

  • webapp
    • WEB-INF/classes/applicationContext.xml
    • WEB-INF/lib/nad-core-0.0.1-SNAPSHOT.jar/applicationContext-core.xml

Application context.xml refers to the applicationContext-core.xml file using the import tag. I did encounter a similar situation in my web application, here're the check lists that you should go through and may be one of them can apply to your situation.

  1. Check the generated snapshot jar file for the applicationContext-core.xml file and make sure it is in the root directory of the jar. As silly as it sounds, this was the root cause of the issue I faced in my deployment.
  2. Make sure your Maven Pom.xml file is configured to include this XML file from the resources folder. You can use the resource tags in the build phase of Maven to package them within the jar file itself.
  3. You can try removing the import tag from application context.xml file and instead load both of them from Spring's webapplication context itself.
    1. Add a context loader listener class from spring org.springframework.web.context.ContextLoaderListener
    2. Add context-param contextConfigLocation with value classpath:applicationContext-core.xml,classpath:applicationContext.xml. Spring has the ability to dynamically sort out the dependencies before initiating the bean factory.

Hope this check list helps.

Share:
51,773
Baptiste Metge
Author by

Baptiste Metge

Updated on November 05, 2020

Comments

  • Baptiste Metge
    Baptiste Metge over 3 years

    I'm working on a java project with spring on eclipse using Maven, and running on a Tomcat server v6.0. Everything was working fine since yesterday morning.

    Here his my problem : I'm building my project, I got a build success. Then I start my Tomcat server and got this error :

    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:applicationContext- core.xml]

    Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext-core.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext-core.xml] cannot be opened because it does not exist

    I found out similar problem on some website but none of them give me a solution that worked for me.

    It seems that eclipse isn't able to find applicationContext-core.xml when I'm doing this :

    <import resource="classpath:applicationContext-core.xml" />
    

    However, I do have the needed jar file nad-core-0.0.1-SNAPSHOT.jar in WEB-INF/lib containing applicationContext-core.xml.

    I even tried to add it manually to the classpath but I was still having the same problem.

    I keep on looking for a solution, when suddendly it work again once after restarting Eclipse and building while Eclipse was still updating indexes and my project was having this strange status Hg status pending instead of default. Surprised by this result I decide to build again my project after restarting Eclipse and I got the error again and I enable to make it work again. It's quite annoying...

    This looks to be a really random problem.

    Thanks a lot for your help :)