Как правильно обращаться к создаваемым бинам внутри класса конфигурации если один бин зависит от другого? Вот такой пример приводит к ошибке «Error creating bean with name 'sessionFactoryBean': Requested bean is currently in creation: Is there an unresolvable circular reference?»
@Configuration
@PropertySource("classpath:properties/hibernate.properties")
public abstract class HibernateConfig {
@Autowired
Environment env;
@Autowired
DataSource dataSource;
@Bean(name = "sessionFactoryBean")
public LocalSessionFactoryBean getSessionFactory()
{
LocalSessionFactoryBean fb = new LocalSessionFactoryBean();
fb.setDataSource(dataSource);
fb.setHibernateProperties(new Properties() {
private static final long serialVersionUID = 1L;
{
setProperty("hibernate.show_sql",
env.getProperty("hibernate.show_sql"));
setProperty("hibernate.format_sql",
env.getProperty("hibernate.format_sql"));
setProperty("hibernate.use_sql_comments",
env.getProperty("hibernate.use_sql_comments"));
setProperty("hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
setProperty("hibernate.dialect",
env.getProperty("hibernate.dialect"));
}});
return fb;
}
@Autowired
LocalSessionFactoryBean sessionFactoryBean;
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager()
{
HibernateTransactionManager htm = new HibernateTransactionManager();
htm.setSessionFactory(sessionFactoryBean.getObject());
return htm;
}
}