![](/img/trans.png)
[英]How Hibernate SessionFactory autowired using LocalSessionFactoryBean?
[英]Spring 5 + Hibernate 5 : How SessionFactory is getting Autowired with LocalSessionFactoryBean
我正在使用 Spring MVC 5.0.8 和 Hibernate 5.2.11 开发 REST api
我创建了AppConfig
class,其中我创建了返回类型为LocalSessionFactoryBean
的getSessionFactory()
方法
@Bean
public LocalSessionFactoryBean getSessionFactory() {
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
factoryBean.setDataSource(dataSource());
factoryBean.setPackagesToScan("saptarsi.auditdb.model");
factoryBean.setHibernateProperties(hibernateProperties());
return factoryBean;
}
在DaoImpl
class 里面我有自动装配的SessionFactory
@Repository
public class LOcaldbDaoImpl implements LocaldbDao {
@Autowired
private SessionFactory sessionFactory;
private Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
@Override
public void getAllApiDetails(HttpRequestEntity<ApiDetailsFilterDto> requestEntityDto) {
}
}
一切正常
但我想知道 SessionFactory 是如何自动装配的。
因为我没有返回factoryBean.getObject()
,它负责返回SessionFactory
类型 object。
@Autowire
将在 Bean 工厂中查找SessionFactory
类型。
那么自动装配是如何发生的呢?
因为在初始化LocalSessionFactoryBean
之后,调用了buildSessionFactory
方法。 链接到调用。
protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
return (this.bootstrapExecutor != null ? sfb.buildSessionFactory(this.bootstrapExecutor) :
sfb.buildSessionFactory());
}
之后,SessionFactory bean 将在 ApplicationContext 中。
所有 Spring bean 都加载到ApplicationContext
中。 尝试在这里阅读更多https://docs.spring.io/spring/docs/1.2.x/reference/beans.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.