簡體   English   中英

如何從AnnotationSessionFactoryBean自動連接SessionFactory

[英]How to Autowire SessionFactory from AnnotationSessionFactoryBean

我正在基於注釋的Spring配置上工作,我也想使用Hibernate。 我有一個AnnotationSessionFactoryBean:

@Bean
public AnnotationSessionFactoryBean getSessionFactory() {
    AnnotationSessionFactoryBean annotationSessionFactoryBean = new AnnotationSessionFactoryBean();
    annotationSessionFactoryBean.setDataSource(getDataSource());
    annotationSessionFactoryBean.setHibernateProperties(getHibernateProperties());
    annotationSessionFactoryBean.setPackagesToScan("com.mobiusinversion.web");
    return annotationSessionFactoryBean;
}

但是現在在我的代碼中,如何在SessionFactory中自動裝配,如下所示:

@Transactional
@Repository
public class UserRepository {

    @Autowired
    private SessionFactory sessionFactory;

}

AnnotationSessionFactoryBean既是InitializingBean又是FactoryBean 這些是Spring作為bean生命周期的一部分處理的特殊接口。 InitializingBean將提供afterProperties設置以初始化Bean,而FactoryBean將提供getObject用於檢索Bean。 然后將該bean添加到上下文中。

AnnotationSessionFactoryBean產生一個SessionFactory bean,因此,您要做的就是自動裝配它

@Autowired
private SessionFactory sessionFactory;

在文檔中對此進行了全部解釋:

您還應該閱讀javadoc。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM