Spring-Hibernate:无法实例化DataSource,提示DataSource的构造函数抛出空指针异常


Spring-Hibernate整合,报错:


 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in URL [file:/D:/DevelopTools/apache-tomcat-7.0.54/webapps/HMQMP/WEB-INF/classes/applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/D:/DevelopTools/apache-tomcat-7.0.54/webapps/HMQMP/WEB-INF/classes/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in URL [file:/D:/DevelopTools/apache-tomcat-7.0.54/webapps/HMQMP/WEB-INF/classes/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.jolbox.bonecp.BoneCPDataSource]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:326)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1417)

提示无法实例化DataSource,因为DataSource的构造函数抛出了空指针异常,
检查Spring和Hibernate整合的配置文件,没发现错误,配置文件如下:
applicationContext.xml:


 <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"  
        destroy-method="close">  
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" /> 
        <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" />   
        <property name="idleMaxAge" value="${jdbc.idleMaxAge}" />  
        <property name="maxConnectionsPerPartition" value="${jdbc.maxConnectionsPerPartition}" />  
        <property name="minConnectionsPerPartition" value="${jdbc.minConnectionsPerPartition}" />  
        <property name="partitionCount" value="${jdbc.partitionCount}" />   
        <property name="acquireIncrement" value="${jdbc.acquireIncrement}" />    
        <property name="statementsCacheSize" value="${jdbc.statementsCacheSize}" />    
        <property name="releaseHelperThreads" value="${jdbc.releaseHelperThreads}" />  
        <property name="logStatementsEnabled" value="true"/>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />

        <property name="namingStrategy">
            <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.validator.apply_to_ddl">false</prop>
                <prop key="hibernate.validator.autoregister_listeners">false</prop>
                <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
                <prop key="hibernate.dialect">${hibernate.dialect} </prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
                <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop> 
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_structured_entries">true</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
            </props>
        </property>
        <!-- hibernate annotation config -->
        <property name="packagesToScan" value="com.goodwill" /> 
    </bean>

请大家帮忙看下,这里是什么问题啊?

spring hibernate datasource java-ee

逆游的鱼oO 10 years, 7 months ago

信息少,解决不了
我教你个最笨但也是最有效的办法!既然你认定了是dataSource实例化的问题,那就先把和dataSource实例化无关的内容拿掉,try一下,如果还有问题,就把dataSource实例化里面的内容一行一行的删除加载试,这样基本能试出来是哪行的问题。如果还是不行,再考虑其他方面。

云雨巫山数落红 answered 10 years, 7 months ago

Your Answer