在java类中创建了简单的java spring+hibernate应用程序,并带有注解,但当我尝试启动project时,会出现如下错误:
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?
和
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?
我认为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;
}
}
正如我在stacktrace中看到的,spring正在尝试创建hibernatemanager,而datasourcebean的创建还没有完成。我怎样才能解决这个问题?
暂无答案!
目前还没有任何答案,快来回答吧!