Created simple java-spring+hibernate application with config in java-class with annotations, but when I'm trying to launch project get errors like:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in com.company.configuration.AppConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTransactionManager' defined in com.company.configuration.AppConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.transaction.PlatformTransactionManager]: Factory method 'hibernateTransactionManager' threw exception; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'hibernateTransactionManager': Requested bean is currently in creation: Is there an unresolvable circular reference?
and
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTransactionManager' defined in com.company.configuration.AppConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.transaction.PlatformTransactionManager]: Factory method 'hibernateTransactionManager' threw exception; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'hibernateTransactionManager': Requested bean is currently in creation: Is there an unresolvable circular reference?
I suppose that something is wrong with app config:
@Configuration
@PropertySource("classpath:appConfig.properties")
@ComponentScan("com.company.department_app")
@Transactional
@EnableTransactionManagement
@EnableWebMvc
public class AppConfig {
@Autowired
private Environment environment;
@Bean
public DataSource dataSource() {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
try {
dataSource.setDriverClass(environment.getProperty("db.driver"));
dataSource.setJdbcUrl(environment.getProperty("db.url"));
dataSource.setUser(environment.getProperty("db.username"));
dataSource.setPassword(environment.getProperty("db.password"));
} catch (Exception e) {
System.out.println("[DB EXCEPTION] : ");
e.printStackTrace();
throw new RuntimeException();
}
return dataSource;
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan("com.company.department_app.entity");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public PlatformTransactionManager hibernateTransactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
private Properties hibernateProperties() {
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", environment.getProperty("hibernate.dialect"));
hibernateProperties.setProperty("hibernate.show_sql", environment.getProperty("hibernate.show_sql"));
return hibernateProperties;
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
As I see from stacktrace spring is trying to create hibernateManager while creation of datasource bean hasn't been finished. How can I solve this?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…