Spring源代码解析89hibernateacegi.docx
- 文档编号:28773444
- 上传时间:2023-07-19
- 格式:DOCX
- 页数:42
- 大小:30.92KB
Spring源代码解析89hibernateacegi.docx
《Spring源代码解析89hibernateacegi.docx》由会员分享,可在线阅读,更多相关《Spring源代码解析89hibernateacegi.docx(42页珍藏版)》请在冰豆网上搜索。
Spring源代码解析89hibernateacegi
O/R工具出现之后,简化了许多复杂的信息持久化的开发。
Spring应用开发者可以通过Spring提供的O/R方案更方便的使用各种持久化工具,比如Hibernate;下面我们就Spring+Hibernate中的Spring实现做一个简单的剖析。
Spring对Hinberanate的配置是通过LocalSessionFactoryBean来完成的,这是一个工厂Bean的实现,在基类AbstractSessionFactoryBean中:
代码
1./**
2. * 这是FactoryBean需要实现的接口方法,直接取得当前的sessionFactory的值
3. */
4.public Object getObject() {
5. return this.sessionFactory;
6.}
这个值在afterPropertySet中定义:
代码
1.public void afterPropertiesSet() throws Exception {
2. //这个buildSessionFactory是通过配置信息得到SessionFactory的地方
3. SessionFactory rawSf = buildSessionFactory();
4. //这里使用了Proxy方法插入对getCurrentSession的拦截,得到和事务相关的session
5. this.sessionFactory = wrapSessionFactoryIfNecessary(rawSf);
6.}
我们先看看SessionFactory是怎样创建的,这个方法很长,包含了创建Hibernate的SessionFactory的详尽步骤:
代码
1.protected SessionFactory buildSessionFactory() throws Exception {
2. SessionFactory sf = null;
3.
4. // Create Configuration instance.
5. Configuration config = newConfiguration();
6.
7. //这里配置数据源,事务管理器,LobHander到Holder中,这个Holder是一个ThreadLocal变量,这样这些资源就和线程绑定了
8. if (this.dataSource !
= null) {
9. // Make given DataSource available for SessionFactory configuration.
10. configTimeDataSourceHolder.set(this.dataSource);
11. }
12.
13. if (this.jtaTransactionManager !
= null) {
14. // Make Spring-provided JTA TransactionManager available.
15. configTimeTransactionManagerHolder.set(this.jtaTransactionManager);
16. }
17.
18. if (this.lobHandler !
= null) {
19. // Make given LobHandler available for SessionFactory configuration.
20. // Do early because because mapping resource might refer to custom types.
21. configTimeLobHandlerHolder.set(this.lobHandler);
22. }
23.
24. //这里是使用Hibernate的各个属性的配置,这里使用了Configuration类来抽象这些数据
25. try {
26. // Set connection release mode "on_close" as default.
27. // This was the case for Hibernate 3.0; Hibernate 3.1 changed
28. // it to "auto" (i.e. "after_statement" or "after_transaction").
29. // However, for Spring's resource management (in particular for
30. // HibernateTransactionManager), "on_close" is the better default.
31. config.setProperty(Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.ON_CLOSE.toString());
32.
33. if (!
isExposeTransactionAwareSessionFactory()) {
34. // Not exposing a SessionFactory proxy with transaction-aware
35. // getCurrentSession() method -> set Hibernate 3.1 CurrentSessionContext
36. // implementation instead, providing the Spring-managed Session that way.
37. // Can be overridden by a custom value for corresponding Hibernate property.
38. config.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS,
39. "org.springframework.orm.hibernate3.SpringSessionContext");
40. }
41.
42. if (this.entityInterceptor !
= null) {
43. // Set given entity interceptor at SessionFactory level.
44. config.setInterceptor(this.entityInterceptor);
45. }
46.
47. if (this.namingStrategy !
= null) {
48. // Pass given naming strategy to Hibernate Configuration.
49. config.setNamingStrategy(this.namingStrategy);
50. }
51.
52. if (this.typeDefinitions !
= null) {
53. // Register specified Hibernate type definitions.
54. Mappings mappings = config.createMappings();
55. for (int i = 0; i < this.typeDefinitions.length; i++) {
56. TypeDefinitionBean typeDef = this.typeDefinitions[i];
57. mappings.addTypeDef(typeDef.getTypeName(), typeDef.getTypeClass(), typeDef.getParameters());
58. }
59. }
60.
61. if (this.filterDefinitions !
= null) {
62. // Register specified Hibernate FilterDefinitions.
63. for (int i = 0; i < this.filterDefinitions.length; i++) {
64. config.addFilterDefinition(this.filterDefinitions[i]);
65. }
66. }
67.
68. if (this.configLocations !
= null) {
69. for (int i = 0; i < this.configLocations.length; i++) {
70. // Load Hibernate configuration from given location.
71. config.configure(this.configLocations[i].getURL());
72. }
73. }
74.
75. if (this.hibernateProperties !
= null) {
76. // Add given Hibernate properties to Configuration.
77. config.addProperties(this.hibernateProperties);
78. }
79.
80. if (this.dataSource !
= null) {
81. boolean actuallyTransactionAware =
82. (this.useTransactionAwareDataSource || this.dataSource instanceof TransactionAwareDataSourceProxy);
83. // Set Spring-provided DataSource as Hibernate ConnectionProvider.
84. config.setProperty(Environment.CONNECTION_PROVIDER,
85. actuallyTransactionAware ?
86. TransactionAwareDataSourceConnectionProvider.class.getName() :
87. LocalDataSourceConnectionProvider.class.getName());
88. }
89.
90. if (this.jtaTransactionManager !
= null) {
91. // Set Spring-provided JTA TransactionManager as Hibernate property.
92. config.setProperty(
93. Environment.TRANSACTION_MANAGER_STRATEGY, LocalTransactionManagerLookup.class.getName());
94. }
95.
96. if (this.mappingLocations !
= null) {
97. // Register given Hibernate mapping definitions, contained in resource files.
98. for (int i = 0; i < this.mappingLocations.length; i++) {
99. config.addInputStream(this.mappingLocations[i].getInputStream());
100. }
101. }
102.
103. if (this.cacheableMappingLocations !
= null) {
104. // Register given cacheable Hibernate mapping definitions, read from the file system.
105. for (int i = 0; i < this.cacheableMappingLocations.length; i++) {
106. config.addCacheableFile(this.cacheableMappingLocations[i].getFile());
107. }
108. }
109.
110. if (this.mappingJarLocations !
= null) {
111. // Register given Hibernate mapping definitions, contained in jar files.
112. for (int i = 0; i < this.mappingJarLocations.length; i++) {
113. Resource resource = this.mappingJarLocations[i];
114. config.addJar(resource.getFile());
115. }
116. }
117.
118. if (this.mappingDirectoryLocations !
= null) {
119. // Register all Hibernate mapping definitions in the given directories.
120. for (int i = 0; i < this.mappingDirectoryLocations.length; i++) {
121. File file = this.mappingDirectoryLocations[i].getFile();
122. if (!
file.isDirectory()) {
123. throw new IllegalArgumentException(
124. "Mapping directory location [" + this.mappingDirectoryLocations[i] +
125. "] does not denote a directory");
126. }
127. config.addDirectory(file);
128. }
129. }
130.
131. if (this.entityCacheStrategies !
= null) {
132. // Register cache strategies for mapped entities.
133. for (Enumeration classNames = this.entityCacheStrategies.propertyNames(); classNames.hasMoreElements();) {
134. String className = (String) classNames.nextElement();
135. String[] strategyAndRegion =
136. StringUmaDelimitedListToStringArray(this.entityCacheStrategies.getProperty(className));
137. if (strategyAndRegion.length > 1) {
138. config.setCacheConcurrencyStrategy(className, strategyAndRegion[0], strategyAndRegion[1]);
139. }
140. else if (strategyAndRegion.length > 0) {
141. config.setCacheConcurrencyStrategy(className, strategyAndRegion[0]);
142. }
143. }
144. }
145.
146. if (this.collectionCacheStrategies !
= null) {
147. // Register cache strategies for mapped collections.
148. for (Enumeration collRoles = this.collectionCacheStrategies.propertyNames(); collRoles.hasMoreElements();) {
149. String collRole = (String) collRoles.nextElement();
150. String[] strategyAndRegion =
151. StringUmaDelimitedListToStringArray(this.collectionCacheStrategies.getProperty(collRole));
152. if (strategyAndRegion.length > 1) {
153. config.setCollectionCacheConcurrencyStrategy(collRole, strategyAndRegion[0], strategyAndRegion[1]);
154. }
155. else if (strategyAndRegion.length > 0) {
156. config.setCollectionCacheConcurrencyStrategy(collRole, strategyAndRegion[0]);
157. }
158. }
159. }
160.
161. if (this.eventListeners !
= null) {
162. // Register specified Hibernate event listeners.
163. for (Iterator it = th
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Spring 源代码 解析 89 hibernateacegi
![提示](https://static.bdocx.com/images/bang_tan.gif)