The difference between classpathxmlapplication and Filesystemxmlapplicationcontext in spring

Source: Internet
Author: User

I. Overview

When you encounter a spring configuration file that is not loaded in the project, simply analyze it and write this article memo!

II. Resource requirements for testing

Testbean.java

public class TestBean {    public TestBean(){ System.out.println(this.getClass().getName().concat(" init !")); } public String getTestStr() { return "testStr"; }}

Applicationcontext.xml

<bean id="testBean" class="com.bean.TestBean" />
Second, the difference 2.1 Classpathxmlapplicationcontext use method

Classpathxmlapplicationcontext will go to the ClassPath path by default. The ClassPath path refers to the compiled classes directory.

Example:

@TestPublicvoidTestbean(){Single configuration file mode one beanfactory beanfactory=New Classpathxmlapplicationcontext ( "Applicationcontext.xml"); //single profile mode two beanfactory beanfactory=new Classpathxmlapplicationcontext ( "Classpath:applicationContext.xml"); //multiple configuration Files beanfactory beanfactory=new Classpathxmlapplicationcontext (new string[]{ " Applicationcontext.xml "}); //absolute path must be added "file:" prefix beanfactory beanfactory = new Classpathxmlapplicationcontext ( "file:e:\workspace\idea_workspace\spring\springtest\ Src\main\resources\applicationcontext.xml "); Testbean bean= (Testbean) Beanfactory.getbean ( "Testbean"); Assertequals (" Teststr ", Bean.getteststr ());}            

To run the sample you will find that "classpath:" is the default.
If it is an absolute path, you need to add a "file:" prefix, not the default.

2.2 Filesystemxmlapplicationcontext How to use

Filesystemxmlapplicationcontext default is to go to the project under the path of loading, can be a relative path, can also be an absolute path, if the absolute path, "file:" prefix can default.

Example:

@TestPublicvoid Testbean () {Classes Directory Beanfactory beanfactory=New Filesystemxmlapplicationcontext ("Classpath:applicationContext.xml");Project path relative path Beanfactory beanfactory=new filesystemxmlapplicationcontext ( "src\\main\\resources\ \applicationcontext.xml "); //multi-profile beanfactory beanfactory=new Filesystemxmlapplicationcontext (new String[]{ "Src\\main\\resources\\applicationcontext.xml"}); //absolute directory beanfactory beanfactory=new Filesystemxmlapplicationcontext (new String[]{ "E:\\workspace\\idea_workspace\\spring\\springtest\\src\\main\\resources\\applicationcontext.xml "}); Testbean bean= (Testbean) Beanfactory.getbean ( "Testbean"); Assertequals (" Teststr ", Bean.getteststr ());}            

The difference between classpathxmlapplication and Filesystemxmlapplicationcontext 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.