我的问题如下:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.hibernate.cfg.Configuration]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1336)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:471)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.test.Test.main(Test.java:16)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.hibernate.cfg.Configuration]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:111)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:74)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newConfiguration(LocalSessionFactoryBean.java:772)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:517)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1367)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1333)
... 15 more
Caused by: java.lang.ExceptionInInitializerError
at org.hibernate.cfg.Configuration.reset(Configuration.java:174)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:193)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:197)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:96)
... 21 more
Caused by: java.lang.NullPointerException
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:144)
at org.hibernate.cfg.Environment.<clinit>(Environment.java:529)
... 29 more
配置文件: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:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classPath:hibernate.cfg.xml</value>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="pointCut" expression="execution(* *(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>

<bean id="userMgr" class="com.test.UserMgrImp"/>

<bean id="test" class="com.test.Test">
<property name="userMgr" ref="userMgr"/>
</bean>
</beans>
在main函数中做如下操作:
public static void main(String[] args) {
// TODO Auto-generated method stub
BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMgrImp userMgr = (UserMgrImp)bf.getBean("userMgr");

User user = new User();
user.setName("haha");

userMgr.addUser(user);
}
一直报错:我用的是Hibernate3.2.8,Spring2.0.8请问是什么故障

解决方案 »

  1.   

    检查一下sessionFactory的class和classpath是否正确,
      

  2.   

    那些都没问题,好像是jar包的问题
      

  3.   

    请问我用hibernate3.2.8整合spring2.0.6都需要那些jar包啊?
    我加的是:hibernate3.jar以及hibernate中lib/*.jar
    spring中的:spring.jar以及lib/aspectj/*.jar;log4j/*.jar;jakata-commons/common-logging.jar
    请问还需要什么吗?
                      急!!!!!!!!!
      

  4.   

    我也遇到这个问题啊,我在自己电脑上一直能正常运行,可以把代码考到公司的电脑就不行了,公司的电脑是用myeclipse7.0的,我自己电脑是用myeclipse5.5
    <?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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    "> <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
    value="com.mysql.jdbc.Driver">
    </property>
    <property name="initialSize" value="10"></property>
    <property name="maxActive" value="30"></property>
    <property name="url"
    value="jdbc:mysql://localhost:3306/bbs?useUnicode=true&amp;characterEncoding==utf8">
    </property>
    <property name="username" value="root"></property>
    <property name="password" value="100200"></property>
    </bean> <bean id="sessionfactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </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>
    <prop key="hibernate.cache.use_second_level_cache">
    true
    </prop>
    <prop key="hibernate.cache.provider_class">
    org.hibernate.cache.EhCacheProvider
    </prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>
    </props> </property>
    <property name="mappingResources">
    <list>
    <value>
    com/bbs/model/bean/mapping/JforumUsers.hbm.xml
    </value>
    <value>
    com/bbs/model/bean/mapping/JforumGroups.hbm.xml
    </value>
    <value>
    com/bbs/model/bean/mapping/JforumForums.hbm.xml
    </value>
    <value>
    com/bbs/model/bean/mapping/JforumCategorie.hbm.xml
    </value>
    <value>
    com/bbs/model/bean/mapping/JforumTopics.hbm.xml
    </value>
    <value>
    com/bbs/model/bean/mapping/JforumTopicsWatch.hbm.xml
    </value>
    <value>
    com/bbs/model/bean/mapping/JforumPosts.hbm.xml
    </value> <value>
    com/bbs/model/bean/mapping/JforumRanks.hbm.xml
    </value>
    <value>
    com/bbs/model/bean/mapping/JforumAttach.hbm.xml
    </value>
    <value>
    com/bbs/model/bean/mapping/JforumPostsText.hbm.xml
    </value> <value>
    com/bbs/model/bean/mapping/JforumConfig.hbm.xml
    </value>
    <value>
    com/bbs/model/bean/mapping/JforumPrivmsgsText.hbm.xml
    </value>
    <value>
    com/bbs/model/bean/mapping/JforumPrivmsgs.hbm.xml
    </value>
    <value>
    com/bbs/model/bean/mapping/JforumAttachDesc.hbm.xml
    </value>
    </list>
    </property>
    </bean>
    </beans>
      

  5.   

    框架集成问题参考我的帖子http://topic.csdn.net/u/20081220/02/57b7ede0-213b-44ba-ae72-96375612c6fd.html别忘了给分