16
votes

I know that:

  1. ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

    loads context definition from an XML file located in the classpath, treating context definitions as classpath resources.

  2. ApplicationContext context = new FileSystemXmlApplicationContext("bean.xml");

    loads context definition from an XML file in the filesystem.

  3. XmlWebApplicationContext

    loads context definition from an XML file contained within a web application.

But, what does it exactly mean??

Thanks :)

3
You already described how they are different in usage. Internally they have different behaviors like XmlWebApplicationContent by default will look for applicationContext.xml in WEB-INF folder. And also it takes init parameter for configuring different location. Basically they will use different approaches to load the XML files.Seshagiri

3 Answers

29
votes
  • ClassPathXmlApplicationContext will read files from your classpath. They must be in classes folder of your web application or in a jar in your libfolder.

  • FileSystemXmlApplicationContext can access all your file system, for example c:/config/applicationContext.xml.

  • XmlWebApplicationContext certainly can access to files contained in your web application, but this is not the most important thing. It implements WebApplicationContext and this means that it will detect ServletContextAware beans, register custom scopes (request, session, ...) among other things.

1
votes

FileSystemXmlApplicationContext- You need to provide complete full path of xml bean ClassPathXmlApplicationContext - In this case you DONOT need to set full path, as long as classpath is set

0
votes

I think above opinion may have something wrong, FileSystemXmlApplicationContext can not access your whole file system, what it can only scan is your whole project folder.In order to prove my conclusion i make a example, first using ClasspathXmlApplicationContext and everything is normal, the second time i move beans.xml file to my desktop folder, so there is no beans.xml file in the project hirachy, and change ClassPathXmlApplicationContext to FileSytemXmlApplicationContext and something goes wrong, error trace below:

    INFO: Loading XML bean definitions from file [/Users/crabime/Development/IdeaProjects/springInterview/Users/crabime/Desktop/beans.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [/Users/crabime/Development/IdeaProjects/springInterview/Users/crabime/Desktop/beans.xml]; nested exception is java.io.FileNotFoundException: Users/crabime/Desktop/beans.xml (No such file or directory)

So FileSystemXmlApplicationContext can only detect the current project all folder. For example you make a directory which named config under the project root directory, and you can change your Main Class code like below:

ApplicationContext atx = new FileSystemXmlApplicationContext("/config/beans.xml");

And everything will ok again. So if all like sinuhepop said i think there should something need to be changed.