spring BeanCreationException:使用在 ServletContext 资源中定义的名称“sessionFactory”创建 bean 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23925067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:46:00  来源:igfitidea点击:

BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource

springhibernate

提问by user3685947

Im using hibernate 3.2.7 and spring 3.2.8. The error is ***

我使用 hibernate 3.2.7 和 spring 3.2.8。错误是***

**": Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: ServletContext resource [/resources/hibernate.cfg.xml] cannot be resolved to URL because it does not exist".

**”:在 ServletContext 资源 [/WEB-INF/spring-servlet.xml] 中定义名称为“sessionFactory”的 bean 创建错误:init 方法调用失败;嵌套异常为 java.io.FileNotFoundException:ServletContext 资源 [/resources/ hibernate.cfg.xml] 无法解析为 URL,因为它不存在”。



I try to put hibernate.cfg.xml in src, in webinf and classpath: , but it still dont find it.This is my error stack trace:

我尝试将 hibernate.cfg.xml 放在 src、webinf 和 classpath: 中,但它仍然没有找到它。这是我的错误堆栈跟踪:

> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'sessionFactory' defined in ServletContext
> resource [/WEB-INF/spring-servlet.xml]: Invocation of init method
> failed; nested exception is java.io.FileNotFoundException:
> ServletContext resource [/resources/hibernate.cfg.xml] cannot be
> resolved to URL because it does not exist     at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:296)
>   at
> org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
>   at
> org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:610)
>   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:602)
>   at
> org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
>   at
> org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:521)
>   at
> org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:462)
>   at
> org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
>   at javax.servlet.GenericServlet.init(GenericServlet.java:158)   at
> org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
>   at
> org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
>   at
> org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
>   at
> org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
>   at
> org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
>   at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>   at
> org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
>   at
> org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
>   at
> org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
>   at
> org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:670)
>   at
> org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1839)
>   at
> java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
>   at java.util.concurrent.FutureTask.run(FutureTask.java:262)     at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
>   at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
>   at java.lang.Thread.run(Thread.java:745) Caused by:
> java.io.FileNotFoundException: ServletContext resource
> [/resources/hibernate.cfg.xml] cannot be resolved to URL because it
> does not exist    at
> org.springframework.web.context.support.ServletContextResource.getURL(ServletContextResource.java:154)
>   at
> org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBe

My spring-servlet is:

我的 spring-servlet 是:

<bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
    <value>/</value>
  </property>
  <property name="suffix">
    <value>.xhtml</value>
  </property>
</bean>

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
    p:password="${jdbc.password}" />


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
    <value>resources/hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.connection.SetBigStringTryClob">true</prop>
            <prop key="hibernate.jdbc.batch_size">0</prop>
            <prop key="hibernate.jdbc.batch_size">0</prop>
        </props>
    </property>
</bean>
<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="10000000" />
</bean>
<tx:annotation-driven />

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

>

>

采纳答案by Fernando Rincon

The property configLocationof LocalSessionFactoryBeanis of type Resourceand you can specify the type of resource with a prefix, for example:

该物业configLocationLocalSessionFactoryBean类型是Resource,你可以用一个前缀指定资源的类型,例如:

classpath:resources/hibernate.cfg.xml

By default, in ServletContext, if you dont specify the prefix, the resource use the class ServletContextResource. From Spring Documentation:

默认情况下,在 ServletContext 中,如果不指定前缀,则资源使用 class ServletContextResource。来自 Spring 文档:

5.3.4 ServletContextResource

This is a Resource implementation for ServletContext resources, interpreting relative paths within the relevant web application's root directory.

This always supports stream access and URL access, but only allows java.io.File access when the web application archive is expanded and the resource is physically on the filesystem. Whether or not it's expanded and on the filesystem like this, or accessed directly from the JAR or somewhere else like a DB (it's conceivable) is actually dependent on the Servlet container.

5.3.4 ServletContextResource

这是 ServletContext 资源的 Resource 实现,解释相关 Web 应用程序根目录中的相对路径。

这始终支持流访问和 URL 访问,但仅在 Web 应用程序存档展开且资源物理位于文件系统上时才允许 java.io.File 访问。它是否像这样在文件系统上进行扩展,或者直接从 JAR 或其他地方(如 DB)访问(可以想象)实际上取决于 Servlet 容器。

Then, you need put the prefix or put the resource in the root of the application.

然后,您需要将前缀或资源放在应用程序的根目录中。

You can see all abaout resources in Resourcesfrom spring documentation.

您可以在Spring 文档的Resources 中查看所有 abaout 资源。