What is difference between class path , file syste

2020-02-10 03:36发布

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条回答
甜甜的少女心
2楼-- · 2020-02-10 04:32

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.

查看更多
闹够了就滚
3楼-- · 2020-02-10 04:37

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

查看更多
forever°为你锁心
4楼-- · 2020-02-10 04:39
  • 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.

查看更多
登录 后发表回答