线程" main"中的例外情况org.springframework.beans.factory.BeanCreationException:在Spring中

时间:2016-06-13 08:26:01

标签: java spring hibernate spring-mvc

[![在此处输入图片描述] [1]] [1]我是Spring的新手。我正在按照一些教程使用Hibernate& amp进行简单的插入操作。弹簧。然而,它最终出现了一些错误,我无法理解这个问题中的确切问题。我的代码

的applicationContext.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.apache.commons.dbcp.BasicDataSource">  
    <property name="driverClassName"  value="com.mysql.jdbc.Driver"></property>  
    <property name="url" value="jdbc:mysql://localhost/test"></property>  
    <property name="username" value="root"></property>  
    <property name="password" value="root"></property>  
</bean>  

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

    <property name="annotatedClasses">  
        <list>  
            <value>com.hib.model.MailHib</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="d" class="com.dao.MailDaoCls">  
<property name="template" ref="template"></property>  
</bean>  


</beans>  

MailDaoCls

package com.dao;

import org.springframework.orm.hibernate3.HibernateTemplate;

import com.bo.model.Mail;
import com.hib.model.MailHib;

public class MailDaoCls {
    HibernateTemplate template;  
    public void setTemplate(HibernateTemplate template) {  
        this.template = template;  
    }  
    //method to save employee  
    public void saveEmployee(MailHib e){  
        template.save(e);  
    } 

}

我的逻辑

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

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

    MailHib mail = new MailHib();
    //mail.setMailId(1);
    mail.setFrmEMail("ffff");
    mail.setToEMail("ssss");
    mail.setSubjectEMail("fffffsf");
    mail.setMessage("messagssse");
    dao.saveEmployee(mail);

它抛出的错误是

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating 
 bean with name 'd' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'template' while setting bean property 
 'template'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'template' defined in class path resource [applicationContext.xml]: 
 Cannot resolve reference to bean 'mysessionFactory' while setting bean property 'sessionFactory'; nested exception is 
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'dataSource' 
 while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find 
class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource

更新 添加commons-dbcp后,错误消息为:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean   with name 'd' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'template' while setting bean property 'template'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'template' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'mysessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/support/lob/LobHandler
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1469)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at com.dao.impl.MailDaoImpl.main(MailDaoImpl.java:59)

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'template' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'mysessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/support/lob/LobHandler
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1469)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)

1 个答案:

答案 0 :(得分:0)

请参阅stacktrace的这一部分:

java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource

看起来你错过了类路径中的commons-dbcp.jar。

添加依赖项:

<!-- http://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>

到你的pom.xml

<强>更新 发生了第二个异常,因为缺少org.springframework.jdbc<Version>.jar