Classpathxmlapplication and Filesystemxmlapplicationcontext use differences in spring

Source: Internet
Author: User

ApplicationContext ac = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
DataSource ds = (DataSource) Ac.getbean ("DS");
System.out.println (DS);

The parameters in the Classpathxmlapplicationcontext silently go to the classpath path to find. You can add Classpath:applicationContext.xml.

If more than one XML file can be a new string array

ApplicationContext ac = new Classpathxmlapplicationcontext (new string[]{"Applicationcontext.xml", " Applicationcontext_dao.xml "});

If you use a file path, you can also

ApplicationContext ac = new Classpathxmlapplicationcontext ("file:f:\\lee\\workspace\\struts2\\oa\\src\\ Applicationcontext.xml ");

Note: If the absolute path is dead, file: is required.

Differences in the use of Classpathxmlapplicationcontext and Filesystemxmlapplicationcontext

If you use an absolute path in the Filesystemxmlapplicationcontext parameter, you can add a file prefix or no file prefix, which is one of the differences with Classpahtxmlapplicaitoncontext.

ApplicationContext ac = new Filesystemxmlapplicationcontext ("file:f:\\lee\\workspace\\struts2\\oa\\src\\ Applicationcontext.xml ");

ApplicationContext ac = new Filesystemxmlapplicationcontext ("f:\\lee\\workspace\\struts2\\oa\\src\\ Applicationcontext.xml ");

The second one is also possible.

If you're looking for a class path, you have to add classpath.

ApplicationContext ac = new Filesystemxmlapplicationcontext ("Classpath:applicationContext.xml");

If the path is not absolute and classpath is not specified, it is found by default in the path of the project

ApplicationContext ac = new Filesystemxmlapplicationcontext ("Src/applicationcontext.xml");

ApplicationContext ac = new Filesystemxmlapplicationcontext ("Webcontent/applicationcontext.xml");

And the path is case insensitive

Summary: Classpathxmlapplicationcontext default is to load the classpath path under the configuration file, and Filesystemxmlapplicationcontext default is to load the file system configuration file

Classpathxmlapplication and Filesystemxmlapplicationcontext use differences in spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.