One, configuration file loading
1,configuration
If it is not annoation, you can use configuration configuration = new configuration ();
Using Annoation, you can use configuration configuration = new Annotationconfiguration ();
2, loading, using the Onfiguration configure method according to the method parameters can have a few load modes:
(1) Configure ();
Eating method will go to classpath under the search for our configuration file
In fact, the Configure ("/hibernate.cfg.xml") was invoked; the Configure (String resource);
(2) Configure (String Resource);
The most commonly used method of the square, in fact, calls the Doconfigure (InputStream stream, String resourcename);
Explain a little internal code:
Confighelper.getresourceasstream (Resource);
Java code
String stripped = resource.startsWith("/") ?
resource.substring(1) : resource;
InputStream stream = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader!=null) {
stream = classLoader.getResourceAsStream( stripped );
}
if ( stream == null ) {
stream = Environment.class.getResourceAsStream( resource );
}
if ( stream == null ) {
stream = Environment.class.getClassLoader().getResourceAsStream( stripped );
}
if ( stream == null ) {
throw new HibernateException( resource + " not found" );
}
return stream;
The first line of high number we "/hibernate.cfg.xml" can remove the previous "/" which calls the Thread.CurrentThread (). Getcontextclassloader (); It's usually our appclassloader.
by stream = Environment.class.getResourceAsStream (resource); can see our "/ Hibernate.cfg.xml can also be placed in the same directory as and environment to our code in the use of (3), (4), (5) can be placed in the load class package or other
(3) Configure (URL URL);
Doconfigure (Url.openstream (), url.tostring ()); Call (5)
(4) Configure (File configfile);
Doconfigure (New FileInputStream (ConfigFile), configfile.tostring ()); Call (5)
(5) Doconfigure (InputStream stream, String resourcename);
Use DOM4J to parse the file as document and then
Xmlhelper.createsaxreader (resourcename, errors, Entityresolver)
. Read (new InputSource (stream));
Fall out of Use
(6) Configure (document document);
In this method, all configuration information and mapping class or HB files are parsed.