首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

1 Configuration类 之 1_Configuration对象

2012-10-15 
一Configuration类 之 1_Configuration对象用过Hibernate的朋友都知道下面代码的意思,不知道的话,看这篇文

一 Configuration类 之 1_Configuration对象
用过Hibernate的朋友都知道下面代码的意思,不知道的话,看这篇文章也没意思了!
 
启动代码: 
         Configuration cfg=new Configuration();
cfg.addClass(User.class);
cfg.configure();
SchemaExport export=new SchemaExport(cfg);
     export.create(true, true);


(1)实例化一个Configuration对象
Configuration cfg=new Configuration();
       --->
       public Configuration() {
this( new SettingsFactory() );
  }
--->
           this( new SettingsFactory() );相当于调用
protected Configuration(SettingsFactory settingsFactory) {
          this.settingsFactory = settingsFactory;
           reset();
          }

Configuration是需要将配置信息的设置委托给SettingsFactory, SettingsFactory拥有设置配置信息的方法,比如说,public Settings buildSettings(Properties props)

返回的Settings类:存放系统配置信息转换的对象,比如从Properties得到的信息,转换成对象,然后存入Settings对象。
         (注意:settings是Settings类对象,props是Properties类对象)
String sessionFactoryName = props.getProperty(Environment.SESSION_FACTORY_NAME);
settings.setSessionFactoryName(sessionFactoryName);

Configuration只做自己应该做的事情就是从properties *.cfg.xml读取系统的参数.

     ---> 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();
}

reset():
除了申请一些HashMap对象,ArrayList对象,TreeMap对象,用来存放XML解释后的信息外,还有XMLHelper用于帮助读取XML,
  EmptyInterceptor用于扩展自定义拦截器。
EventListeners,一个支持对所有session事件的监听器
DefaultNamingStrategy,默认的命名策略。
还有最重要的
properties = Environment.getProperties();
关键是初始化hibernate.properties文件的信息.

热点排行