2
votes

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

**": 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".


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:

> 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

My spring-servlet is:

<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
Try and use classpath:hibernate.cfg.xml instead of resources/hibernate.cfg.xml Seems like the url resources/hibernate.cfg.xml doesn't exist which is correct. When using maven style, you don't need the resources reference. This is assuming your maven file structure is src/main/resources/hibernate.cfg.xmlPaul Samsotha

1 Answers

1
votes

The property configLocation of LocalSessionFactoryBean is of type Resource and you can specify the type of resource with a prefix, for example:

classpath:resources/hibernate.cfg.xml

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

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.

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

You can see all abaout resources in Resources from spring documentation.