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.