-2

Now I want to load applicationContext.xml by ClassPathXmlApplicationContext, my working folder structure is src/main/java; src/main/resources/applicationContext.xml;

My code is

ApplicationContext ctx =
            new ClassPathXmlApplicationContext("src/main/resources/applicationContext.xml");

Throw cannot be opened because it does not exist,but

    ApplicationContext ctx =
            new ClassPathXmlApplicationContext("applicationContext.xml");

is ok, Why is it happen?

Shiva
  • 6,677
  • 4
  • 36
  • 61
Hming
  • 1
  • Try `new ClassPathXmlApplicationContext("applicationContext.xml");` – Jay Smith May 17 '17 at 04:25
  • @JaySmith, he already said that "new ClassPathXmlApplicationContext("applicationContext.xml");" is working fine, but question here is "why its working?" – Afridi May 17 '17 at 04:28
  • Because in maven project `src/main/resources` is source folder. Java files from `src/main/java` and resources are compiled into `target/classes` folder. – Jay Smith May 17 '17 at 04:30
  • Please check this http://stackoverflow.com/questions/37198095/spring-classpathresource-cannot-be-opened-because-it-does-not-exist – Shiva May 17 '17 at 04:31

1 Answers1

-2

ClassPathXmlApplicationContext will look for specific file from the classpath. From the Java Build Path -> Source section, you can see the source folders: src/main/java, src/main/resources, src/test/java. They will be used as classpath by the java applicaiton.

So if you pass applicationContext.xml as parameter, jvm will look for this file from the above 3 folders. src/main/resources/beanConfigurationName.xml will lead jvm to look for "src/main/resources/beanConfigurationName.xml" from classpath.

pechen
  • 1