首页 文章

Spring Hibernate中的org.springframework.beans.factory.BeanCreationException

提问于
浏览
1

我使用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>

这是堆栈跟踪:

线程“main”中的异常org.springframework.beans.factory.BeanCreationException:在类路径资源[applicationContext.xml]中定义名称为'd'的bean时出错:在设置bean属性'模板时无法解析对bean'template'的引用“;嵌套异常是org.springframework.beans.factory.BeanCreationException:在类路径资源[applicationContext.xml]中定义名称为'template'的bean创建错误:bean的实例化失败;嵌套异常是java.lang.NoClassDefFoundError:org / hibernate / JDBCException

1 回答

  • 0
    <bean id="d" class="com.javatpoint.EmployeeDao">  
        <property name="template" ref="template"></property>  
    </bean>
    

    您已经创建了对 ref="template" 的引用,而Spring找不到它 .

    Check if all required dependencies are available. 在这里你应该检查 org.springframework.orm.hibernate3.HibernateTemplate jar 是否可用 .

相关问题