当前位置: 首页 > 知识库问答 >
问题:

SessionFactory初始化空指针spring 3 hibernate 4

都飞跃
2023-03-14

我有一个奇怪的问题,当Spring试图初始化会话工厂时返回 null。我没有在我的代码中注入它在上下文初始化时抛出 null。

import java.util.List;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import fr.demo.bean.Client;
import fr.demo.service.dao.ClientDAO;

@Transactional
@Repository
public class ClientDAOImpl implements ClientDAO{

//  @Autowired
//  private SessionFactory  sessionFactory;

    @Override
    public Integer create(Client obj) {
    return null;
    }
    @Override
    public void delete(Client obj) {
    }
    @Override
    public void update(Client obj) {
    }
    @Override
    public Client find(Integer id) {
        return null;
    }
    @Override
    public List<Client> findAll() {
        return null;
    }
}

这是我的错误:

组织Spring框架清洁工厂。BeanCreationException:使用ServletContext资源[/WEB-INF/resources/spring/database/DataSource.xml]中定义的名称“sessionFactory”创建bean时出错:初始化方法调用失败;嵌套异常是java.lang.NullPointerException

我告诉你,当我在sessionfactory中编写映射Hibernate时,我遇到了这个错误。这是我的地图

@Entity
@Table(name="client")
public class Client implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 8707073906887139645L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Column(name="nom", nullable=false, length=100)
    private String nom;
    @Column(name="prenom", nullable=false, length=100)
    private String prenom;
    @Column(name="adresse", nullable=false, length=100)
    private String adresse;
    @Column(name="telephone", nullable=false, length=100)
    private String telephone;
    @Column(name="email", nullable=false, length=100)
    private String email;
    private String image;
 }

这是servlet上下文:

<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> -->
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
                            http://www.springframework.org/schema/context 
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>
    <context:annotation-config/>
    <context:component-scan base-package="fr.demo.service.web"/>
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/errors"/>
    </bean>
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.JstlView</value>
        </property>
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <import resource="../database/DataSource.xml"/>
    <import resource="../database/Hibernate.xml"/>
    <context:component-scan base-package="fr.demo.service.dao" />
</beans>

和完整的跟踪

SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/resources/spring/database/DataSource.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1488)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:608)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at org.apache.catalina.loader.WebappClassLoader.findLoadedResource(WebappClassLoader.java:3266)
    at org.apache.catalina.loader.WebappClassLoader.getResourceAsStream(WebappClassLoader.java:1480)
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:154)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:291)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1485)
    ... 33 more

May 11, 2013 1:37:55 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet spring-mvc
java.lang.NullPointerException
    at org.apache.catalina.loader.WebappClassLoader.findLoadedResource(WebappClassLoader.java:3266)
    at org.apache.catalina.loader.WebappClassLoader.getResourceAsStream(WebappClassLoader.java:1480)
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:154)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:291)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:608)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我的数据源配置bean:

   <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:context="http://www.springframework.org/schema/context"
        xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:lang="http://www.springframework.org/schema/lang"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
            http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

        <!--  Declaration de la DATASOURCES -->
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location">
                <value>/WEB-INF/resources/properties/database.properties</value>
            </property>
        </bean>
        <bean id="datasource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${db.driverClassName}" />
            <property name="url" value="${db.url}" />
            <property name="username" value="${db.username}" />
            <property name="password" value="" />
        </bean>
        <!--  Declaration Session Factory -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="datasource" />
            <property name="mappingResources">
                <list>
                    <value>fr.demo.bean.Client</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">
                        org.hibernate.dialect.MySQLDialect
                    </prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.connection.autocommit">true</prop> 
                </props>
            </property>
        </bean>
        <bean id="transactionManager"
             class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
            <property name="dataSource" ref="datasource" />
        </bean>
        <tx:annotation-driven transaction-manager="transactionManager"/>

    </beans>

共有1个答案

郝修为
2023-03-14

问题似乎出在以下配置上:

<property name="mappingResources">
    <list>
        <value>fr.demo.bean.Client</value>
    </list>
</property>

setMappingResources的javadoc说:

将HiberNate映射资源设置为在类路径中找到,如“example.hbm.xml”或“我的包/example.hbm.xml”

所以你的配置告诉 Spring 在根包中查找一个名为 fr.demo.bean.Client 的文件,这是错误的。如果要指定带批注的类的名称,请使用...带注释的类

 类似资料:
  • 我发现有两种方法可以初始化hibernate SessionFactory, > 通过org.hibernate.cfg.configuration读取hibernate.cfg.xml进行配置,我将获得java代码

  • 问题内容: 我最近在学习Java,并尝试使用类。但是我无法初始化数组对象 谁能告诉我我要去哪里错了? 问题答案: 当你做 您正在创建 引用数组(即引用变量数组) ,这些 引用数组 未指向其他任何地方,即它们为null。您需要将对象分配给数组中上面创建的引用变量。

  • 问题内容: 另一个链接的问题是在使用strcpy()时出现细分错误吗? 我有一个结构: 如何初始化指向上述类型结构的指针,以及如何初始化指向结构内部的10个字符串(incall [])的指针。 我首先初始化字符串,然后初始化结构。 谢谢。 编辑:我猜我用错了字,应该说分配。实际上,我将此结构作为线程的参数传递。线程数不是固定的,作为参数发送的数据结构对于每个线程都必须是唯一的,并且是“线程安全的”

  • 问题内容: 我正在尝试制作2D切片游戏,当制作包含切片的阵列时,我得到了NullPointerException,这是一些代码。抱歉,如果格式不正确,请第一时间 公共类世界{ 然后在本课程中被称为(我相信这是问题的一部分) 公共类worldDraw { 问题答案: 创建对象数组时,将创建引用数组,但未分配引用。在尝试使用它们之前,必须先执行此操作。认为它类似于创建鸡蛋纸箱。在先将鸡蛋装满纸箱之前,

  • 问题内容: Go Newbie问题:我正在尝试使用默认值初始化以下结构。我知道,如果“ Uri”是字符串而不是指向字符串(* string)的指针,则可以使用。但是我需要这个指针来比较结构的两个实例,如果未设置Uri,则Uri将为nil,例如,当我从json文件解组内容时。但是,如何正确地将这样的结构初始化为“静态默认值”呢? 上面的代码因 问题答案: 无法获得(指向)常数值的地址,这就是初始化失

  • 指针变量的值为内存地址。通常变量直接包含特定值,而指针则包含特定值变量的地址。因此可以说,变量名直接(directly)引用数值,而指针间接(indirectly)引用数值(如图 5.1 通过指针引用数值称为间接引用。 指针和任何其他变量一样,应先声明后使用。下列声明: int *countPtr, count; 声明变量 countPtr 的类型为 int*(即指向整型值的指针),或者说成 co