[英]Spring 5 + Hibernate 5 : How SessionFactory is getting Autowired with LocalSessionFactoryBean
I am developing REST apis using Spring MVC 5.0.8 and Hibernate 5.2.11我正在使用 Spring MVC 5.0.8 和 Hibernate 5.2.11 开发 REST api
I have created AppConfig
class,in which I have created getSessionFactory()
method with return type LocalSessionFactoryBean
我创建了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;
}
And Inside DaoImpl
class I have autowired SessionFactory
在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) {
}
}
And everything is working fine一切正常
But I want to know how SessionFactory is getting autowired.但我想知道 SessionFactory 是如何自动装配的。
Because I am not returning factoryBean.getObject()
,which is responsible to return SessionFactory
type object.因为我没有返回factoryBean.getObject()
,它负责返回SessionFactory
类型 object。
And @Autowire
will look for SessionFactory
type in Bean factory. @Autowire
将在 Bean 工厂中查找SessionFactory
类型。
So how Autowiring is happening?那么自动装配是如何发生的呢?
Because after you initialized the LocalSessionFactoryBean
, the buildSessionFactory
method was called.因为在初始化LocalSessionFactoryBean
之后,调用了buildSessionFactory
方法。 Link to calling . 链接到调用。
protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
return (this.bootstrapExecutor != null ? sfb.buildSessionFactory(this.bootstrapExecutor) :
sfb.buildSessionFactory());
}
After that, SessionFactory bean will be in the ApplicationContext.之后,SessionFactory bean 将在 ApplicationContext 中。
All Spring beans was load in ApplicationContext
.所有 Spring bean 都加载到ApplicationContext
中。 Try to read here more https://docs.spring.io/spring/docs/1.2.x/reference/beans.html尝试在这里阅读更多https://docs.spring.io/spring/docs/1.2.x/reference/beans.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.