| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.mybatis.spring; |
| 17 | |
|
| 18 | |
import static org.springframework.util.Assert.notNull; |
| 19 | |
import static org.springframework.util.ObjectUtils.isEmpty; |
| 20 | |
import static org.springframework.util.StringUtils.hasLength; |
| 21 | |
import static org.springframework.util.StringUtils.tokenizeToStringArray; |
| 22 | |
|
| 23 | |
import java.io.IOException; |
| 24 | |
import java.sql.SQLException; |
| 25 | |
import java.util.Properties; |
| 26 | |
|
| 27 | |
import javax.sql.DataSource; |
| 28 | |
|
| 29 | |
import org.apache.ibatis.builder.xml.XMLConfigBuilder; |
| 30 | |
import org.apache.ibatis.builder.xml.XMLMapperBuilder; |
| 31 | |
import org.apache.ibatis.executor.ErrorContext; |
| 32 | |
import org.apache.ibatis.logging.Log; |
| 33 | |
import org.apache.ibatis.logging.LogFactory; |
| 34 | |
import org.apache.ibatis.mapping.DatabaseIdProvider; |
| 35 | |
import org.apache.ibatis.mapping.Environment; |
| 36 | |
import org.apache.ibatis.plugin.Interceptor; |
| 37 | |
import org.apache.ibatis.reflection.factory.ObjectFactory; |
| 38 | |
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory; |
| 39 | |
import org.apache.ibatis.session.Configuration; |
| 40 | |
import org.apache.ibatis.session.SqlSessionFactory; |
| 41 | |
import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 42 | |
import org.apache.ibatis.transaction.TransactionFactory; |
| 43 | |
import org.apache.ibatis.type.TypeHandler; |
| 44 | |
import org.mybatis.spring.transaction.SpringManagedTransactionFactory; |
| 45 | |
import org.springframework.beans.factory.FactoryBean; |
| 46 | |
import org.springframework.beans.factory.InitializingBean; |
| 47 | |
import org.springframework.context.ApplicationEvent; |
| 48 | |
import org.springframework.context.ApplicationListener; |
| 49 | |
import org.springframework.context.ConfigurableApplicationContext; |
| 50 | |
import org.springframework.context.event.ContextRefreshedEvent; |
| 51 | |
import org.springframework.core.NestedIOException; |
| 52 | |
import org.springframework.core.io.Resource; |
| 53 | |
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | 109 | public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> { |
| 73 | |
|
| 74 | 1 | private static final Log LOGGER = LogFactory.getLog(SqlSessionFactoryBean.class); |
| 75 | |
|
| 76 | |
private Resource configLocation; |
| 77 | |
|
| 78 | |
private Resource[] mapperLocations; |
| 79 | |
|
| 80 | |
private DataSource dataSource; |
| 81 | |
|
| 82 | |
private TransactionFactory transactionFactory; |
| 83 | |
|
| 84 | |
private Properties configurationProperties; |
| 85 | |
|
| 86 | 69 | private SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); |
| 87 | |
|
| 88 | |
private SqlSessionFactory sqlSessionFactory; |
| 89 | |
|
| 90 | |
|
| 91 | 69 | private String environment = SqlSessionFactoryBean.class.getSimpleName(); |
| 92 | |
|
| 93 | |
private boolean failFast; |
| 94 | |
|
| 95 | |
private Interceptor[] plugins; |
| 96 | |
|
| 97 | |
private TypeHandler<?>[] typeHandlers; |
| 98 | |
|
| 99 | |
private String typeHandlersPackage; |
| 100 | |
|
| 101 | |
private Class<?>[] typeAliases; |
| 102 | |
|
| 103 | |
private String typeAliasesPackage; |
| 104 | |
|
| 105 | |
private Class<?> typeAliasesSuperType; |
| 106 | |
|
| 107 | |
|
| 108 | |
private DatabaseIdProvider databaseIdProvider; |
| 109 | |
|
| 110 | |
private ObjectFactory objectFactory; |
| 111 | |
|
| 112 | |
private ObjectWrapperFactory objectWrapperFactory; |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public void setObjectFactory(ObjectFactory objectFactory) { |
| 121 | 1 | this.objectFactory = objectFactory; |
| 122 | 1 | } |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
public void setObjectWrapperFactory(ObjectWrapperFactory objectWrapperFactory) { |
| 131 | 1 | this.objectWrapperFactory = objectWrapperFactory; |
| 132 | 1 | } |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
public DatabaseIdProvider getDatabaseIdProvider() { |
| 141 | 0 | return databaseIdProvider; |
| 142 | |
} |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
public void setDatabaseIdProvider(DatabaseIdProvider databaseIdProvider) { |
| 152 | 3 | this.databaseIdProvider = databaseIdProvider; |
| 153 | 3 | } |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public void setPlugins(Interceptor[] plugins) { |
| 164 | 7 | this.plugins = plugins; |
| 165 | 7 | } |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
public void setTypeAliasesPackage(String typeAliasesPackage) { |
| 176 | 4 | this.typeAliasesPackage = typeAliasesPackage; |
| 177 | 4 | } |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
public void setTypeAliasesSuperType(Class<?> typeAliasesSuperType) { |
| 189 | 1 | this.typeAliasesSuperType = typeAliasesSuperType; |
| 190 | 1 | } |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
public void setTypeHandlersPackage(String typeHandlersPackage) { |
| 201 | 1 | this.typeHandlersPackage = typeHandlersPackage; |
| 202 | 1 | } |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
public void setTypeHandlers(TypeHandler<?>[] typeHandlers) { |
| 212 | 1 | this.typeHandlers = typeHandlers; |
| 213 | 1 | } |
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
public void setTypeAliases(Class<?>[] typeAliases) { |
| 223 | 1 | this.typeAliases = typeAliases; |
| 224 | 1 | } |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
public void setFailFast(boolean failFast) { |
| 236 | 0 | this.failFast = failFast; |
| 237 | 0 | } |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
public void setConfigLocation(Resource configLocation) { |
| 244 | 3 | this.configLocation = configLocation; |
| 245 | 3 | } |
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
public void setMapperLocations(Resource[] mapperLocations) { |
| 256 | 18 | this.mapperLocations = mapperLocations; |
| 257 | 18 | } |
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
public void setConfigurationProperties(Properties sqlSessionFactoryProperties) { |
| 265 | 0 | this.configurationProperties = sqlSessionFactoryProperties; |
| 266 | 0 | } |
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
public void setDataSource(DataSource dataSource) { |
| 284 | 68 | if (dataSource instanceof TransactionAwareDataSourceProxy) { |
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | 0 | this.dataSource = ((TransactionAwareDataSourceProxy) dataSource).getTargetDataSource(); |
| 290 | |
} else { |
| 291 | 68 | this.dataSource = dataSource; |
| 292 | |
} |
| 293 | 68 | } |
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
public void setSqlSessionFactoryBuilder(SqlSessionFactoryBuilder sqlSessionFactoryBuilder) { |
| 303 | 1 | this.sqlSessionFactoryBuilder = sqlSessionFactoryBuilder; |
| 304 | 1 | } |
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
public void setTransactionFactory(TransactionFactory transactionFactory) { |
| 321 | 2 | this.transactionFactory = transactionFactory; |
| 322 | 2 | } |
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
public void setEnvironment(String environment) { |
| 332 | 1 | this.environment = environment; |
| 333 | 1 | } |
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
@Override |
| 339 | |
public void afterPropertiesSet() throws Exception { |
| 340 | 69 | notNull(dataSource, "Property 'dataSource' is required"); |
| 341 | 67 | notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required"); |
| 342 | |
|
| 343 | 66 | this.sqlSessionFactory = buildSqlSessionFactory(); |
| 344 | 66 | } |
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
protected SqlSessionFactory buildSqlSessionFactory() throws IOException { |
| 356 | |
|
| 357 | |
Configuration configuration; |
| 358 | |
|
| 359 | 66 | XMLConfigBuilder xmlConfigBuilder = null; |
| 360 | 66 | if (this.configLocation != null) { |
| 361 | 2 | xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties); |
| 362 | 2 | configuration = xmlConfigBuilder.getConfiguration(); |
| 363 | |
} else { |
| 364 | 64 | if (LOGGER.isDebugEnabled()) { |
| 365 | 64 | LOGGER.debug("Property 'configLocation' not specified, using default MyBatis Configuration"); |
| 366 | |
} |
| 367 | 64 | configuration = new Configuration(); |
| 368 | 64 | configuration.setVariables(this.configurationProperties); |
| 369 | |
} |
| 370 | |
|
| 371 | 66 | if (this.objectFactory != null) { |
| 372 | 1 | configuration.setObjectFactory(this.objectFactory); |
| 373 | |
} |
| 374 | |
|
| 375 | 66 | if (this.objectWrapperFactory != null) { |
| 376 | 1 | configuration.setObjectWrapperFactory(this.objectWrapperFactory); |
| 377 | |
} |
| 378 | |
|
| 379 | 66 | if (hasLength(this.typeAliasesPackage)) { |
| 380 | 4 | String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage, |
| 381 | |
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); |
| 382 | 8 | for (String packageToScan : typeAliasPackageArray) { |
| 383 | 4 | configuration.getTypeAliasRegistry().registerAliases(packageToScan, |
| 384 | |
typeAliasesSuperType == null ? Object.class : typeAliasesSuperType); |
| 385 | 4 | if (LOGGER.isDebugEnabled()) { |
| 386 | 4 | LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases"); |
| 387 | |
} |
| 388 | |
} |
| 389 | |
} |
| 390 | |
|
| 391 | 66 | if (!isEmpty(this.typeAliases)) { |
| 392 | 2 | for (Class<?> typeAlias : this.typeAliases) { |
| 393 | 1 | configuration.getTypeAliasRegistry().registerAlias(typeAlias); |
| 394 | 1 | if (LOGGER.isDebugEnabled()) { |
| 395 | 1 | LOGGER.debug("Registered type alias: '" + typeAlias + "'"); |
| 396 | |
} |
| 397 | |
} |
| 398 | |
} |
| 399 | |
|
| 400 | 66 | if (!isEmpty(this.plugins)) { |
| 401 | 14 | for (Interceptor plugin : this.plugins) { |
| 402 | 7 | configuration.addInterceptor(plugin); |
| 403 | 7 | if (LOGGER.isDebugEnabled()) { |
| 404 | 7 | LOGGER.debug("Registered plugin: '" + plugin + "'"); |
| 405 | |
} |
| 406 | |
} |
| 407 | |
} |
| 408 | |
|
| 409 | 66 | if (hasLength(this.typeHandlersPackage)) { |
| 410 | 1 | String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage, |
| 411 | |
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); |
| 412 | 2 | for (String packageToScan : typeHandlersPackageArray) { |
| 413 | 1 | configuration.getTypeHandlerRegistry().register(packageToScan); |
| 414 | 1 | if (LOGGER.isDebugEnabled()) { |
| 415 | 1 | LOGGER.debug("Scanned package: '" + packageToScan + "' for type handlers"); |
| 416 | |
} |
| 417 | |
} |
| 418 | |
} |
| 419 | |
|
| 420 | 66 | if (!isEmpty(this.typeHandlers)) { |
| 421 | 2 | for (TypeHandler<?> typeHandler : this.typeHandlers) { |
| 422 | 1 | configuration.getTypeHandlerRegistry().register(typeHandler); |
| 423 | 1 | if (LOGGER.isDebugEnabled()) { |
| 424 | 1 | LOGGER.debug("Registered type handler: '" + typeHandler + "'"); |
| 425 | |
} |
| 426 | |
} |
| 427 | |
} |
| 428 | |
|
| 429 | 66 | if (xmlConfigBuilder != null) { |
| 430 | |
try { |
| 431 | 2 | xmlConfigBuilder.parse(); |
| 432 | |
|
| 433 | 2 | if (LOGGER.isDebugEnabled()) { |
| 434 | 2 | LOGGER.debug("Parsed configuration file: '" + this.configLocation + "'"); |
| 435 | |
} |
| 436 | 0 | } catch (Exception ex) { |
| 437 | 0 | throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex); |
| 438 | |
} finally { |
| 439 | 2 | ErrorContext.instance().reset(); |
| 440 | 2 | } |
| 441 | |
} |
| 442 | |
|
| 443 | 66 | if (this.transactionFactory == null) { |
| 444 | 65 | this.transactionFactory = new SpringManagedTransactionFactory(); |
| 445 | |
} |
| 446 | |
|
| 447 | 66 | configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource)); |
| 448 | |
|
| 449 | 66 | if (this.databaseIdProvider != null) { |
| 450 | |
try { |
| 451 | 0 | configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource)); |
| 452 | 0 | } catch (SQLException e) { |
| 453 | 0 | throw new NestedIOException("Failed getting a databaseId", e); |
| 454 | 0 | } |
| 455 | |
} |
| 456 | |
|
| 457 | 66 | if (!isEmpty(this.mapperLocations)) { |
| 458 | 32 | for (Resource mapperLocation : this.mapperLocations) { |
| 459 | 16 | if (mapperLocation == null) { |
| 460 | 1 | continue; |
| 461 | |
} |
| 462 | |
|
| 463 | |
try { |
| 464 | 15 | XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(), |
| 465 | |
configuration, mapperLocation.toString(), configuration.getSqlFragments()); |
| 466 | 15 | xmlMapperBuilder.parse(); |
| 467 | 0 | } catch (Exception e) { |
| 468 | 0 | throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e); |
| 469 | |
} finally { |
| 470 | 15 | ErrorContext.instance().reset(); |
| 471 | 15 | } |
| 472 | |
|
| 473 | 15 | if (LOGGER.isDebugEnabled()) { |
| 474 | 15 | LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'"); |
| 475 | |
} |
| 476 | |
} |
| 477 | |
} else { |
| 478 | 50 | if (LOGGER.isDebugEnabled()) { |
| 479 | 50 | LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found"); |
| 480 | |
} |
| 481 | |
} |
| 482 | |
|
| 483 | 66 | return this.sqlSessionFactoryBuilder.build(configuration); |
| 484 | |
} |
| 485 | |
|
| 486 | |
|
| 487 | |
|
| 488 | |
|
| 489 | |
@Override |
| 490 | |
public SqlSessionFactory getObject() throws Exception { |
| 491 | 69 | if (this.sqlSessionFactory == null) { |
| 492 | 29 | afterPropertiesSet(); |
| 493 | |
} |
| 494 | |
|
| 495 | 66 | return this.sqlSessionFactory; |
| 496 | |
} |
| 497 | |
|
| 498 | |
|
| 499 | |
|
| 500 | |
|
| 501 | |
@Override |
| 502 | |
public Class<? extends SqlSessionFactory> getObjectType() { |
| 503 | 206 | return this.sqlSessionFactory == null ? SqlSessionFactory.class : this.sqlSessionFactory.getClass(); |
| 504 | |
} |
| 505 | |
|
| 506 | |
|
| 507 | |
|
| 508 | |
|
| 509 | |
@Override |
| 510 | |
public boolean isSingleton() { |
| 511 | 133 | return true; |
| 512 | |
} |
| 513 | |
|
| 514 | |
|
| 515 | |
|
| 516 | |
|
| 517 | |
@Override |
| 518 | |
public void onApplicationEvent(ApplicationEvent event) { |
| 519 | 121 | if (failFast && event instanceof ContextRefreshedEvent) { |
| 520 | |
|
| 521 | 0 | this.sqlSessionFactory.getConfiguration().getMappedStatementNames(); |
| 522 | |
} |
| 523 | 121 | } |
| 524 | |
|
| 525 | |
} |