Simple use of the Classpathxmlapplicationcontext class in spring/How to access the spring configuration file in a jar package

Source: Internet
Author: User
Tags log log

A simple test with applicationcontext, get the Bean instance (object) defined in spring. You can use:

ApplicationContext ac = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
Registerdao Registerdao = (Registerdao) ac.getbean ("Registerdao");

If it is more than two:
ApplicationContext ac = new Classpathxmlapplicationcontext (new string[]{"Applicationcontext.xml", "Dao.xml"});

Or use wildcard characters:
ApplicationContext ac = new Classpathxmlapplicationcontext ("Classpath:/*.xml");


Second, classpathxmlapplicationcontext[can only read the configuration files placed in the Web-info/classes directory] and the difference between filesystemxmlapplicationcontext

Classpath: prefix is not required, default refers to the project's classpath path below;
If you want to use an absolute path, you need to add a file: prefix to indicate that this is an absolute path;

For Filesystemxmlapplicationcontext:
The default represents two types:

1. No drive letter is the project work path, that is, the root directory of the project;
2. A drive symbol indicates the absolute path of the file.

If you want to use the classpath path, prefix classpath is required:

public class Helloclient {

Protected static final Log log = Logfactory.getlog (Helloclient.class);

public static void Main (string[] args) {
Resource Resource = new Classpathresource ("Appcontext.xml");
Beanfactory factory = new Xmlbeanfactory (Resource);

Using the Classpath path
ApplicationContext factory = new Classpathxmlapplicationcontext ("Classpath:appcontext.xml");
ApplicationContext factory = new Classpathxmlapplicationcontext ("Appcontext.xml");

Classpathxmlapplicationcontext uses a file prefix that can use an absolute path.
ApplicationContext factory = new Classpathxmlapplicationcontext ("File:f:/workspace/example/src/appcontext.xml");

The path to the file system, which is the root path of the project by default
ApplicationContext factory = new Filesystemxmlapplicationcontext ("Src/appcontext.xml");
ApplicationContext factory = new Filesystemxmlapplicationcontext ("Webroot/web-inf/appcontext.xml");

The classpath: prefix is used so that filesystemxmlapplicationcontext can also read the relative path under Classpath
ApplicationContext factory = new Filesystemxmlapplicationcontext ("Classpath:appcontext.xml");
ApplicationContext factory = new Filesystemxmlapplicationcontext ("File:f:/workspace/example/src/appcontext.xml") ;

   //Do not add the file prefix
    applicationcontext factory = new Filesystemxmlapplicationcontext ("F:/workspace/example/src/appcontext.xml");
    Ihelloworld hw = (ihelloworld) factory.getbean ("Helloworldbean");
    Log.info (hw.getcontent ("Luoshifei"));
 }
}

Related Article

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.