初始化org.springframework.orm.hibernate4.LocalSessionFactoryBean

时间:2016-05-08 11:02:52

标签: spring hibernate

冬眠-cfg.xml中:

<context:property-placeholder location="classpath*:config/db/database.properties"/>

<!-- DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>    

<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
            <value>classpath:config/db/mapping/userdetails.hbm.xml</value>
        </list>
    </property>
</bean>

启动tomcat时出错:

>WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/D:/workspace/project/test/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/testWeb/WEB-INF/classes/config/db/hibernate-cfg.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
  

2016年5月8日上午10:02:02 org.springframework.web.context.ContextLoader initWebApplicationContext   严重:上下文初始化失败   org.springframework.beans.factory.BeanCreationException:创建名称为&#39; sessionFactory&#39;的bean时出错在URL中定义[file:/ D:/workspace/project/test/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/testWeb/WEB-INF/classes/config/db/hibernate- cfg.xml]:调用init方法失败;嵌套异常是java.lang.NullPointerException       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)       at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)       at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:753)       at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)       在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)       在org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)       在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)       在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)       在org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4812)       at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)       在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)       在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1408)       在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1398)       at java.util.concurrent.FutureTask $ Sync.innerRun(Unknown Source)       at java.util.concurrent.FutureTask.run(Unknown Source)       at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)       在java.lang.Thread.run(未知来源)   引起:java.lang.NullPointerException       at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl $ AggregatedClassLoader.getResources(ClassLoaderServiceImpl.java:173)       at java.util.ServiceLoader $ LazyIterator.hasNext(Unknown Source)       at java.util.ServiceLoader $ 1.hasNext(Unknown Source)       at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:340)       在org.hibernate.integrator.internal.IntegratorServiceImpl。(IntegratorServiceImpl.java:40)       在org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:213)       在org.hibernate.cfg.Configuration。(Configuration.java:119)       在org.springframework.orm.hibernate4.LocalSessionFactoryBuilder。(LocalSessionFactoryBuilder.java:140)       在org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:343)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)       ......还有22个

请帮助解决问题。

提前致谢。

2 个答案:

答案 0 :(得分:0)

只需在AbstractAutowireCapableBeanFactory.java设置断点,行号1578(确保已下载Spring源,在IDEA中,您可以通过按Maven工具窗口中的“下载源”按钮下载源代码),您会发现究竟发生了什么。

或者指定你的Spring版本(spring-beans,AbstractAutowireCapableBeanFactory类所在的位置)。

答案 1 :(得分:0)

将所有jar移动到WEB-INF / lib后,它可以正常工作。

感谢。