我使用spring和Hibernate集成使用mysql创建了一个简单的java项目,其中包含一个包和3个类,一个应用程序context.xml
文件和一个hbm.xml
用于映射,但编程得到了在运行时使用Exception
崩溃:
org.springframework.beans.factory.BeanCreationException
这是Employee.java文件
public class Employee {
private int id;
private String name;
private int salary;
public Employee() {
}
public Employee(int id, String name, int salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
void display() {
System.out.println(id + " " + name + " " + salary);
}
}
这是Test.java
public class Test {
public static void main(String[] args) {
Resource r = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(r);
EmployeeDao dao = (EmployeeDao) factory.getBean("d");
Employee e = new Employee();
e.setId(114);
e.setName("varun");
e.setSalary(50000);
dao.saveEmployee(e);
}}
这是EmployeeDao.java
public class EmployeeDao {
HibernateTemplate template;
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public void saveEmployee(Employee e) {
template.save(e);
}
public void updateEmployee(Employee e) {
template.update(e);
}
public void deleteEmployee(Employee e) {
template.delete(e);
}
public Employee getById(int id) {
Employee e = (Employee) template.get(Employee.class, id);
return e;
}
public List<Employee> getEmployees() {
List<Employee> list = new ArrayList<Employee>();
list = template.loadAll(Employee.class);
return list;
}}
这是employee.hbm.xml
<hibernate-mapping>
<class name="com.javatpoint.Employee" table="emp555">
<id name="id">
<generator class="assigned"></generator>
</id>
<property name="name"></property>
<property name="salary"></property>
</class>
这是applicationContext.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/spring" />
<property name="username" value="root" />
<property name="password" value="****" />
</bean>
<bean id="mysessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</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="d" class="com.javatpoint.EmployeeDao">
<property name="template" ref="template"></property>
</bean>
这是堆栈跟踪:
线程中的异常&#34; main&#34; org.springframework.beans.factory.BeanCreationException:错误 创建名称为&#39; d&#39;在类路径资源中定义 [applicationContext.xml]:无法解析对bean&#39; template&#39;的引用 设置bean属性&#39;模板&#39 ;;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名称&#39;模板&#39;在类路径资源中定义 [applicationContext.xml]:bean的实例化失败;嵌套 异常是java.lang.NoClassDefFoundError:org / hibernate / JDBCException
答案 0 :(得分:0)
<bean id="d" class="com.javatpoint.EmployeeDao">
<property name="template" ref="template"></property>
</bean>
您已创建对ref="template"
的引用,而Spring找不到它。
检查是否所有必需的依赖项都可用。
在这里,您应该检查org.springframework.orm.hibernate3.HibernateTemplate
jar
是否可用。