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

BeanCreationException:创建ServletContext资源中定义的名为“会话工厂”的bean时出错

郗鹏
2023-03-14

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

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

> 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$1.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
<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>

>

共有1个答案

岳均
2023-03-14

LocalSessionFactoryBeanConfigLocation属性为resource类型,您可以使用前缀指定资源类型,例如:

classpath:resources/hibernate.cfg.xml

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

5.3.4 ServletContextResource

您可以在spring文档的参考资料中看到所有的abaout资源。

 类似资料: