最近简略看了一下hibernate的源代码,比较晕,不过也看出了大概,
我看源代码喜欢模仿着写,这样有助理解,写的比较简陋,估计一段时间也不会再更新了,
先贴这,
把总结写出来,边看边写吧,先写初始化部分。
Configuration cfg = new Configuration();
SessionFactory factory = cfg.buildSessionFactory();
Session s = factory.openSession();
Transaction tx = s.beginTransaction();
上边是hibernate 自带例子里边的调用顺序,
就采用流水账写法,跟着这几句代码,一个方法一个方法的走。
Configuration cfg = new Configuration();
protected Configuration(SettingsFactory settingsFactory) {
this.settingsFactory = settingsFactory;
reset();
}
public Configuration() {
this( new SettingsFactory() );
}
显示new Configuration 类, 从上边实现可以看出,要先new 一个 SettingsFactory
那就先看SettingsFactory
public class SettingsFactory implements Serializable {
public static final String DEF_CACHE_REG_FACTORY = NoCachingRegionFactory.class.getName();
private static final Logger log = LoggerFactory.getLogger(SettingsFactory.class);
protected SettingsFactory() {
}
public Settings buildSettings(Properties props) {
Settings settings = new Settings();
构造函数为空,那就回头接着看Configuration
reset() 方法,
protected void reset() {
classes = new HashMap();
imports = new HashMap();
collections = new HashMap();
tables = new TreeMap();
namedQueries = new HashMap();
namedSqlQueries = new HashMap();
sqlResultSetMappings = new HashMap();
xmlHelper = new XMLHelper();
typeDefs = new HashMap();
propertyReferences = new ArrayList();
secondPasses = new ArrayList();
interceptor = EmptyInterceptor.INSTANCE;
properties = Environment.getProperties();
entityResolver = XMLHelper.DEFAULT_DTD_RESOLVER;
eventListeners = new EventListeners();
filterDefinitions = new HashMap();
// extendsQueue = new ArrayList();
extendsQueue = new HashMap();
auxiliaryDatabaseObjects = new ArrayList();
tableNameBinding = new HashMap();
columnNameBindingPerTable = new HashMap();
namingStrategy = DefaultNamingStrategy.INSTANCE;
sqlFunctions = new HashMap();
}
properties = Environment.getProperties();
看下他的实现
Environment.java
public static Properties getProperties() {
Properties copy = new Properties();
copy.putAll(GLOBAL_PROPERTIES);
return copy;
}
函数返回的,实际就是 GLOBAL_PROPERTIES,
那么他是在哪里被初始化的呢?
原来这个类里边有个静态代码块
static {
log.info("Hibernate " + VERSION);
RENAMED_PROPERTIES.put( "hibernate.cglib.use_reflection_optimizer", USE_REFLECTION_OPTIMIZER );
ISOLATION_LEVELS.put( new Integer(Connection.TRANSACTION_NONE), "NONE" );
ISOLATION_LEVELS.put( new Integer(Connection.TRANSACTION_READ_UNCOMMITTED), "READ_UNCOMMITTED" );
ISOLATION_LEVELS.put( new Integer(Connection.TRANSACTION_READ_COMMITTED), "READ_COMMITTED" );
ISOLATION_LEVELS.put( new Integer(Connection.TRANSACTION_REPEATABLE_READ), "REPEATABLE_READ" );
ISOLATION_LEVELS.put( new Integer(Connection.TRANSACTION_SERIALIZABLE), "SERIALIZABLE" );
GLOBAL_PROPERTIES = new Properties();
//Set USE_REFLECTION_OPTIMIZER to false to fix HHH-227
GLOBAL_PROPERTIES.setProperty( USE_REFLECTION_OPTIMIZER, Boolean.FALSE.toString() );
try {
InputStream stream = ConfigHelper.getResourceAsStream("/hibernate.properties");
try {
GLOBAL_PROPERTIES.load(stream);
log.info( "loaded properties from resource hibernate.properties: " + PropertiesHelper.maskOut(GLOBAL_PROPERTIES, PASS) );
}
catch (Exception e) {
log.error("problem loading properties from hibernate.properties");
}
finally {
try{
stream.close();
}
catch (IOException ioe){
log.error("could not close stream on hibernate.properties", ioe);
}
}
}
catch (HibernateException he) {
log.info("hibernate.properties not found");
}
try {
GLOBAL_PROPERTIES.putAll( System.getProperties() );
}
catch (SecurityException se) {
log.warn("could not copy system properties, system properties will be ignored");
}
verifyProperties(GLOBAL_PROPERTIES);
ENABLE_BINARY_STREAMS = PropertiesHelper.getBoolean(USE_STREAMS_FOR_BINARY, GLOBAL_PROPERTIES);
ENABLE_REFLECTION_OPTIMIZER = PropertiesHelper.getBoolean(USE_REFLECTION_OPTIMIZER, GLOBAL_PROPERTIES);
if (ENABLE_BINARY_STREAMS) {
log.info("using java.io streams to persist binary types");
}
if (ENABLE_REFLECTION_OPTIMIZER) {
log.info("using bytecode reflection optimizer");
}
BYTECODE_PROVIDER_INSTANCE = buildBytecodeProvider( GLOBAL_PROPERTIES );
boolean getGeneratedKeysSupport;
try {
Statement.class.getMethod("getGeneratedKeys", null);
getGeneratedKeysSupport = true;
}
catch (NoSuchMethodException nsme) {
getGeneratedKeysSupport = false;
}
JVM_SUPPORTS_GET_GENERATED_KEYS = getGeneratedKeysSupport;
if (!JVM_SUPPORTS_GET_GENERATED_KEYS) log.info("JVM does not support Statement.getGeneratedKeys()");
boolean linkedHashSupport;
try {
Class.forName("java.util.LinkedHashSet");
linkedHashSupport = true;
}
catch (ClassNotFoundException cnfe) {
linkedHashSupport = false;
}
JVM_SUPPORTS_LINKED_HASH_COLLECTIONS = linkedHashSupport;
if (!JVM_SUPPORTS_LINKED_HASH_COLLECTIONS) log.info("JVM does not support LinkedHasMap, LinkedHashSet - ordered maps and sets disabled");
JVM_HAS_TIMESTAMP_BUG = new Timestamp(123456789).getTime() != 123456789;
if (JVM_HAS_TIMESTAMP_BUG) log.info("using workaround for JVM bug in java.sql.Timestamp");
Timestamp t = new Timestamp(0);
t.setNanos(5 * 1000000);
JVM_HAS_JDK14_TIMESTAMP = t.getTime() == 5;
if (JVM_HAS_JDK14_TIMESTAMP) {
log.info("using JDK 1.4 java.sql.Timestamp handling");
}
else {
log.info("using pre JDK 1.4 java.sql.Timestamp handling");
}
}
GLOBAL_PROPERTIES 的初始化就在这里,主要是这几句,
GLOBAL_PROPERTIES = new Properties();
GLOBAL_PROPERTIES = new Properties();
InputStream stream = ConfigHelper.getResourceAsStream("/hibernate.properties");
try {
GLOBAL_PROPERTIES.load(stream);
("/hibernate.properties") 就是默认的配置文件了,
至于 static 块里的其他代码,就暂时不看了。
到了这里,hibernate的配置文件,就加载到hibernate框架里了。