- 已锁定16小时**。此时正在解决disputes about this question’s content。当前不接受新答案或交互。
我是spring和hib的新手,我用的是netbeans v.8.2,我用的是spring 3.2.7和hib4.3.x库,我该怎么做才能修复这个错误?
错误:创建在类路径资源[applicationContext.xml]中定义的名为'dao'的Bean时出错:设置Bean属性"template"时无法解析对Bean "template"的引用;嵌套的异常是org. springframework. bean.工厂。创建在类路径资源[applicationContext.xml]中定义的名为"template"的Bean时出错:设置Bean属性"sessionFactory"时无法解析对Bean "mysessionFactory"的引用;嵌套的异常是org. springframework. bean.工厂。创建在类路径资源[applicationContext.xml]中定义的名为"mysessionFactory"的Bean时出错:初始化bean失败;嵌套异常为java. lang. NoClassDefFoundError:org/休眠/缓存/缓存提供程序
这是应用程序context.xml
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/kampus"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="mysessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>Mahasiswa.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="mysessionFactory"></property>
</bean>
<bean id="dao" class="dao.MahasiswaDao">
<property name="template" ref="template"></property>
</bean>
</beans>
this is mahasiswa.java (mahasiswa=student in English):
package entity;
public class Mahasiswa implements java.io.Serializable {
private String npm;
private String nama;
private String jurusan;
private String alamat;
public Mahasiswa() {
}
public Mahasiswa(String npm, String nama, String jurusan, String alamat) {
this.npm = npm;
this.nama = nama;
this.jurusan = jurusan;
this.alamat = alamat;
}
public String getNpm() {
return this.npm;
}
public void setNpm(String npm) {
this.npm = npm;
}
public String getNama() {
return this.nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public String getJurusan() {
return this.jurusan;
}
public void setJurusan(String jurusan) {
this.jurusan = jurusan;
}
public String getAlamat() {
return this.alamat;
}
public void setAlamat(String alamat) {
this.alamat = alamat;
}
}
这是大般若。道:
package dao;
import entity.Mahasiswa;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class MahasiswaDao {
HibernateTemplate template;
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public void saveMahasiswa(Mahasiswa m){
template.save(m);
}
}
这个文件是mahasiswa.hbm.xml:
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 30, 2021 6:57:57 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="entity.Mahasiswa" table="mahasiswa" catalog="kampus" optimistic-lock="version">
<id name="npm" type="string">
<column name="npm" length="13" />
<generator class="assigned" />
</id>
<property name="nama" type="string">
<column name="nama" length="100" not-null="true" />
</property>
<property name="jurusan" type="string">
<column name="jurusan" length="100" not-null="true" />
</property>
<property name="alamat" type="string">
<column name="alamat" length="100" not-null="true" />
</property>
</class>
</hibernate-mapping>
and this is the main.java :
public class test06 {
public static void main(String[] args) {
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(r);
MahasiswaDao dao=(MahasiswaDao)factory.getBean("dao");
Mahasiswa m=new Mahasiswa();
m.setNpm("1");
m.setNama("test");
m.setJurusan("");
m.setAlamat("");
dao.saveMahasiswa(m);
}
}
谢啦,谢啦
1条答案
按热度按时间inb24sb21#
您将错过
classpath
上的org.hibernate.cache.Provider
类。按照java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider exception while integrating spring and hiberate
您使用的是Hibernate 4,但是您在
applicationContext.xml
中使用的Spring中与Hibernate相关的类来自org.springframework.orm.hibernate3
包,因此它们需要缺少的依赖项。尝试使用
org.springframework.orm.hibernate4
包中的Hibernate模板和LocalSessionFactoryBean。如果Spring 3.2.7中没有这样的包,请升级到Spring 4。