@ -26,23 +26,63 @@ import org.hibernate.resource.beans.spi.BeanInstanceProducer;
@@ -26,23 +26,63 @@ import org.hibernate.resource.beans.spi.BeanInstanceProducer;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory ;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory ;
import org.springframework.lang.Nullable ;
import org.springframework.util.Assert ;
import org.springframework.util.ConcurrentReferenceHashMap ;
/ * *
* Spring ' s implementation of Hibernate 5 . 3 ' s { @link BeanContainer } SPI ,
* delegating to a Spring { @link ConfigurableListableBeanFactory } .
*
* < p > Auto - configured by { @link LocalSessionFactoryBean # setBeanFactory } ,
* programmatically supported via { @link LocalSessionFactoryBuilder # setBeanContainer } ,
* and manually configurable through a "hibernate.resource.beans.container" entry
* in JPA properties , e . g . :
*
* < pre class = "code" >
* & lt ; bean id = "entityManagerFactory" class = "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" & gt ;
* . . .
* & lt ; property name = "jpaPropertyMap" & gt ;
* & lt ; map >
* & lt ; entry key = "hibernate.resource.beans.container" & gt ;
* & lt ; bean class = "org.springframework.orm.hibernate5.SpringBeanContainer" / & gt ;
* & lt ; / entry & gt ;
* & lt ; / map & gt ;
* & lt ; / property & gt ;
* & lt ; / bean & gt ; < / pre >
*
* Or in Java - based JPA configuration :
*
* < pre class = "code" >
* LocalContainerEntityManagerFactoryBean emfb = . . .
* emfb . getJpaPropertyMap ( ) . put ( AvailableSettings . BEAN_CONTAINER , new SpringBeanContainer ( beanFactory ) ) ;
* < / pre >
*
* Please note that Spring ' s { @link LocalSessionFactoryBean } is an immediate alternative
* to { @link org . springframework . orm . jpa . LocalContainerEntityManagerFactoryBean } for common
* JPA purposes : In particular with Hibernate 5 . 3 , the Hibernate { @code SessionFactory }
* will natively expose the JPA { @code EntityManagerFactory } interface as well , and
* Hibernate { @code BeanContainer } integration will be registered out of the box .
*
* @author Juergen Hoeller
* @since 5 . 1
* @see LocalSessionFactoryBean # setBeanFactory
* @see LocalSessionFactoryBuilder # setBeanContainer
* @see org . springframework . orm . jpa . LocalContainerEntityManagerFactoryBean # setJpaPropertyMap
* @see org . hibernate . cfg . AvailableSettings # BEAN_CONTAINER
* /
final class SpringBeanContainer implements BeanContainer {
public final class SpringBeanContainer implements BeanContainer {
private final ConfigurableListableBeanFactory beanFactory ;
private final Map < Object , SpringContainedBean < ? > > beanCache = new ConcurrentReferenceHashMap < > ( ) ;
/ * *
* Instantiate a new SpringBeanContainer for the given bean factory .
* @param beanFactory the Spring bean factory to delegate to
* /
public SpringBeanContainer ( ConfigurableListableBeanFactory beanFactory ) {
Assert . notNull ( beanFactory , "ConfigurableListableBeanFactory is required" ) ;
this . beanFactory = beanFactory ;
}