|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
40 | 40 | import org.springframework.beans.factory.DisposableBean;
|
41 | 41 | import org.springframework.beans.factory.FactoryBean;
|
42 | 42 | import org.springframework.beans.factory.InitializingBean;
|
| 43 | +import org.springframework.beans.factory.SmartInitializingSingleton; |
43 | 44 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
44 | 45 | import org.springframework.context.ResourceLoaderAware;
|
| 46 | +import org.springframework.core.InfrastructureProxy; |
45 | 47 | import org.springframework.core.io.ClassPathResource;
|
46 | 48 | import org.springframework.core.io.Resource;
|
47 | 49 | import org.springframework.core.io.ResourceLoader;
|
|
79 | 81 | * @see org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
|
80 | 82 | */
|
81 | 83 | public class LocalSessionFactoryBean extends HibernateExceptionTranslator
|
82 |
| - implements FactoryBean<SessionFactory>, ResourceLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean { |
| 84 | + implements FactoryBean<SessionFactory>, ResourceLoaderAware, BeanFactoryAware, |
| 85 | + InitializingBean, SmartInitializingSingleton, DisposableBean { |
83 | 86 |
|
84 | 87 | @Nullable
|
85 | 88 | private DataSource dataSource;
|
@@ -390,6 +393,8 @@ public void setPackagesToScan(String... packagesToScan) {
|
390 | 393 | * then block until Hibernate's bootstrapping completed, if not ready by then.
|
391 | 394 | * For maximum benefit, make sure to avoid early {@code SessionFactory} calls
|
392 | 395 | * in init methods of related beans, even for metadata introspection purposes.
|
| 396 | + * <p>As of 6.2, Hibernate initialization is enforced before context refresh |
| 397 | + * completion, waiting for asynchronous bootstrapping to complete by then. |
393 | 398 | * @since 4.3
|
394 | 399 | * @see LocalSessionFactoryBuilder#buildSessionFactory(AsyncTaskExecutor)
|
395 | 400 | */
|
@@ -600,6 +605,14 @@ public void afterPropertiesSet() throws IOException {
|
600 | 605 | this.sessionFactory = buildSessionFactory(sfb);
|
601 | 606 | }
|
602 | 607 |
|
| 608 | + @Override |
| 609 | + public void afterSingletonsInstantiated() { |
| 610 | + // Enforce completion of asynchronous Hibernate initialization before context refresh completion. |
| 611 | + if (this.sessionFactory instanceof InfrastructureProxy proxy) { |
| 612 | + proxy.getWrappedObject(); |
| 613 | + } |
| 614 | + } |
| 615 | + |
603 | 616 | /**
|
604 | 617 | * Subclasses can override this method to perform custom initialization
|
605 | 618 | * of the SessionFactory instance, creating it via the given Configuration
|
|
0 commit comments