|
26 | 26 | import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
27 | 27 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
28 | 28 | import org.springframework.lang.Nullable;
|
| 29 | +import org.springframework.util.Assert; |
29 | 30 | import org.springframework.util.ConcurrentReferenceHashMap;
|
30 | 31 |
|
31 | 32 | /**
|
32 | 33 | * Spring's implementation of Hibernate 5.3's {@link BeanContainer} SPI,
|
33 | 34 | * delegating to a Spring {@link ConfigurableListableBeanFactory}.
|
34 | 35 | *
|
| 36 | + * <p>Auto-configured by {@link LocalSessionFactoryBean#setBeanFactory}, |
| 37 | + * programmatically supported via {@link LocalSessionFactoryBuilder#setBeanContainer}, |
| 38 | + * and manually configurable through a "hibernate.resource.beans.container" entry |
| 39 | + * in JPA properties, e.g.: |
| 40 | + * |
| 41 | + * <pre class="code"> |
| 42 | + * <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> |
| 43 | + * ... |
| 44 | + * <property name="jpaPropertyMap"> |
| 45 | + * <map> |
| 46 | + * <entry key="hibernate.resource.beans.container"> |
| 47 | + * <bean class="org.springframework.orm.hibernate5.SpringBeanContainer"/> |
| 48 | + * </entry> |
| 49 | + * </map> |
| 50 | + * </property> |
| 51 | + * </bean></pre> |
| 52 | + * |
| 53 | + * Or in Java-based JPA configuration: |
| 54 | + * |
| 55 | + * <pre class="code"> |
| 56 | + * LocalContainerEntityManagerFactoryBean emfb = ... |
| 57 | + * emfb.getJpaPropertyMap().put(AvailableSettings.BEAN_CONTAINER, new SpringBeanContainer(beanFactory)); |
| 58 | + * </pre> |
| 59 | + * |
| 60 | + * Please note that Spring's {@link LocalSessionFactoryBean} is an immediate alternative |
| 61 | + * to {@link org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean} for common |
| 62 | + * JPA purposes: In particular with Hibernate 5.3, the Hibernate {@code SessionFactory} |
| 63 | + * will natively expose the JPA {@code EntityManagerFactory} interface as well, and |
| 64 | + * Hibernate {@code BeanContainer} integration will be registered out of the box. |
| 65 | + * |
35 | 66 | * @author Juergen Hoeller
|
36 | 67 | * @since 5.1
|
| 68 | + * @see LocalSessionFactoryBean#setBeanFactory |
| 69 | + * @see LocalSessionFactoryBuilder#setBeanContainer |
| 70 | + * @see org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean#setJpaPropertyMap |
| 71 | + * @see org.hibernate.cfg.AvailableSettings#BEAN_CONTAINER |
37 | 72 | */
|
38 |
| -final class SpringBeanContainer implements BeanContainer { |
| 73 | +public final class SpringBeanContainer implements BeanContainer { |
39 | 74 |
|
40 | 75 | private final ConfigurableListableBeanFactory beanFactory;
|
41 | 76 |
|
42 | 77 | private final Map<Object, SpringContainedBean<?>> beanCache = new ConcurrentReferenceHashMap<>();
|
43 | 78 |
|
44 | 79 |
|
| 80 | + /** |
| 81 | + * Instantiate a new SpringBeanContainer for the given bean factory. |
| 82 | + * @param beanFactory the Spring bean factory to delegate to |
| 83 | + */ |
45 | 84 | public SpringBeanContainer(ConfigurableListableBeanFactory beanFactory) {
|
| 85 | + Assert.notNull(beanFactory, "ConfigurableListableBeanFactory is required"); |
46 | 86 | this.beanFactory = beanFactory;
|
47 | 87 | }
|
48 | 88 |
|
|
0 commit comments