[英]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.