无法启动基于Spring ORM的Java应用:HibernateTemplate实例化失败
解决Spring中HibernateTemplate实例化失败:未找到默认构造方法的问题
问题描述
线程"main"中抛出异常:
org.springframework.beans.factory.BeanCreationException:无法实例化类路径资源[config.xml]中定义的名为'hibernateTemplate'的Bean:实例化[org.springframework.orm.hibernate5.HibernateTemplate]失败:未找到默认构造方法
原配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" > <tx:annotation-driven/> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="ds"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/springorm"/> <property name="username" value="root"/> <property name="password" value="ParaSF@59"/> </bean> <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" name="factory"> <property name="dataSource" ref="ds" ></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="annotatedClasses"> <list> <value> com.spring.orm.entity.Student </value> </list> </property> </bean> <bean class=" org.springframework.orm.hibernate5.HibernateTemplate" name="hibernateTemplate"> <property name="sessionFactory" ref="factory"></property> </bean> <bean class="com.spring.orm.dao.StudentDao" name="studentDao"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" name="transactionManager"> <property name="sessionFactory" ref="factory"/> </bean> </beans>
问题原因
Spring ORM 5.x版本的org.springframework.orm.hibernate5.HibernateTemplate没有无参构造方法,它只提供了一个接收SessionFactory作为参数的构造方法。当前配置用<property>标签通过setter方式注入sessionFactory,Spring会优先尝试调用无参构造创建Bean实例,这就触发了"未找到默认构造方法"的异常。
解决方案
改用构造方法注入的方式创建HibernateTemplate Bean,具体修改如下:
方式一:使用<constructor-arg>标签
将原HibernateTemplate的Bean定义替换为:
<bean class="org.springframework.orm.hibernate5.HibernateTemplate" name="hibernateTemplate"> <constructor-arg ref="factory"/> </bean>
方式二:使用p命名空间简化配置
如果想简化代码,也可以用p命名空间直接指定构造参数:
<bean class="org.springframework.orm.hibernate5.HibernateTemplate" name="hibernateTemplate" p:sessionFactory-ref="factory"/>
额外建议:升级Spring Schema版本
原配置中使用的是Spring 2.5的Schema,建议升级到与Spring 5.x匹配的版本,避免潜在兼容性问题,修改后的xsi:schemaLocation如下:
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
内容的提问来源于stack exchange,提问作者Sujit Kate
相关产品推荐
相关产品推荐

