Search code examples
springclasspathfilenotfoundexceptionapplicationcontext

Spring:configuration xml not found in classpath


For my simple maven project this doesn't work:

ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");

config.xml is resided at the same class level

How,actually,add config.xml to classpath? note: my project is a lib,if I do the same in other web project with configuration in web.xml:

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:config.xml</param-value>
        </context-param>

that it works OK

Here I needn't web.xml, just correct classpath.


Solution

  • When you enter classpath*:config.xml, the classpath* is a wild card indicates that you want to load every file matching config.xml on the entire classpath, not just the single file config.xml. This may be why your solution is working otherwise.

    When instantiating a new ClassPathXmlApplicationContext, try giving the full classpath as an argument: com\sergionni\myproj\config.xml.