Java loads a resource file from the Classpath __java the resource file from the Classpath

Source: Internet
Author: User

Java's world is inseparable from resource profiles, like the spring framework, and so are heavily dependent on resource profiles, and spring also encapsulates the resource class to handle the loading of resource files, where the spring load resource files are not carefully analyzed, mainly analyzing the Java The ResourceBundle class loads the resource configuration file.

In fact, the implementation of this class is analyzed, and its rationale, like spring, is to use ClassLoader to read system resource (Systemresource) files.


  ResourceBundle bundle = Resourcebundle.getbundle (Propfile, locale.english);

This is the unified resource loading method, and the property file is loaded by the

System.out.println (Bundle.getclass ());//Class Java.util.PropertyResourceBundle


Java.util.PropertyResourceBundle This implementation class is implemented, the implementation class mainly reads the resource (from the specified file), loads the file to

Java.util.Properties.

Read key code to load file contents:

@SuppressWarnings ({"Unchecked", "rawtypes"}) public
    propertyResourceBundle (InputStream stream) throws IOException {
        Properties Properties = new properties ();
        Properties.load (stream);
        Lookup = new HashMap (properties);
    }


@SuppressWarnings ({"Unchecked", "rawtypes"}) public
    propertyresourcebundle (Reader reader) throws IOException {
        Properties Properties = new properties ();
        Properties.load (reader);
        Lookup = new HashMap (properties);
    }


However, the operation of locating a resource file depends on the code of the abstract class ResourceBundle:

/** * A Wrapper of Classloader.getsystemclassloader (). * Private static Class Rbclassloader extends ClassLoader {private static final rbclassloader INSTANCE = Acce Sscontroller.doprivileged (New privilegedaction<rbclassloader> () {Publi
                        C Rbclassloader Run () {return new Rbclassloader ();
        }
                    });

        private static final ClassLoader loader = Classloader.getsystemclassloader ();
            Private Rbclassloader () {} public class<?> LoadClass (String name) throws ClassNotFoundException {
            if (loader!= null) {return loader.loadclass (name);
        return Class.forName (name); Public URL getresource (String name) {if (loader!= null) {return LOADER.GETRESOURC
            E (name);
 return Classloader.getsystemresource (name);       Public InputStream getResourceAsStream (String name) {if (loader!= null) {R
            Eturn Loader.getresourceasstream (name);
        return Classloader.getsystemresourceasstream (name); }
    }

locating a resource fileThe key to loading resources is
GetResource
<pre name= "code" class= "Java" >getresourceasstream
These methods.

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.