BeanCreationException:创建名称为' sessionFactory'的bean时出错在ServletContext资源中定义

时间:2014-05-29 03:05:46

标签: spring hibernate

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

  

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


我尝试将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$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

我的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>

&GT;

1 个答案:

答案 0 :(得分:1)

configLocation的属性LocalSessionFactoryBean的类型为Resource,您可以指定带前缀的资源类型,例如:

classpath:resources/hibernate.cfg.xml

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

  

5.3.4 ServletContextResource

     

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

     

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

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

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