HibernateTemplate类依赖注入的org.springframework.beans.factory.BeanCreationException

时间:2019-04-08 09:15:30

标签: java spring

将Spring与Hibernate集成时,上面的异常和下面的日志均会出现异常。随附以下所有必需的文件,如果您需要任何东西,请告诉我。仅供参考:我正在使用下面的罐子。

enter image description here

错误日志

  

线程“主”中的异常   org.springframework.beans.factory.BeanCreationException:创建在类路径资源[HibernateApplicationContext.xml]中定义的名称为'd'的bean时出错:设置bean属性'template'时无法解析对bean'template'的引用;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建在类路径资源[HibernateApplicationContext.xml]中定义的名称为“模板”的bean时出错。嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.orm.hibernate3.HibernateTemplate]:找不到默认的构造函数;嵌套的异常是java.lang.NoClassDefFoundError:org / hibernate / JDBCException               
在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378)               
在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)               
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1681)               
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1433)               
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)               
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)               
在org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:320)               
在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)               
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)               
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)               
在com.SpringIntegrationWithHibernate.MainClass.main(MainClass.java:18)           由以下原因引起:org.springframework.beans.factory.BeanCreationException:在类路径资源[HibernateApplicationContext.xml]中创建名称为'template'的bean时出错。嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.orm.hibernate3.HibernateTemplate]:找不到默认的构造函数;嵌套的异常是java.lang.NoClassDefFoundError:org / hibernate / JDBCException               
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1303)               
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1197)               
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)               
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)               
在org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:320)               
在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)               
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)               
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)               
在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367)               ...还有10个           原因:org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.orm.hibernate3.HibernateTemplate]:没有找到默认的构造函数;嵌套的异常是java.lang.NoClassDefFoundError:org / hibernate / JDBCException               
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)               
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1295)               ...另外18个           引起原因:java.lang.NoClassDefFoundError:org / hibernate / JDBCException               
在java.lang.Class.getDeclaredConstructors0(本机方法)               
在java.lang.Class.privateGetDeclaredConstructors(未知来源)               
在java.lang.Class.getConstructor0(未知来源)               
在java.lang.Class.getDeclaredConstructor(未知来源)               
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)               ...另外19个           引起原因:java.lang.ClassNotFoundException:org.hibernate.JDBCException               
在java.net.URLClassLoader.findClass(未知来源)               
在java.lang.ClassLoader.loadClass(未知来源)               
在sun.misc.Launcher $ AppClassLoader.loadClass(未知来源)               
在java.lang.ClassLoader.loadClass(未知来源)               ...还有24个

主类

    package com.SpringIntegrationWithHibernate;

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;

    public class MainClass {



        public static void main(String[] args) {
            // TODO Auto-generated method stub

            Resource r=new ClassPathResource("HibernateApplicationContext.xml");  
            BeanFactory factory=new XmlBeanFactory(r);  

            EmployeeDao dao=(EmployeeDao)factory.getBean("d");  

            Employees e=new Employees();  
            e.setId(114);  
            e.setFirstName("Varun");  
            e.setLastName("Dhawan");  

            dao.saveEmployee(e);

        }

    }

休眠映射Xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC  
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <hibernate-mapping>
        <class name="com.SpringIntegrationWithHibernate.Employees"
            table="Employees_Spring">
            <id name="id">
                <generator class="assigned"></generator>
            </id>

            <property name="FirstName"></property>
            <property name="LastName"></property>
        </class>

    </hibernate-mapping> 

##员工DAO类##

    package com.SpringIntegrationWithHibernate;


    import java.util.*;

    import org.springframework.orm.hibernate3.HibernateTemplate;




    public class EmployeeDao {  
    HibernateTemplate template;  
    public void setTemplate(HibernateTemplate template) {  
        this.template = template;  
    }  
    //method to save employee  
    public void saveEmployee(Employees e){  
        template.save(e);  
    }  
    //method to update employee  
    public void updateEmployee(Employees e){  
    //    template.update(e);  
    }  
    //method to delete employee  
    public void deleteEmployee(Employees e){  
    //    template.delete(e);  
    }  
    //method to return one employee of given id  
    //public Employees getById(int id){  
    //    Employees e=(Employees)template.get(Employees.class,id);  
    //    return e;  
    //}  
    //method to return all employees  
    public List<Employees> getEmployees(){  
        List<Employees> list=new ArrayList<Employees>();  
        list=template.loadAll(Employees.class);  
        return list;  
    }  
    } 

##应用程序上下文XML ##
        

    <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.apache.commons.dbcp.BasicDataSource">  
            <property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver"></property>  
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>  
            <property name="username" value="hr"></property>  
            <property name="password" value="umashetti123"></property>  
        </bean>  

        <bean id="mysessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
            <property name="dataSource" ref="dataSource"></property>  

            <property name="mappingResources">  
            <list>  
            <value>Employees.hbm.xml</value>  
            </list>  
            </property>  

            <property name="hibernateProperties">  
                <props>  
                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</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.SpringIntegrationWithHibernate.EmployeeDao">  
        <property name="template" ref="template"></property>  
        </bean>  


        </beans>  

员工持久性课程

    package com.SpringIntegrationWithHibernate;

    public class Employees {


        private int id;
        private String FirstName;
        private String LastName;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getFirstName() {
            return FirstName;
        }

        public void setFirstName(String firstName) {
            FirstName = firstName;
        }


        public String getLastName() {
            return LastName;
        }

        public void setLastName(String lastName) {
            LastName = lastName;
        }

        public Employees() {
            // TODO Auto-generated constructor stub
        }

    }

请帮助我解决这个问题。

0 个答案:

没有答案