spring整合hibernate,项目出现bean创建失败异常

在整合Spring和Hibernate时遇到BeanCreationException,文章详细分析了可能的原因及解决方案,包括:版本不对应、组件扫描未开启、Hibernate配置错误、注解使用不当以及数据源配置错误等,并提供了具体的解决步骤。

在使用sring整合hibernate的时候,出现如下异常信息:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'empController'

严重: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'empController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'empService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xiaoy.dao.EmpDao com.xiaoy.service.EmpServiceImpl.empDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'empDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.xiaoy.dao.EmpDapImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [create table Emp (empno integer not null auto_increment, deptno integer, ename varchar(255), primary key (empno))]
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:311)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
	at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
	at javax.servlet.GenericServlet.init(GenericServlet.java:212)
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4044)
	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4350)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)

错误信息中提示bean创建失败,首先想到是不是我的spring和hibernate版本是不是不对应,后来去网上查了版本对应关系如下:

org.springframework 3.0.x对应org.hibernate4.0.x版本

org.springframework 3.2.x对应org.hibernate4.2.x版本

org.springframework 4.0.x对应org.hibernate4.3.x版本

org.springframework 4.1.x对应org.hibernate5.0.x版本

org.springframework 4.3.x对应org.hibernate5.1.x版本

嗯,确实不对应,我又去下载了springframework 4.2.4和hibernate5.0.1final版本,依然报这个异常,好吧又在网上找了好久的解决方法,发现并不能解决问题,因为造成该异常的起因很多,以下是解决方案整理:

1.spring和hibernate版本不对应,上文已经提到了,去官网下载对应的版本即可。

2.如果使用的注解的方式管理bean,在spring配置文件中未开启组件扫描也会导致无法创建bean,因为找不到对应的类的方法。

<!-- 开启组件扫描 -->
    <context:component-scan base-package="com.xiaoy"/>

3.hibernate版本配置错误,我所使用的是hibernate5版本,所以在配置文件中做如下配置,如果是3或者4版本需要修改class属性。

<!-- 模板对象,提供了一套增删改查方法 -->
     <bean class="org.springframework.orm.hibernate5.HibernateTemplate">
     	<property name="sessionFactory" ref="sessionFactory"></property>
     </bean>

4.注解@Service、@Repository、@Controller使用有误,一开始我的注解是这样的

@Controller
public class EmpController {
	@Resource
	private EmpService empService;
	@RequestMapping("list.do")
	public String list(Model model){	
		List<Emp> list = empService.list();
		model.addAttribute("list",list);
		return "list";
	}

@Service
public class EmpServiceImpl implements EmpService {
	@Autowired
	private EmpDao empDao;
	public List<Emp> list() {
		List<Emp> list = empDao.getList();
		return list;
	}

需要在@Service、@Repository、@Controller后面配置别名,默认对象名是首字母小写,默认的@Autowired容易出错

5.注入数据源出错(这也是最容易忽视和找不到的错误)

我的db.properties配置的url:mysql.dateSource.url=jdbc:mysql://localhost:3306/xiaoy,一开始只写到3306,没有指定xiaoy这个库,也会导致这个异常信息,这谁出这个异常会去看这个资源文件啊。。。

以上就是这个异常信息处理的总结,欢迎补充~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值