简体   繁体   English

Spring 5 + Hibernate 5:SessionFactory 如何与 LocalSessionFactoryBean 自动装配

[英]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,其中我创建了返回类型为LocalSessionFactoryBeangetSessionFactory()方法

    @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 SessionFactoryDaoImpl 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.

相关问题 Hibernate SessionFactory如何使用LocalSessionFactoryBean自动连线? - How Hibernate SessionFactory autowired using LocalSessionFactoryBean? 即使没有可用的 SessionFactory 实例,Spring @Autowired 如何绑定 SessionFactory 对象 - How Spring @Autowired binds the SessionFactory object even if there is no SessionFactory instance available 如何从 org.springframework.orm.hibernate4.LocalSessionFactoryBean 获取 Hibernate SessionFactory? - How to get the Hibernate SessionFactory from org.springframework.orm.hibernate4.LocalSessionFactoryBean? 在 Spring Boot 2.0/Hibernate 5 中获取 SessionFactory - Getting a SessionFactory in Spring Boot 2.0 / Hibernate 5 Spring 4-Hibernate 5-创建LocalSessionFactoryBean时出错 - Spring 4 - Hibernate 5 - Error Creating LocalSessionFactoryBean Java Spring + Hibernate LocalSessionFactoryBean无法正常工作 - Java Spring + Hibernate LocalSessionFactoryBean not working 如何为Spring 3 / Hibernate 4 LocalSessionFactoryBean设置事件监听器? - How do I set event listeners for my Spring 3 / Hibernate 4 LocalSessionFactoryBean? 无法从 org.springframework.orm.hibernate5.LocalSessionFactoryBean 实例化 sessionFactory - Failed to instantiate sessionFactory From org.springframework.orm.hibernate5.LocalSessionFactoryBean Spring @Autowired SessionFactory始终为null - Spring @Autowired SessionFactory is always null Spring 4 + Hibernate 4:sessionFactory为空 - Spring 4 + Hibernate 4: sessionFactory is null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM