What is the correct way to use factory beans in @Configuration classes?
Suppose I have the following for SessionFactory:
@Bean
public AnnotationSessionFactoryBean sessionFactory() {
AnnotationSessionFactoryBean factory = new AnnotationSessionFactoryBean();
// set up properties etc.
return factory;
}
Right now this method returns a bean factory which does not implement SessionFactory. If I use it in another bean with @Autowired as SessionFactory, it works just fine and obtains it from bean factory:
@Controller
public class MyController {
@Autowired SessionFactory sessionFactory;
// ...
}
I guess that's OK.
However, it becomes an issue if I want to use the SessionFactory in the same configuration class:
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager man = new HibernateTransactionManager();
// Ideal - doesn't work because sessionFactory() return type doesn't match:
// SessionFactory sessionFactory = sessionFactory();
// Is this one correct?
// SessionFactory sessionFactory = (SessionFactory) sessionFactory().getObject();
man.setSessionFactory(sessionFactory);
return man;
}
What is The Right Way to implement this kind of dependency?